import Vue from "vue";
import Router from "vue-router";

import NotFoundComponent from "./views/NotFoundComponent.vue";
import OnlineExamHome from "./features/OnlineExam/OnlineExamHome.vue";
import OnlineExamOverview from "./features/OnlineExam/OnlineExamOverview.vue";
import ExamingHome from "./features/OnlineExam/Examing/ExamingHome.vue";
import ExamingEnd from "./features/OnlineExam/Examing/ExamingEnd.vue";
import OfflineExamHome from "./features/OfflineExam/OfflineExamHome.vue";
import OnlinePracticeHome from "./features/OnlinePractice/OnlinePracticeHome.vue";
import OnlinePracticeRecordList from "./features/OnlinePractice/OnlinePracticeRecordList.vue";
import OnlinePracticeRecordDetail from "./features/OnlinePractice/OnlinePracticeRecordDetail.vue";
import Login from "./features/Login/Login.vue";
import StudentAccess from "./features/Login/StudentAccess.vue";
import SiteMessageHome from "./features/SiteMessage/SiteMessageHome.vue";
import SiteMessageDetail from "./features/SiteMessage/SiteMessageDetail.vue";
import Password from "./features/Password/Password.vue";
import CheckComputer from "./features/OnlineExam/CheckComputer.vue";

Vue.use(Router);
let router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      name: "Home",
      component: OnlineExamHome,
      props: {
        examType: "ONLINE", // 在线考试
      },
    },
    {
      path: "/login/:domain",
      name: "Login",
      component: Login,
    },
    {
      path: "/StudentAccess",
      name: "StudentAccess",
      component: StudentAccess,
    },
    {
      path: "/online-exam",
      name: "OnlineExamHome",
      component: OnlineExamHome,
      props: {
        examType: "ONLINE", // 在线考试
      },
    },
    {
      path: "/online-homework",
      name: "OnlineHomeworkHome",
      component: OnlineExamHome,
      props: {
        examType: "ONLINE_HOMEWORK", // 在线考试
      },
    },
    {
      path: "/online-exam/exam/:examId/overview",
      name: "OnlineExamOverview",
      component: OnlineExamOverview,
    },
    {
      path:
        "/online-exam/exam/:examId/examRecordData/:examRecordDataId/order/:order",
      name: "OnlineExamingHome",
      component: ExamingHome,
    },
    {
      path: "/online-exam/exam/:examId/examRecordData/:examRecordDataId/end",
      name: "ExamingEnd",
      component: ExamingEnd,
    },
    {
      path: "/online-practice",
      name: "OnlinePracticeHome",
      component: OnlinePracticeHome,
    },
    {
      path: "/online-practice/exam/:examId/list",
      name: "OnlinePracticeRecordList",
      component: OnlinePracticeRecordList,
    },
    {
      path: "/online-practice/exam/:examId/detail",
      name: "OnlinePracticeRecordDetail",
      component: OnlinePracticeRecordDetail,
    },
    {
      path: "/offline-exam",
      name: "OfflineExamHome",
      component: OfflineExamHome,
    },
    {
      path: "/site-message",
      name: "SiteMessageHome",
      component: SiteMessageHome,
    },
    {
      path: "/site-message/:id",
      name: "SiteMessageDetail",
      component: SiteMessageDetail,
    },
    {
      path: "/password",
      name: "Password",
      component: Password,
    },
    {
      path: "/check-computer",
      name: "CheckComputer",
      component: CheckComputer,
    },
    {
      path: "*",
      component: NotFoundComponent,
    },
  ],
});

router.beforeEach((to, from, next) => {
  const loginPath = "/login/" + localStorage.getItem("domain");

  if (to.path) {
    window._hmt.push(["_trackPageview", "/oe" + to.fullPath]);
  }

  if (to.path.match(/^\/login\/.+$/) || to.path.match(/^\/StudentAccess$/)) {
    next();
  } else {
    if (!localStorage.getItem("domain")) {
      alert("地址出错，找不到机构！请关闭应用后重试！");
    }
    if (!window.sessionStorage.getItem("token")) {
      next({ path: loginPath });
    } else {
      next();
    }
  }
});

export default router;
