<script setup lang="ts">
import { reactive, ref } from 'vue'
import { fnMd5 } from '@/utils/common'
import { useRouter } from 'vue-router'
import { GlobalStore } from '@/stores/index'
import { ElNotification } from 'element-plus'
import type { InLogin } from '@/typings/config'
import { Login_verification } from '@/api/user/index'
import type { FormInstance, FormRules } from 'element-plus'
import { View, Lock, Hide, UserFilled } from '@element-plus/icons-vue'

const loading = ref(false)
const router = useRouter()
const globalStore = GlobalStore()
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<InLogin>({
  username: '',
  password: ''
})

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const rules = reactive<FormRules>({
  username: [{ required: true, message: '请输入用户账号', trigger: 'blur' }],
  password: [{ required: true, validator: validatePass, trigger: 'blur' }]
}) 

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  console.log('验证')
  // formEl.validate(async valid => {
  //   if (valid) {
  //     try {
  //       loading.value = true
  //       const res: any = await Login_verification({
  //         bzd_username: ruleForm.username,
  //         bzd_password: fnMd5(ruleForm.password)
  //       })

  //       if (res.Code === 200) {
  //         sessionStorage.setItem('User_tokey', res.Data)
  //         globalStore.SET_User_Tokey(res.Data)
  //         router.replace('/')
  //         ElNotification.success({
  //           title: '登录成功',
  //           message: '欢迎进入宝智达ERP系统！',
  //           position: 'bottom-right'
  //         })
  //         setTimeout(() => {
  //           loading.value = false
  //         }, 1000)
  //       }
  //     } catch (error: any) {
  //       // if (error.Code === 202) {
  //       //   loading.value = false
  //       // }
  //       loading.value = false
  //     }
  //   } else {
  //     return false
  //   }
  // })
    formEl.validate(valid => {
    if (valid) {
      (async () => {
        try {
          loading.value = true
          const res: any = await Login_verification({
            bzd_username: ruleForm.username,
            bzd_password: fnMd5(ruleForm.password)
          })
  
          if (res.Code === 200) {
            sessionStorage.setItem('User_tokey', res.Data)
            globalStore.SET_User_Tokey(res.Data)
            router.replace('/')
            ElNotification.success({
              title: '登录成功',
              message: '欢迎进入宝智达ERP系统！',
              position: 'bottom-right'
            })
            setTimeout(() => {
              loading.value = false
            }, 1000)
          }
        } catch (error: any) {
          loading.value = false
        }
      })()
    }
  })
}

let passType = ref('password')
const changeType = () => {
  passType.value = passType.value === 'password' ? 'text' : 'password'
}
</script>

<template>
  <div class="login">
    <div class="content">
        
      <div class="logo">
        <img src="../assets/images/icon.png" />
      </div>
      <div class="title">
        
        <h2>宝智达ERP系统</h2>
        <span>ShenZhen Baozhida Technology ERP. system</span>
      </div>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" type="text" :prefix-icon="UserFilled" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            :type="passType"
            :prefix-icon="Lock"
            autocomplete="off"
            placeholder="请输入密码"
          >
            <template #suffix>
              <span class="view">
                <el-icon v-if="passType === 'password'" class="el-input__icon" @click="changeType">
                  <Hide />
                </el-icon>
                <el-icon v-else class="el-input__icon" @click="changeType">
                  <View />
                </el-icon>
              </span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="submit" type="primary" :loading="loading" @click.enter="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="scss">
@import './login.scss';
</style>
