<template>
  <sign-layout class="user-login" :title="t('auth.login.title')">
    <el-form ref="formRef" :model="formData" :rules="formRules">
      <el-form-item prop="accountCode">
        <el-input :placeholder="t('auth.login.accountCode')" v-model="formData.accountCode" size="large" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" :placeholder="t('auth.login.password')" v-model="formData.password" size="large" />
      </el-form-item>
      <el-form-item prop="verifyCode" v-if="service.getConfig('loginVerifyCodeEnabled')">
        <div class="el-form-item--col">
          <el-input :placeholder="t('auth.login.verifyCode')" v-model="formData.verifyCode" size="large" />
          <el-image :src="verifyImage" style="width: 180px; height: 38px; cursor: pointer;" @click="run()"
            v-if="verifyImage" />
        </div>
      </el-form-item>
      <!-- <el-form-item>
        <el-checkbox label="记住密码" />
      </el-form-item> -->
      <el-form-item>
        <el-button class="submit" color="#354d67" :loading="loading" @click="formSubmit">
          <span v-if="loading">{{ t('auth.login.loading') }}</span>
          <span v-else>{{ t('operation.login') }}</span>
        </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, FormInstance, FormRules } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { verifyCode } from '@/services/api/user'
import { useLogin } from '@/business/login'
import { i18n } from '@/stores'
import service from '@/services'
import dynamicRouter from '../../../router/dynamicRouter'
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 verifyImage = shallowRef('') // 验证码图片

const { global: { t } } = i18n

const formRules: FormRules = {
  accountCode: [
    { required: true, max: 20, message: t('common.pleaseenter'), trigger: 'blur' }
  ],
  password: [
    { required: true, max: 32, message: t('common.pleaseenter'), trigger: 'blur' }
  ],
  verifyCode: [
    { required: true, message: '请输入验证码' }
  ]
}

const { run } = useRequest(verifyCode, {
  manual: true,
  onSuccess: (res) => {
    formData.key = res.data.key
    verifyImage.value = res.data.img
  }
})

const formSubmit = () => {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true
        await userLogin()

        // 路由跳转前需移除 NotFound 路由，否则可能因为动态路由还未加载，路由不存在而被匹配到 NotFound 页面
        dynamicRouter.unregisterRoutes()

        const redirect = route.query.redirect
        if (redirect) {
          router.replace(redirect.toString())
        } else {
          router.replace('/')
        }
      } catch (err) {
        loading.value = false
        ElMessage.error(err as string)
      }
    }
  })
}

service.onReady().then(() => run())
</script>