<template>
  <sign-layout class="user-login" :title="islogin ? '登录' : '重置密码'">
    <el-form ref="formRef" :model="formData" :rules="formRules" v-if="islogin">
      <el-form-item prop="userName">
        <el-input placeholder="用户名/账号/手机号" v-model="formData.userName">
          <!-- <template #append>
            <el-checkbox v-model="remember"></el-checkbox>
          </template> -->
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" placeholder="请输入您的登录密码" v-model="formData.password">
        </el-input>
      </el-form-item>
      <el-form-item>
        <SliderVerify @statu="slide" v-if="showSliderVerify" />
      </el-form-item>
      <el-form-item>
        <div class="text-link">
          <span @click="click" v-if="shwoRegister">立即注册</span>
          <span @click="islogin = false">忘记密码？</span>
        </div>
      </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-form-item>
    </el-form>
    <Forget @close="islogin = true" v-else></Forget>
    <el-dialog class="register" v-model="isRegister" title="扫码注册" :width="360" center align-center>
      <div v-loading="rloading" v-if="rloading"></div>
      <div style="text-align: center;" v-else>
        <app-qrcode class="app-register-code__qrcode" :width="240" :text="qrContent" />
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="isRegister = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
    <PasswordMidify v-if="showPasswordMidify" @closed="onClosed" />
  </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 { useLogin } from '@/business/login'
import { useGlobalStore, useMenuStore } from '@/stores'
import service from '@/services'
import SignLayout from '../components/layout/index.vue'
import Forget from "../forget/index.vue"
import AppQrcode from '@/components/base/qrcode/index.vue'
import PasswordMidify from '@pc/components/layouts/header/components/modify/index.vue' // 待优化
import SliderVerify from '@/components/base/slider-verify/index.vue' // 临时调用，待优化

const { formData, userLogin, userLogout } = useLogin()
const route = useRoute()
const router = useRouter()
const globalStore = useGlobalStore()
const menuStore = useMenuStore()
const formRef = shallowRef<FormInstance>()
const loading = shallowRef(false)
const shwoRegister = shallowRef(false)
const islogin = shallowRef(true)
const isRegister = shallowRef(false)
const qrContent = shallowRef('')
const rloading = shallowRef(false)
const showPasswordMidify = shallowRef(false) // 显示修改密码
const showSliderVerify = shallowRef(true) // 验证滑块组件重载
const sliderVerifyStatus = shallowRef(false) // 滑块验证状态

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

const click = () => {
  isRegister.value = true
  rloading.value = true
  service.onReady().then((res) => {
    qrContent.value = res.mobileOpenUrl
  }).finally(() => {
    rloading.value = false
  })
}

// 滑块验证 
const slide = (vfcStatu: { statu: string }) => {
  if (vfcStatu.statu === 'success') {
    sliderVerifyStatus.value = true
  } else {
    sliderVerifyStatus.value = false
  }
}

const onClosed = () => {
  userLogout()
  showPasswordMidify.value = false
}

const formSubmit = () => {
  const reset = () => {
    loading.value = false
    showSliderVerify.value = false
    sliderVerifyStatus.value = false
    formData.password = ''
    setTimeout(() => {
      showSliderVerify.value = true
    }, 0)
  }
  formRef.value?.validate(async (valid) => {
    if (valid && sliderVerifyStatus.value) {
      try {
        loading.value = true
        const forcedPasswordChange = await userLogin()
        if (forcedPasswordChange) {
          reset()
          showPasswordMidify.value = true
        } else {
          await menuStore.getUserRoutes()
          const redirect = route.query.redirect
          if (redirect) {
            router.replace(redirect.toString())
          } else {
            router.replace('/')
          }
        }
      } catch (err) {
        reset()
        ElMessage.error('登录失败：' + err as string)
      }
    }
  })
}

service.systemInfoAsync.then(() => {
  shwoRegister.value = globalStore.hasSystemModule('register')
})
</script>

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