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

import Home from "./views/Home.vue";
import HomePage from "./views/HomePage.vue";
import NotFound from "./views/404.vue";

import login from "./modules/login/router";
// module-example
import base from "./modules/base/router";
import exam from "./modules/exam/router";
import print from "./modules/print/router";
import stmms from "./modules/stmms/router";
import analysis from "./modules/analysis/router";
import mark from "./modules/mark/router";
// card part
import card from "./modules/card/router";
// admin
import admin from "./modules/admin/router";
// tool
import tool from "../tools/router";

// ignore NavigationDuplicated. https://github.com/vuejs/vue-router/issues/2881
// const originalPush = Router.prototype.push;
// Router.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject)
//     return originalPush.call(this, location, onResolve, onReject);
//   try {
//     return originalPush.call(this, location).catch((err) => err);
//   } catch (error) {
//     console.log(error);
//   }
// };
// end ignore

Vue.use(Router);

/*
history模式 nginx配置
location / {
  try_files $uri $uri/ /index.html;
}
*/

let router = new Router({
  mode: "history",
  routes: [
    {
      path: "/",
      name: "Index",
      redirect: { name: "Login" },
    },
    {
      path: "/home/:nextRouter?",
      name: "Home",
      component: Home,
      children: [
        {
          path: "/home-page",
          name: "HomePage",
          component: HomePage,
        },
        ...base,
        ...exam,
        ...print,
        ...stmms,
        ...analysis,
        ...mark,
      ],
    },
    { ...login },
    { ...admin },
    ...card,
    ...tool,
    {
      path: "*",
      name: "404",
      component: NotFound,
    },
    // [lazy-loaded] route level code-splitting
    // {
    //   path: "/about",
    //   name: "about",
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () =>
    //     import(/* webpackChunkName: "about" */ "./views/About.vue")
    // }
  ],
});

// route interceptor
router.beforeEach((to, from, next) => {
  const privilegeMap = Vue.ls.get("privilegeMap", {});
  Vue.ls.set(
    "privilegeId",
    privilegeMap[to.name] ? privilegeMap[to.name][0] : ""
  );

  const token = Vue.ls.get("token");
  if (to.meta.noRequire) {
    next();
    return;
  }

  if (!token) {
    // 登录失效的处理
    const returnUrl = Vue.ls.get("returnUrl");
    if (returnUrl) {
      window.location.href = returnUrl;
      return;
    }

    Vue.ls.clear();
    const paramDomainCode = window.sessionStorage.getItem("paramDomainCode");
    const routeDomainCode = window.sessionStorage.getItem("routeDomainCode");
    if (paramDomainCode) {
      next({ name: "Login", query: { code: paramDomainCode } });
    } else if (routeDomainCode) {
      next({ name: "Login", params: { code: routeDomainCode } });
    } else {
      next({ name: "Login" });
    }
    return;
  }

  next();
});

export default router;
