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, setBehaviorId, setToken } from "../utils";
import { getQuery } from "../utils/queryString";
import Notfind from "../view/notfind";
import { employeeQueryUserInfo, studentQueryUserInfo, teacherQueryUserInfo } from "./api";

export default defineComponent({
	name: "App",
	setup() {
		const query: any = getQuery()
		/** 获取用户信息 */
		const getUserInfo = async () => {
			// const a = await request.get(`/student/queryUserInfo`)
			// console.log(a)
			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);
			}
			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}</>;
	},
});
