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: "/login",
    name: "Auth",
    component: () => import("../views/AuthPage/AuthPage.vue"),
  },
  {
    path: "/completephone",
    name: "CompletePhone",
    component: () => import("../views/AuthPage/CompletePhonePage"),
  },
  {
    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/CourseDetailPage/CourseDetailPage.vue"),
  },

  {
    path: "/test",
    name: "Test",
    component: () => import("../views/TaskPage/TaskPage.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/CoderPage/ExamDetail.vue")
  },
  {
    path: "/exam/:examId/commit/:commitId",
    name: "CommitDetail-First",
    component: () => import("../views/CoderPage/ResultPage/CommitPage/CommitDetail")
  },
  {
    path: "/exam/:examId/commit/:commitId/record/:recordId",
    name: "RecordDetail-First",
    component: () => import("../views/CoderPage/ResultPage/CommitPage/RecordPage/RecordDetail")
  },
  {
    path: "/exam/:examId/result",
    name: "ResultPage",
    component: () => import("../views/CoderPage/ResultPage/ResultPage")
  },
  {
    path: "/exam/:examId/result/:resultId",
    name: "ResultDetail",
    component: () => import("../views/CoderPage/ResultPage/ResultDetail")
  },
  {
    path: "/exam/:examId/result/:resultId/commit/:commitId",
    name: "CommitDetail-Second",
    component: () => import("../views/CoderPage/ResultPage/CommitPage/CommitDetail")
  },
  {
    path: "/exam/:examId/result/:resultId/commit/:commitId/record/:recordId",
    name: "RecordDetail-Second",
    component: () => import("../views/CoderPage/ResultPage/CommitPage/RecordPage/RecordDetail")
  },
  {
    path: "/evalExam/:examId/edit",
    name: "edit-exams",
    component: () =>import("../views/EvalPage/EditExam.vue")
  },
  {
    path: "/evalExam/:examId/teacher/details",
    name: "teacher-examDetails",
    component: () =>import("../views/EvalPage/teacherExamDetails.vue")
  },
  {
    path: "/evalExam/:examId/student/details",
    name: "student-examDetails",
    component: () =>import("../views/EvalPage/studentExamDetails.vue")
  },
  {
    path: "/courseWare/:courseWareId/details",
    name: "courseWareDetails",
    component: ()=>import("../views/CourseWarePage/CourseWarePage")
  },
  {
    path: "/root",
    name: "Root",
    component: ()=>import("../views/RootPage/RootPage.vue")
  },
  {
    path: "/communication",
    name: "Communication",
    component: ()=>import("../views/CommunicationPage/CommunicationPage.vue"),
    redirect: "/communication/explore",
    children:[
      {
        path:"explore",
        name: "CommunicationExplore",
        component: ()=>import("../views/CommunicationPage/ExplorePage.vue")
      },
      {
        path:"publish",
        name: "CommunicationPublish",
        component: ()=>import("../views/CommunicationPage/PublishPage.vue")
      },
      {
        path:"manage",
        name: "CommunicationManage",
        component: ()=>import("../views/CommunicationPage/ManagePage.vue")
      },
      {
        path:"star",
        name: "CommunicationStar",
        component: ()=>import("../views/CommunicationPage/StarPage.vue")
      },
      {
        path:"communicationDetail/:communicationId",
        name: "CommunicationDetail",
        component: ()=>import("../views/CommunicationPage/CommunicationDetail.vue")
      }
    ],
  }
];

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

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

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;
