import request from "umi-request";
import { computed, defineComponent, onBeforeMount, onMounted } from "vue";
import { RouterView } from "vue-router";
import TheError from "../components/The-error";
import { setUserInfo, storeData } from "../store";
import { getRandomKey, getToken, setBehaviorId, setToken } from "../utils";
import { getQuery } from "../utils/queryString";
import Notfind from "../view/notfind";
import { studentQueryUserInfo } from "./api";

export default defineComponent({
	name: "App",
	setup() {
		const query: any = getQuery();
		/** 获取用户信息 */
		const getUserInfo = async () => {
			// if (storeData.platformType === "WEB") {
			// 	return await employeeQueryUserInfo();
			// } else if (storeData.platformType === "TEACHER") {
			// 	return await teacherQueryUserInfo();
			// }
			return await studentQueryUserInfo();
		};
		const setUser = async () => {
			const res = await getUserInfo();
			const student = res?.data || {};
			setUserInfo(student);
			// console.log("🚀 ~ res:", student);
		};
		onBeforeMount(() => {
			if (query.Authorization) {
				setToken(query.Authorization);
			}
			if (!getToken()) {
				const token = /(dev\.kt\.colexiu\.com|localhost)/gi.test(location.origin)
					? "bearer 529d1359-20c6-4ea2-b2ba-2694cdeda8a4"
					: "bearer 3527f6b3-9953-41de-80f0-334dec910eac";
				setToken(token);
			}
			setUser();
			setBehaviorId(getRandomKey());
		});
		onMounted(() => {
			const _loading = document.getElementById("loading");
			_loading && document.body.removeChild(_loading);
		});

		const inited = computed(() => {
			return storeData.status === "login";
		});
		return () => (
			<>{storeData.status === "error" ? <TheError /> : inited.value ? <RouterView /> : null}</>
		);
	},
});
