<script setup lang="ts">

import {useRoute} from "vue-router";
import {onMounted} from "vue";
import {getCurrentUser, tpLoginApi} from "@/api/userApi.ts";
import router from '../router/index';
import {ElLoading, ElNotification} from "element-plus";
import {useUserInfo} from "@/stores/userInfo.ts";

const route = useRoute()
const userInfo = useUserInfo();
const tpToken = route.query.tpToken;
const targetPath = route.query.targetPath;

onMounted(() => {

  const loading = ElLoading.service({
    lock: true,
    text: '登录中...',
    background: 'rgba(0, 0, 0, 0.7)',
  })

  console.log(tpToken)
  if (tpToken == null) {
    router.push("/")
  }
  tpLoginApi({
    tpToken: tpToken + ""
  }).then(res => {
    console.log(res)

    if (res.data.code === 0) {

      userInfo.setToken(res.data.data.token + "", 'auth');
      const userType = res.data.data.userType;
      getCurrentUser().then((res) => {
        userInfo.dataFill(Object.assign({userType: userType}, res.data.data));

        if (targetPath) {
          router.push({path:targetPath+""})
        }else {
          if (userType == 0) router.push({path: '/train'});
          else if (userType == 1) router.push({path: '/TaskMng'});
          else {
            ElNotification({
              message: '无效用户',
              type: 'info',
            });
          }
        }

      }).finally(() => {
        loading.close()
      });


    }


  })
})

</script>

<template>
  <!--  登录中...-->
</template>

<style scoped>

</style>