import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/home/Index.vue";

Vue.use(VueRouter);

const createRouter = () =>
  new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: [
      {
        path: "/",
        redirect: "/login",
      },
      {
        path: "/home",
        redirect: "/home/cockpitManage",
        name: "home",
        component: Home,
        children: [
          // 驾驶舱
          {
            id: 1,
            path: "cockpitManage",
            name: "驾驶舱",
            iconName: "gps",
            component: () =>
              import(
                /*webpackChunkName:'home-cockpitManage'*/ "../views/admin/cockpitManage/Index.vue"
              ),
          },
          {
            // 电子地图
            id: 11,
            path: "/home/cockpitManage/electronic-map",
            name: "电子地图",
            component: () =>
              import(
                /*webpackChunkName:'home-cockpitManage'*/ "../views/admin/cockpitManage/electronicMap.vue"
              ),
          },
        ],
      },
      {
        path: "/login",
        name: "login",
        component: () => import("../views/login/Index.vue"),
      },
      // 404 Page Not Found
      // {
      //   path: "*",
      //   name: "NotFound",
      //   component: () => import("../views/error/404.vue"),
      // },
    ],
  });

const router = createRouter();

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 重置路由
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}
router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    return next("/login");
  } else {
    next();
  }
});

export default router;
