<template>
    <div class="hc-login-body hc-full">
        <img class="hc-login-bg" :src="bg1Png" :data-src="bgPng" loading="lazy" alt="bg">
        <img class="hc-login-img" :src="imgPng" :data-src="imgPng" loading="lazy" alt="bg">
        <div class="hc-login-main hc-h-full hc-flex-center w-[50%]">
            <div class="hc-login-main-content relative mt-30 w-[400px]">
                <div class="app-title mb-6 text-center text-[28px] text-white font-bold">
                    <span>欢迎使用</span>
                    <span class="name text-red-5">数据分析</span>
                    <span>系统</span>
                </div>
                <div class="hc-login-form-card bg-white p-6">
                    <div class="login-title mb-6 text-[28px] font-bold">登录</div>
                    <el-form ref="formRef" :model="formValue" :rules="formRules" label-position="left" label-width="0px" size="large">
                        <el-form-item prop="username">
                            <el-input v-model="formValue.username" clearable placeholder="请输入账号" />
                        </el-form-item>
                        <el-form-item class="mt-8" prop="password">
                            <el-input v-model="formValue.password" clearable placeholder="请输入密码" show-password type="password" @keyup="passwordKeyUp" />
                        </el-form-item>
                        <el-form-item>
                            <el-checkbox v-model="checkbox" label="记住密码" />
                        </el-form-item>
                        <el-form-item class="mb-0 mt-6">
                            <el-button class="text-[16px] text-white" :loading="loading" block color="#20C88B" @click="formValidateClick">登 录</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import router from '~src/router/index'
import { useAppStore } from '~src/store'
import { formValidate } from 'js-fast-way'
import { useAppLogin } from '~store/user'

//图片文件
import bg1Png from '~src/assets/login/bg1.png'
import bgPng from '~src/assets/login/bg.png'
import imgPng from '~src/assets/login/img.png'

//初始化
const store = useAppStore()

onMounted(() => {
    store.clearStoreData() //先清理下缓存
    // 兼容性脚本，如果loading属性不被支持
    document.querySelectorAll('img[loading="lazy"]').forEach((img) => {
        img.src = img.dataset.src
    })
})

//表单
const formRef = ref(null)
const checkbox = ref(false)
const formValue = ref({ tenantId: '000000', username: '', password: '', type: 'account' })
const formRules = {
    username: {
        required: true,
        message: '请输入账号',
        trigger: 'blur',
    },
    password: {
        required: true,
        message: '请输入密码',
        trigger: 'blur',
    },
}
const passwordKeyUp = (e) => {
    if (e.key === 'Enter') {
        formValidateClick()
    }
}

//登录
const loading = ref(false)
const formValidateClick = async () => {
    const formRes = await formValidate(formRef.value)
    if (!formRes) return false
    //登录请求
    loading.value = true
    const { error, msg } = await useAppLogin(formValue.value)
    //登录失败
    if (error) {
        loading.value = false
        window?.$message?.error(msg)
        return false
    }
    //登录成功
    loading.value = false
    window?.$message?.success('登录成功')
    setTimeout(() => {
        loading.value = false
        router.push({ name: 'index' })
    }, 1500)
}
</script>

<style lang="scss">
@import "~src/styles/view/login.scss";
</style>
