<template>
    <el-main></el-main>
</template>
<script setup lang="ts">
//useRouter
import { useRoute } from "vue-router";
import { useMsg } from "@/hooks/useMsg";
//登录请求接口
import { loginApi } from "@/api/modules/login";
//全局状态

import { getGlobalStatusApi } from "@/api/modules/global";
//全局仓库
import { getStockApi } from "@/api/modules/global";
//用户信息存储
import { useUserStore } from "@/stores/modules/user";
import { usePathUrl } from "@/hooks/usePathUrl";
//全局状态
import { useStatusStore } from "@/stores/modules/status";
const userStore = useUserStore();
const statusStore = useStatusStore();
// 路由
const $route = useRoute();
const $router = useRouter();

const getGlobalStatus = async () => {
    const result = await getGlobalStatusApi();
    if (result.status === 200) {
        const { data } = result;
        //设置全局状态
        statusStore.setGlobalStatus(data);
    }
};
const getStock = async () => {
    const result = await getStockApi();
    if (result.status === 200) {
        const { data } = result;
        data.forEach((item: any) => {
            item.code = item.code + "_" + "$" + item.erpOrgCode;
        });
        //设置全局仓库
        userStore.setWarehouse(data);
    }
};
const getUcOnline = (phpToken: any, refreshToken: any) => {
    let httpUrl = import.meta.env.VITE_SINGLE_URL + "uc/online";
    fetch(httpUrl, {
        method: "POST",
        credentials: "include",
        keepalive: true,
        headers: {
            Authorization: phpToken,
            "Refresh-Authorization": refreshToken
        }
    });
};

const getUcOffline = (redirectUrl: any) => {
    let httpUrl = import.meta.env.VITE_SINGLE_URL + "uc/offline";
    fetch(httpUrl, {
        method: "POST",
        credentials: "include",
        keepalive: true,
        headers: {
            Authorization: userStore.phpToken,
            "Refresh-Authorization": userStore.refreshToken
        }
    })
        .then((res: any) => {
            if (res.status === 200) {
                useMsg("success", "退出登录成功 ！");
                setTimeout(() => {
                    location.href = redirectUrl;
                }, 300);
            }
        })
        .catch(() => {
            useMsg("error", "退出登录失败,请稍后再试 ！");
        });
};

// 设置用户数据
const setUserData = (data: any) => {
    // 组装token
    let newUserToken = data.tokenInfo.tokenType + " " + data.tokenInfo.token;
    let RefreshToken = data.tokenInfo.tokenType + " " + data.tokenInfo.refreshToken;
    let phpToken = data.tokenInfo.tokenType + " " + data.tokenInfo.phpToken;
    // 设置token
    userStore.setToken(newUserToken);
    userStore.setRefreshToken(RefreshToken);
    userStore.setPhpToken(phpToken);
    // 设置用户信息
    userStore.setUserInfo(data.userInfo);
    // 设置组织id
    userStore.setOrgId(data.userInfo.orgId);
    getUcOnline(phpToken, RefreshToken);
    getGlobalStatus();
    getStock();
    //跳转
    setTimeout(() => {
        $router.push({ path: "/" });
    }, 500);
};

// 登录
const loginHttp = async (code: any) => {
    const result: Record<string, any> = await loginApi({ code: code });
    if (result.status === 200) {
        setUserData(result.data);
    } else {
        getUcOffline(usePathUrl());
    }
};

// 登录前的判断
const login = () => {
    const { code } = $route.query;
    // 没有code直接跳转到登录页
    if (!code) {
        getUcOffline(usePathUrl());
        return;
    }
    // 有code就登录请求
    loginHttp(code);
};
login();
</script>
<style lang="scss">
.el-main {
    height: 100vh;
}
</style>
