import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";
import { ElMessage } from "element-plus";
import PortalPage from "@/views/PortalPage/PortalPage";

const routes = [
  {
    path: "/",
    redirect: "/portal",
  },
  {
    path: "/portal",
    name: "Portal",
    // 落地页同步加载
    component: PortalPage,
  },
  {
    path: "/auth",
    name: "Auth",
    component: () => import("../views/AuthPage/AuthPage.vue"),
  },
  {
    path: "/task",
    name: "Task",
    component: () => import("../views/TaskPage/TaskPage.vue"),
  },
  {
    path: "/project",
    name: "Project",
    component: () => import("../views/ProjectPage/ProjectPage.vue"),
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("../views/HomePage/HomePage.vue"),
  },
  {
    path: "/message",
    name: "Message",
    component: () => import("../views/MessagePage/MessagePage.vue"),
  },
  {
    path: "/manage",
    name: "Manage",
    component: () => import("../views/ManagePage/ManagePage"),
  },
  {
    path: "/course",
    name: "Course",
    component: () => import("../views/CoursePage/CoursePage.vue"),
  },
  {
    path: "/course/:id",
    name: "CourseDetail",
    component: () => import("../views/CoursePage/CourseDetail.vue"),
  },

  {
    path: "/test",
    name: "Test",
    component: () => import("../views/TestPage/TestPage.vue"),
  },

  {
    path: "/problem/:problemId",
    name: "ProblemDetail",
    component: () => import("../views/ProblemPage/ProblemDetail.vue"),
  },
  {
    path: "/problem",
    name: "Problem",
    component: () => import("../views/ProblemPage/ProblemPage.vue"),
  },
  {
    path: "/problem/create",
    name: "ProblemUpload",
    component: () => import("../views/ProblemPage/ProblemUpload.vue"),
  },
  {
    path: "/exam/:examId",
    name: "ExamDetail",
    component: () => import("../views/ExamPage/ExamDetail.vue")
  },
  {
    path: "/exam/:examId/commit/:commitId",
    name: "CommitDetail-First",
    component: () => import("../views/ExamPage/ResultPage/CommitPage/CommitDetail")
  },
  {
    path: "/exam/:examId/commit/:commitId/record/:recordId",
    name: "RecordDetail-First",
    component: () => import("../views/ExamPage/ResultPage/CommitPage/RecordPage/RecordDetail")
  },
  {
    path: "/exam/:examId/result",
    name: "ResultPage",
    component: () => import("../views/ExamPage/ResultPage/ResultPage")
  },
  {
    path: "/exam/:examId/result/:resultId",
    name: "ResultDetail",
    component: () => import("../views/ExamPage/ResultPage/ResultDetail")
  },
  {
    path: "/exam/:examId/result/:resultId/commit/:commitId",
    name: "CommitDetail-Second",
    component: () => import("../views/ExamPage/ResultPage/CommitPage/CommitDetail")
  },
  {
    path: "/exam/:examId/result/:resultId/commit/:commitId/record/:recordId",
    name: "RecordDetail-Second",
    component: () => import("../views/ExamPage/ResultPage/CommitPage/RecordPage/RecordDetail")
  }
];

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

const unauthorizedRoutes = ["Portal", "Auth", "Course", "Test","Project"];

router.beforeEach((to, from, next) => {
  if (unauthorizedRoutes.indexOf(to.name) !== -1) {
    next();
  } else if (!store.state.user.token) {
    ElMessage({
      showClose: true,
      message: "请先登录",
      type: "warning",
      duration: 1000,
    });
    next({ name: "Auth", params: { login_to: to.path } });
  } else {
    if (to.name === "Manage" && !store.getters.isTeacher) {
      ElMessage({
        showClose: true,
        message: "当前用户无权访问教师页面",
        type: "error",
        duration: 1000,
      });
    } else if (to.name === "Task" && !store.getters.isStudent) {
      ElMessage({
        showClose: true,
        message: "当前用户无需访问任务页面",
        type: "warning",
        duration: 1000,
      });
    } else {
      next();
    }
  }
});

router.afterEach(() => {
  window.scrollTo(0, 0);
});
export default router;
