<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import type { InLogin } from '@/typings/config'
import { User, View, Lock, Hide } from '@element-plus/icons-vue'
import { Login_verification } from '@/api/user/index'
import { useRouter } from 'vue-router'
import { GlobalStore } from '@/stores/index'
import { fnMd5 } from '@/utils/common'
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
  formEl.validate(async valid => {
    if (valid) {
      let res: any = {}
      res = await Login_verification({ bzd_username: ruleForm.username, bzd_password: fnMd5(ruleForm.password) })
      if (res.Code === 200) {
        localStorage.setItem('User_tokey', res.Data)
        globalStore.SET_User_Tokey(res.Data)
        router.replace('/')
        ElNotification.success({
          title: '登录成功',
          message: '欢迎进入宝智达ERP系统！',
          position: 'bottom-right'
        })
      }
    } else {
      return false
    }
  })
}

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

<template>
  <div class="login">
    <div class="content">
      <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 label="账号：" prop="username">
          <el-input v-model="ruleForm.username" type="text" :prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="ruleForm.password" :type="passType" :prefix-icon="Lock" autocomplete="off">
            <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" @click.enter="submitForm(ruleFormRef)">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

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