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: [
          // 驾驶舱
          {
            path: "cockpitManage",
            name: "cockpitManage",

            component: () =>
              import(
                /*webpackChunkName:'home-cockpitManage'*/ "../views/admin/cockpitManage/Index.vue"
              ),
            children: [
              // 电子地图
              {
                path: "electronic-map",
                name: "electronicMap",
                component: () =>
                  import(
                    /*webpackChunkName:'home-cockpitManage'*/ "../views/admin/cockpitManage/electronicMap.vue"
                  ),
              },
            ],
          },
          // 电子地图
          {
            path: "electronic-map",
            name: "electronicMap",
            component: () =>
              import(
                /*webpackChunkName:'electronic-map'*/ "../views/admin/cockpitManage/electronicMap.vue"
              ),
          },
          // 数据操作
          {
            path: "dataAdministration",
            name: "dataAdministration",
            component: () =>
              import(
                /*webpackChunkName:'dataAdministration'*/ "../views/dataAdministration/index.vue"
              ),
          },
          // 企业信息
          {
            path: "enterprise",
            name: "enterprise",
            component: () =>
              import(
                /*webpackChunkName:'enterprise'*/ "../views/ledger/enterprise.vue"
              ),
          },
          // 风场信息
          {
            path: "windsite",
            name: "windsite",
            component: () =>
              import(
                /*webpackChunkName:'windsite'*/ "../views/ledger/windsite.vue"
              ),
          },
          // 风机信息
          {
            path: "draught",
            name: "draught",
            component: () =>
              import(
                /*webpackChunkName:'draught'*/ "../views/ledger/draught.vue"
              ),
          },
          // 机型信息
          {
            path: "milltype",
            name: "milltype",
            component: () =>
              import(
                /*webpackChunkName:'milltype'*/ "../views/ledger/milltype.vue"
              ),
          },
          // 测风塔信息
          {
            path: "anemometer",
            name: "anemometer",
            component: () =>
              import(
                /*webpackChunkName:'anemometer'*/ "../views/ledger/anemometer.vue"
              ),
          },
        ],
      },
      {
        path: "/login",
        name: "login",
        component: () => import("../views/login/Index.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
  console.log(router.getRoutes(), "重置后的路由");
}

export default router;
