<template>
    <div class="login_wrap">
        <div class="main">
            <div class="logo">
                <img class="logo_border" src="@/assets/img/login_logo_border.png" alt="">
                <img class="logo_img" src="@/assets/img/login_logo.png" alt="">
            </div>
            <h2 class="title">交通运输综合执法检查系统</h2>
            <form class="form_wrap" @submit="submit">
                <div class="form_item" :class="{ error: errors.userName }">
                    <div class="icon">
                        <img src="@/assets/img/login_user_icon.png" alt="">
                    </div>
                    <input class="input" v-model="loginFormData.userName" type="text" placeholder="请输入账号" @input="clearError('userName')">
                </div>
                <div class="form_item" :class="{ error: errors.password }">
                    <div class="icon">
                        <img src="@/assets/img/login_password_icon.png" alt="">
                    </div>
                    <input class="input" v-model="loginFormData.password" type="password" placeholder="请输入密码" @input="clearError('password')">
                </div>
                <div class="options">
                    <label class="save_password">
                        <span class="check_box" :class="{ checked: savePasswordFlag }">
                            <img src="@/assets/img/login_checked_icon.png" alt="">
                        </span>
                        <input v-model="savePasswordFlag" @change="changSavePassword" type="checkbox" name="" id="">
                        <span>记住密码</span>
                    </label>
                    <a>忘记密码</a>
                </div>
                <button class="submit">登录</button>
            </form>
        </div>
    </div>
</template>


<script setup lang="ts">
import { ref } from "vue"
import { login } from "@/api/user.ts"
import { ElMessage } from "element-plus"
import {useRouter} from "vue-router"
const router = useRouter()
const savePasswordFlag = ref(!!(localStorage.getItem('user')))
const loginFormData = ref(JSON.parse(localStorage.getItem('user') || '{"userName":"","password":""}'))
// 添加错误状态
const errors = ref({
    userName: false,
    password: false
})

// 添加监听函数来清除错误
function clearError(field: 'userName' | 'password') {
    errors.value[field] = false
}

function changSavePassword() {
    if (savePasswordFlag.value) {
        localStorage.setItem("user", JSON.stringify(loginFormData.value))
    } else {
        localStorage.removeItem("user")
    }
}
// 提交表单
function submit(event: Event) {
    event.preventDefault()
    changSavePassword()
    if (!validate()) return;
    router.push("/")
}

/**表单校验 */
function validate() {
    errors.value.userName = !loginFormData.value.userName
    errors.value.password = !loginFormData.value.password

    if (errors.value.userName) {
        ElMessage({
            type: "error",
            message: "请输入用户名"
        })
        return false
    }
    if (errors.value.password) {
        ElMessage({
            type: "error",
            message: "请输入密码"
        })
        return false
    }
    return true
}



async function toLogin() {
    const [err, res] = await login({})
}

</script>

<style lang="less" scoped>
.login_wrap {

    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-image: url("@/assets/img/login_bg.png");
    background-size: cover;
    background-color: antiquewhite;

    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
        // background-color: antiquewhite;


        .logo {
            width: 254px;
            height: 254px;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;


            .logo_border {
                position: absolute;
                transform: translateX(-3px);
                width: 100%;
                height: 100%;
            }

            .logo_img {
                width: 160px;

            }
        }

        .title {
            height: 60px;
            opacity: 0.93;
            font-size: 42px;
            font-weight: 800;
            line-height: 60px;
            text-align: center;
            color: #0055E9;
        }

        .form_wrap {
            width: 658px;
            height: 529px;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            margin-top: 40px;
            border-radius: 4px;
            background: #FFFFFF;
            box-shadow: 0px 4px 30px 0px rgba(220, 220, 220, 0.1);

            .form_item {
                width: 522px;
                height: 72px;
                border-radius: 2px;
                display: flex;
                flex-direction: row;
                align-items: center;
                padding: 10px 16px 10px 22px;
                background: #F5F7FA;
                border: 1px solid #FFFFFF;

                .icon img {
                    width: 24px;
                    height: 24px;
                }

                &+.form_item {
                    display: flex;
                    margin-top: 32px;
                }

                .input {
                    height: 100%;
                    flex: 1;
                    border: none;
                    background-color: transparent;
                    padding: 0 18px;
                    color: #1A1A1A;
                    font-size: 21px;

                    &:-webkit-autofill,
                    &:-webkit-autofill:hover,
                    &:-webkit-autofill:focus,
                    &:-webkit-autofill:active {
                        transition: background-color 5000s !important;
                        -webkit-text-fill-color: rgba(0, 0, 0, 0.65) !important;
                    }

                    &:focus {
                        outline: none;
                    }
                }

                &.error {
                    border: 1px solid #ff4d4f;
                    background: #fff2f0;
                }

            }

            .options {
                display: flex;
                justify-content: space-between;
                margin: 24px 0 56px;
                width: 522px;
                font-size: 22px;
                color: #767676;

                .save_password {
                    display: flex;
                    align-items: center;
                    cursor: pointer;

                    input {
                        display: none;
                    }

                    .check_box {
                        width: 22px;
                        height: 22px;
                        margin-right: 12px;

                        border-radius: 50%;
                        border: 1px #767676 solid;
                        display: flex;
                        justify-content: center;
                        align-items: center;

                        img {
                            width: 14px;
                        }
                    }

                    .check_box.checked {
                        background-color: #0055E9;
                        border: none;
                    }


                }
            }

            .submit {
                width: 522px;
                height: 80px;
                border: none;
                cursor: pointer;
                border-radius: 2px;
                font-size: 28px;
                color: #fff;
                background: linear-gradient(180deg, #004DE7 0%, #009BFC 100%);
            }
        }

    }


}
</style>