<template>
    <div
        class="auto-login-page"
        v-loading="true"
        element-loading-text="正在跳转中，请稍候..."
        element-loading-background="rgba(255, 255, 255, 0.5)"
    ></div>
</template>

<script setup>
import { SET_TOKEN, SET_UNAME, SET_USER_INFO } from "@/store/modules/app/type";
import { ref, reactive, toRefs, onMounted } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";

const store = useStore();
const router = useRouter();

// 与原 Login 页面保持一致的账号配置
const guestAccount = { pwd: "游客", userName: "游客" };
const regularAccount = { pwd: "qz123456789", userName: "13797066447" };

const form = reactive({
    pwd: "",
    userName: "",
});
const { userName, pwd } = toRefs(form);
const ref_form = ref(null);
const success = ref(false);

// 进入页面先清理登录状态
sessionStorage.clear();
store.commit(`app/${SET_TOKEN}`, "");
router.options.isAddDynamicMenuRoutes = false;

// 根据时间设置账号（与 Login.vue 完全一致）
const setAccountByTime = () => {
    const now = new Date();
    const currentHour = now.getHours();

    if (currentHour >= 18 && currentHour < 24) {
        form.pwd = guestAccount.pwd;
        form.userName = guestAccount.userName;
    } else {
        form.pwd = regularAccount.pwd;
        form.userName = regularAccount.userName;
    }
};

// 初始化账号
setAccountByTime();

// 直接调用登录接口，不做表单校验
const doAutoLogin = async () => {
        const res = await VE_API.system.login(form);
        if (res.code == 0) {
            const { token, userName, djiCloudToken } = res.data;
            store.commit(`app/${SET_TOKEN}`, token);
            store.commit(`app/djiCloudToken`, djiCloudToken);
            store.commit(`app/${SET_UNAME}`, userName);
            res.data["token"] = undefined;
            res.data["x-auth-token"] = undefined;
            res.data["pwd"] = undefined;
            store.commit(`app/${SET_USER_INFO}`, JSON.stringify(res.data));
            success.value = true;
            // 登录成功后直接跳转到预警首页（与 Login.vue 一致）
            router.push({ name: "warningHome" });
        }
};

onMounted(() => {
    doAutoLogin();
});
</script>

<style lang="scss" scoped>
.auto-login-page {
    height: 100vh;
    width: 100%;
    background: rgba(29, 29, 29, 0.24);
    ::v-deep(.el-loading-text) {
        font-size: 28px;
    }
    ::v-deep(.el-loading-spinner) {
        .circular {
            width: 60px;
            height: 60px;
            margin-top: -40px;
        }
    }
}
</style>


