import { createRouter, createWebHistory, createWebHashHistory  } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'

import {getUserInfo} from '@/api/login.ts'

// 示例路由配置
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: '',
    redirect:'/main'
    // component: () => import('../views/login/login.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: Layout,
    children: [
      {
        path: '',
        name: 'studentEntry',    //学生入口
        component: () => import('@/views/studentEntry/StudentEntry.vue')
      },
      {
        path: 'questionnaireTaskListPage',
        name: 'questionnaireTaskListPage',    //问卷任务页
        component: () => import('@/views/questionnaireTaskListPage/QuestionnaireTaskListPage.vue')
      },
      {
        path: 'selfQuestionnaireProcess',
        name: 'selfQuestionnaireProcess',    //学生个人问卷列表进度页
        component: () => import('@/views/selfQuestionnaireProcess/SelfQuestionnaireProcess.vue')
      },
      {
        path: 'questionnaireCommit',
        name: 'questionnaireCommit',    //学生个人问卷填写提交页
        component: () => import('@/views/questionnaireCommit/QuestionnaireCommit.vue')
      },
    ]
  },

  {
    path: '/app',
    name: 'app',
    component: Layout,
    children: [
      {
        path: '',
        name: 'appStudentEntry',    //学生入口
        component: () => import('@/views/appView/StudentEntry.vue')
      },
      {
        path: 'questionnaireTaskListPage',
        name: 'appQuestionnaireTaskListPage',    //问卷任务页
        component: () => import('@/views/appView/QuestionnaireTaskListPage.vue')
      },


      {
        path: 'questionnaireElectiveSubject',
        name: 'appQuestionnaireElectiveSubject',    //问卷选科页
        component: () => import('@/views/appView/QuestionnaireElectiveSubject.vue')
      },


      {
        path: 'selfQuestionnaireProcess',
        name: 'appSelfQuestionnaireProcess',    //学生个人问卷列表进度页
        component: () => import('@/views/appView/SelfQuestionnaireProcess.vue')
      },


      {
        path: 'questionnaireCommit',
        name: 'appQuestionnaireCommit',    //学生个人问卷填写提交页
        component: () => import('@/views/appView/QuestionnaireCommit.vue')
      },
    ]
  },


  {
    path: '/:pathMatch(.*)*', // 捕获所有未匹配路径
    name: 'NotFound',
    component: () => import('../views/login/login.vue') // 未匹配到路由跳转到登录页面
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 添加一个路由守卫
router.beforeEach(async(to,from,next)=>{
  console.log('当前to的路由',to)
  console.log('当前from的路由',from)
  if (to.path === '/') {
    // const token = localStorage.getItem('token')
    // 查询用户信息
    const token = to?.query?.token
    const userType = to?.query?.loginRole   // 'student' : 'teacher'
    
    if(token){
      localStorage.setItem('token',token)
      localStorage.setItem('loginRole',userType)
      
      let userInfoRes = await  getUserInfo()
      console.log("获取用户信息返回",userInfoRes)
      if (userInfoRes?.code === 200) {
        // 存储用户信息到 localStorage 或 Pinia
        localStorage.setItem('userInfo', JSON.stringify(userInfoRes.data))
        next('/main')
        return
      }
    }
  }
  next()
})


export default router