import Vue from 'vue';
import VueRouter from 'vue-router';
import HomeView from '../views/HomeView.vue';
import Login from '../components/Login.vue';
import Register from '../components/Register.vue';
import store from '../store';  // 引入 Vuex store

Vue.use(VueRouter);

const routes = [
  {
    path:'/student/message',
    name:'Message',
    component: () => import(/* webpackChunkName: "about" */ '../views/Student/Message.vue'),
    meta: { requiresAuth: true }
  },
  {
    path:'/teacher/teacherExam',
    name:'TeacherExam',
    component: () => import(/* webpackChunkName: "about" */ '../views/Teacher/TeacherExam.vue'),
    meta: { requiresAuth: true }
  },
  {
    path:'/teacher/score',
    name:'Score',
    component: () => import(/* webpackChunkName: "about" */ '../views/Teacher/Score.vue'),
    meta: { requiresAuth: true }
  },
  {
    path:'/teacher/correctExamPaper',
    name:'CorrectExamPaper',
    component: () => import(/* webpackChunkName: "about" */ '../views/Teacher/CorrectExamPaper.vue'),
    meta: { requiresAuth: true }
  },
  {
    path:'/teacher/viewExamPaper',
    name:'ViewExamPaper',
    component: () => import(/* webpackChunkName: "about" */ '../views/Teacher/ViewExamPaper.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/homePage',
    name: 'HomePage',
    component: () => import('../views/Teacher/HomePage.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/correct',
    name: 'Correct',
    component: () => import('../views/Teacher/Correct.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/profile',
    name: 'Profile',
    component: () => import('../views/user/Profile.vue'),
    meta: { requiresAuth: true },
  },
  {
    path:'/student/testPaperDetail',
    name:'TestPaperDetail',
    component: () => import(/* webpackChunkName: "about" */ '../views/Student/TestPaperDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path:'/student/scoreList',
    name:'ScoreList',
    component: () => import(/* webpackChunkName: "about" */ '../views/Student/ScoreList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/student/examPaper',
    name: 'ExamPaper',
    component: () => import(/* webpackChunkName: "about" */ '../views/Student/ExamPaper.vue'),
    meta: { requiresAuth: true }
  },
  {
    path:'/student/exam',
    component: () => import(/* webpackChunkName: "about" */ '../views/Student/Exam.vue'),
    meta: { requiresAuth: true}
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/homeView',
    name: 'HomeView',
    component: () => import(/* webpackChunkName: "about" */ '../views/HomeView.vue')
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const userId = to.query.userId;
  // 判断该路由是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 如果未登录，跳转到登录页
    if (!store.state.tokens[userId]) {  // 使用 Vuex 中的 token 判断登录状态
      next({ name: 'Login' });
    } else {
      next(); // 放行
    }
  } else {
    next(); // 不需要登录，直接访问
  }
});

export default router;
