<template>
    <div>
        <div class="bigBox">
            <div class="winBox">
                <button class="winBtn" @click="WinMin">-</button>
                <button class="winBtn" @click="WinClose">×</button>
            </div>
            <!-- 信息 -->
            <div class="imgBox">
                <img src="../../assets/image/loginImg.png" alt="">
                <!-- <p>登录</p> -->
            </div>
            <!-- 登录信息 -->
            <div class="formBox">
                <el-form :model="loginForm" label-width="45px" style="padding-top: 24px;">
                    <el-form-item label="账号">
                        <el-input class="loginInp" v-model="loginForm.username" placeholder="你的账号..." maxlength="32" />
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input class="loginInp" type="password" v-model="loginForm.userpass" show-password
                            placeholder="你的密码..." maxlength="32" />
                    </el-form-item>
                </el-form>
                <div @keyup.enter="sureLog">
                    <el-button class="loginBtn" type="primary" @click="sureLog">登录</el-button>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
import { ref, onMounted, onBeforeUnmount } from "vue"
import { useRouter } from 'vue-router'
import { ElMessage } from "element-plus"
import login from "@/api/login"
import { useDataStore } from '@/store/modules/golbal-data';
const userStoreCode = useDataStore();
export default {
    setup() {
        let less = ref("")
        let loginForm = ref({
            username: "",
            userpass: ""
        })
        let isLogin = ref(false)
        let router = useRouter()
        function encryptPassword(password) {
            if (typeof SmCrypto == 'undefined') {
                window.location.reload()
            } else {
                let val = SmCrypto.doSm3AndSm2Encrypt(password) + '0000'
                let ascCode = 0
                for (var i = 0; i < password.length; i++) {
                    ascCode = password.charCodeAt(i).toString(16);
                    // console.log(ascCode,'asc');
                    if (ascCode.length == 1) {
                        val += '00' + ascCode
                    } else if (ascCode.length == 2) {
                        val += '0' + ascCode
                    } else {
                        val += ascCode
                    }
                }
                return val
            }
        }
        function sureLog() {
            let kis = encryptPassword(loginForm.value.userpass)
            login.LoginNow({
                login_account: loginForm.value.username,
                pwd: kis
            }).then(res => {
                if (res.returncode === 200) {
                    isLogin.value = true
                    sessionStorage.setItem("loginStatus", isLogin.value)
                    sessionStorage.setItem("userInfo", JSON.stringify(res.data))
                    if (typeof (window.windowEx) != "undefined") {
                        windowEx.Size(0, 0) //全屏
                        windowEx.Position('center')
                    }
                    router.push('/home/mission')
                    userStoreCode.GlobalCodes();
                    ElMessage({
                        message: '登陆成功！',
                        type: "success"
                    })
                } else {
                    ElMessage({
                        message: res.msg,
                        type: "error"
                    })
                }
            })
        }
        // 添加键盘事件监听器
        function handleKeyUp(event) {
            if (event.key === 'Enter') {
                sureLog();
            }
        };
        function WinClose() {
            windowEx.Exit()
        }
        function WinMin() {
            windowEx.Min()
        }
        onMounted(() => {
            window.addEventListener('keyup', handleKeyUp);
            // window.onload = function () {
            //     // 设置定时器，在 5000 毫秒（即 5 秒）后执行刷新操作
            //     setTimeout(function () {
            //         location.reload(); // 执行页面刷新
            //     }, 100); // 5000 毫秒 = 5 秒
            // };
        })
        onBeforeUnmount(() => {
            window.removeEventListener('keyup', handleKeyUp);
        })
        return {
            encryptPassword,
            less,
            sureLog,
            loginForm,
            isLogin,
            WinMin,
            WinClose,
            handleKeyUp,
        }
    }
}
</script>

<style scoped>
.bigBox {
    width: 31.8rem;
    height: 35.5rem;
    margin: 0;
    background: #FFFFFF;
    box-shadow: 0px 1px 1.25rem 2px rgba(61, 128, 177, 0.13);
    position: absolute;
    border: 1px solid #ededed;
    border-radius: 3px;
    left: 50%;
    top: 50%;
    margin-top: -17.25rem;
    margin-left: -15.9rem
}

.formBox {
    width: 320px;
    height: 244px;
    text-align: center;
    background: #FFFFFF;
    box-shadow: 0px 1px 1.67rem 3px rgba(52, 123, 231, 0.12);
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    margin: 0 auto;
}

.imgBox {
    width: 22.67rem;
    height: 8.33rem;
    margin: 0 auto;
    margin-left: 90px;
    padding-top: 40px;
}

.loginInp {
    width: 22.5rem;
    height: 3.33rem;
    margin-top: 5px;
    border: 1px solid #A3ADE0;
}

.loginBtn {
    width: 22.67rem;
    height: 3.33rem;
    margin-top: 10px;
    background-color: #255CE7;
}

.winBox {
    width: 5.67rem;
    height: 5.67rem;
    position: absolute;
    top: 0px;
    right: 0px;
    color: #676767;
}

.winBtn {
    width: 2.83rem;
    height: 2.83rem;
    background-color: transparent;
    border: none;
    font-size: 2.5rem;
    text-align: center;
    line-height: 2.83rem;
    color: #676767;
}

.winBtn:nth-child(1):hover {
    border: 1px black solid;
}

.winBtn:nth-child(2):hover {
    background-color: red;
}
</style>