<template>
  <sign-layout class="user-login" title="账号登录">
    <el-form ref="formRef" :model="user" :rules="formRules">
      <el-form-item prop="LoginID">
        <el-input placeholder="用户名/账号/手机号" v-model="user.LoginID"></el-input>
      </el-form-item>
      <el-form-item prop="LoginPWD">
        <el-input type="password" placeholder="请输入您的登录密码" v-model="user.LoginPWD">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox label="记住账号"></el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button class="submit" type="primary" :loading="loading" @click="userLogin">
          <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 { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useSign } from '@/business/sign'
import SignLayout from '../components/layout/index.vue'

const { loading, user, signIn } = useSign()
const route = useRoute()
const router = useRouter()
const formRef = ref<FormInstance>()
const formRules: FormRules = {
  LoginID: [
    { required: true, max: 20, message: '随便输入', trigger: 'blur' }
  ],
  LoginPWD: [
    { required: true, max: 16, message: '随便输入', trigger: 'blur' }
  ]
}

const userLogin = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      signIn().then(() => {
        const redirect = route.query.redirect;
        if (redirect) {
          router.replace(redirect.toString());
        } else {
          router.replace('/');
        }
      }).catch((err) => ElMessage.error(err))
    }
  })
}
</script>

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