<template>
  <sign-layout class="user-login" title="账号登录">
    <el-form ref="formRef" :model="formData" :rules="formRules">
      <el-form-item prop="LoginID">
        <el-input placeholder="用户名/账号/手机号" v-model="formData.LoginID"></el-input>
      </el-form-item>
      <el-form-item prop="LoginPWD">
        <el-input type="password" placeholder="请输入您的登录密码" v-model="formData.LoginPWD">
        </el-input>
      </el-form-item>
      <!-- <el-form-item>
        <el-checkbox label="记住账号" v-model="remember"></el-checkbox>
      </el-form-item> -->
      <el-form-item>
        <el-button class="submit" type="primary" :loading="loading" @click="formSubmit">
          <span v-if="loading">正在登录</span>
          <span v-else>登录</span>
        </el-button>
        <!-- <el-button>注册</el-button> -->
      </el-form-item>
    </el-form>
  </sign-layout>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { ClientType } from '@/constants/client'
import { useLogin } from '@/business/login'
import { menuStore } from '@/stores'
import SignLayout from '../components/layout/index.vue'

const { formData, userLogin } = useLogin()
const route = useRoute()
const router = useRouter()
const formRef = shallowRef<FormInstance>()
const loading = shallowRef(false)
//const remember = shallowRef(false) // 记住账号

const formRules: FormRules = {
  LoginID: [
    { required: true, max: 20, message: '请输入登录账号', trigger: 'blur' }
  ],
  LoginPWD: [
    { required: true, max: 16, message: '请输入登录密码', trigger: 'blur' }
  ]
}

const formSubmit = () => {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        await userLogin(ClientType.Web)
        await menuStore.actions.getUserMenuList()
        const redirect = route.query.redirect
        if (redirect) {
          router.replace(redirect.toString())
        } else {
          router.replace('/')
        }
      } catch (err) {
        loading.value = false
        ElMessage.error(err as string)
      }
    }
  })
}
</script>

<style lang="less" scoped>
@import './index.less';
</style>