import type { RouteRecordRaw } from "vue-router";
import { routerConfig } from "@/router/config";
import { registerNavigationGuard } from "@/router/guard";
import { createRouter } from "vue-router";
import { flatMultiLevelRoutes } from "./helper";

const Layouts = () => import("@/layouts/index.vue");

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true,
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue"),
      },
    ],
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true,
    },
    alias: "/:pathMatch(.*)*",
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true,
    },
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/pages/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          elIcon: "House",
          affix: true,
        },
      },
    ],
  },
  {
    path: "/miningModule",
    component: Layouts,
    redirect: "/miningModule",
    meta: {
      title: "采矿模块",
      elIcon: "Flag",
    },
    children: [
      {
        path: "miningWorkshop",
        name: "miningWorkshop",
        meta: {
          title: "采矿车间",
        },
        children: [
          {
            path: "miningWorkshopDailyreport",
            component: () => import("@/pages/demo/miningModule/miningWorkshopDailyreport.vue"),
            name: "miningWorkshopDailyreport",
            meta: {
              title: "日报",
            },
          },
          {
            path: "miningWorkshopMonthlyreport",
            component: () => import("@/pages/demo/miningModule/miningWorkshopMonthlyreport.vue"),
            name: "miningWorkshopMonthlyreport",
            meta: {
              title: "月报",
            },
          },
        ]
      },
      {
        path: "motorVehicleWorkshop",
        name: "motorVehicleWorkshop",
        meta: {
          title: "电机车车间",
        },
        children: [
          {
            path: "motorVehicleWorkshopDailyreport",
            component: () => import("@/pages/demo/miningModule/motorVehicleWorkshopDailyreport.vue"),
            name: "motorVehicleWorkshopDailyreport",
            meta: {
              title: "日报",
            },
          },
          {
            path: "motorVehicleWorkshopMonthlyreport",
            component: () => import("@/pages/demo/miningModule/motorVehicleWorkshopMonthlyreport.vue"),
            name: "motorVehicleWorkshopMonthlyreport",
            meta: {
              title: "月报",
            },
          },
        ]
      },
      {
        path: "roadMaintenanceTeam",
        name: "roadMaintenanceTeam",
        meta: {
          title: "养路队车间",
        },
        children: [
          {
            path: "roadMaintenanceTeamDailyreport",
            component: () => import("@/pages/demo/miningModule/roadMaintenanceTeamDailyreport.vue"),
            name: "roadMaintenanceTeamDailyreport",
            meta: {
              title: "日报",
            },
          },
          {
            path: "roadMaintenanceTeamMonthlyreport",
            component: () => import("@/pages/demo/miningModule/roadMaintenanceTeamMonthlyreport.vue"),
            name: "roadMaintenanceTeamMonthlyreport",
            meta: {
              title: "月报",
            },
          },
        ]
      },
      {
        path: "drainageWorkshop",
        name: "drainageWorkshop",
        meta: {
          title: "排水车间",
        },
        children: [
          {
            path: "drainageWorkshopDailyreport",
            component: () => import("@/pages/demo/miningModule/drainageWorkshopDailyreport.vue"),
            name: "drainageWorkshopDailyreport",
            meta: {
              title: "日报",
            },
          },
          {
            path: "drainageWorkshopMonthlyreport",
            component: () => import("@/pages/demo/miningModule/drainageWorkshopMonthlyreport.vue"),
            name: "drainageWorkshopMonthlyreport",
            meta: {
              title: "月报",
            },
          },
        ]
      },
    ],
  },
  {
    path: "/diceke",
    component: Layouts,
    redirect: "/diceke",
    meta: {
      title: "地测科",
      elIcon: "HelpFilled",
    },
    children: [
      {
        path: "shovel",
        component: () => import("@/pages/demo/diceke/shovel.vue"),
        name: "shovel",
        meta: {
          title: "电铲维护",
        },
      },
      {
        path: "plateArea",
        component: () => import("@/pages/demo/diceke/PlateArea.vue"),
        name: "plateArea",
        meta: {
          title: "平盘维护",
        },
      },
      {
        path: "dicekeMiningrecord",
        component: () => import("@/pages/demo/diceke/DicekeMiningrecord.vue"),
        name: "dicekeMiningrecord",
        meta: {
          title: "Mo品位报表",
        },
      },
      {
        path: "dicekePrecipitation",
        component: () => import("@/pages/demo/diceke/Precipitation.vue"),
        name: "dicekePrecipitation",
        meta: {
          title: "降水量报表",
        },
      },
      {
        path: "lossDepletionReport",
        component: () => import("@/pages/demo/diceke/LossDepletionReport.vue"),
        name: "",
        meta: {
          title: "损失和贫化报表",
        },
      },
      {
        path: "excavationQuantityReport",
        component: () =>
          import("@/pages/demo/diceke/ExcavationQuantityReport.vue"),
        name: "",
        meta: {
          title: "采剥量报表",
        },
      },
    ],
  },
  {
    path: "/drainageWorkshop",
    component: Layouts,
    redirect: "/drainageWorkshop",
    meta: {
      title: "防排水车间",
      elIcon: "Menu",
    },
    children: [
      {
        path: "pumpOperationLog",
        component: () =>
          import("@/pages/demo/drainageWorkshop/pumpOperationLog.vue"),
        name: "pumpOperationLog",
        meta: {
          title: "水处理量",
        },
      },
      {
        path: "displacement",
        component: () =>
          import("@/pages/demo/drainageWorkshop/displacement.vue"),
        name: "displacement",
        meta: {
          title: "排水量",
        },
      },
      {
        path: "pouringWaterVolume",
        component: () =>
          import("@/pages/demo/drainageWorkshop/pouringWaterVolume.vue"),
        name: "pouringWaterVolume",
        meta: {
          title: "倒水量",
        },
      },
      {
        path: "drainageReport",
        component: () =>
          import("@/pages/demo/drainageWorkshop/drainageReport.vue"),
        name: "drainageReport",
        meta: {
          title: "排水量报表",
        },
      },
    ],
  },
  {
    path: "/electricLocomotiveWorkshop",
    component: Layouts,
    redirect: "/electricLocomotiveWorkshop",
    meta: {
      title: "电机车车间",
      elIcon: "Link",
    },
    children: [
      {
        path: "singleMachineOutput",
        component: () =>
          import(
            "@/pages/demo/electricLocomotiveWorkshop/singleMachineOutput.vue"
          ),
        name: "singleMachineOutput",
        meta: {
          title: "单机产量",
        },
      },
      {
        path: "needMiningTime",
        component: () =>
          import("@/pages/demo/electricLocomotiveWorkshop/needMiningTime.vue"),
        name: "needMiningTime",
        meta: {
          title: "要矿时间",
        },
      },
      {
        path: "homeworkTimeLedger",
        component: () =>
          import(
            "@/pages/demo/electricLocomotiveWorkshop/homeworkTimeLedger.vue"
          ),
        name: "homeworkTimeLedger",
        meta: {
          title: "作业时间台账",
        },
      },
      {
        path: "mineralTransportationStatistics",
        component: () =>
          import(
            "@/pages/demo/electricLocomotiveWorkshop/mineralTransportationStatistics.vue"
          ),
        name: "mineralTransportationStatistics",
        meta: {
          title: "运矿统计表",
        },
      },
      {
        path: "productionComprehensive",
        component: () =>
          import(
            "@/pages/demo/electricLocomotiveWorkshop/productionComprehensive.vue"
          ),
        name: "productionComprehensive",
        meta: {
          title: "生产综合报表",
        },
      },
      {
        path: "transportationComprehensive",
        component: () =>
          import(
            "@/pages/demo/electricLocomotiveWorkshop/transportationComprehensive.vue"
          ),
        name: "transportationComprehensive",
        meta: {
          title: "运输综合报表",
        },
      },
      {
        path: "dataImport",
        component: () =>
          import("@/pages/demo/electricLocomotiveWorkshop/dataImport.vue"),
        name: "dataImport",
        meta: {
          title: "数据导入",
        },
      },
    ],
  },
  {
    path: "/miningArea",
    component: Layouts,
    redirect: "/miningArea",
    meta: {
      title: "采区",
      elIcon: "MessageBox",
    },
    children: [
      {
        path: "drillingRigMaintenance",
        component: () =>
          import("@/pages/demo/miningArea/drillingRigMaintenance.vue"),
        name: "drillingRigMaintenance",
        meta: {
          title: "钻机维护",
        },
      },
      {
        path: "maintenancePyrotechnicDevices",
        component: () =>
          import("@/pages/demo/miningArea/maintenancePyrotechnicDevices.vue"),
        name: "maintenancePyrotechnicDevices",
        meta: {
          title: "火工品维护",
        },
      },
      {
        path: "allPerforationExplosiveDaily",
        component: () =>
          import("@/pages/demo/miningArea/allPerforationExplosiveDaily.vue"),
        name: "allPerforationExplosiveDaily",
        meta: {
          title: "所有穿孔及炸药日报",
        },
      },
      {
        path: "statisticsMaterialsCracks",
        component: () =>
          import("@/pages/demo/miningArea/statisticsMaterialsCracks.vue"),
        name: "statisticsMaterialsCracks",
        meta: {
          title: "欲裂孔材料统计",
        },
      },
      {
        path: "drillingRigPerforation",
        component: () =>
          import("@/pages/demo/miningArea/drillingRigPerforation.vue"),
        name: "drillingRigPerforation",
        meta: {
          title: "钻机穿孔月报",
        },
      },
      {
        path: "recordPyrotechnicConsumption",
        component: () =>
          import("@/pages/demo/miningArea/recordPyrotechnicConsumption.vue"),
        name: "recordPyrotechnicConsumption",
        meta: {
          title: "火工品消耗台账",
        },
      },
      {
        path: "blastingPerforationOperationLedger",
        component: () =>
          import(
            "@/pages/demo/miningArea/blastingPerforationOperationLedger.vue"
          ),
        name: "blastingPerforationOperationLedger",
        meta: {
          title: "爆破作业穿孔作业台账",
        },
      },
    ],
  },
  {
    path: "/systemManagement",
    component: Layouts,
    redirect: "/systemManagement",
    meta: {
      title: "系统管理",
      elIcon: "Tools",
    },
    children: [
      {
        path: "userManagement",
        component: () =>
          import("@/pages/demo/systemManagement/userManagement.vue"),
        name: "userManagement",
        meta: {
          title: "用户管理",
        },
      },
      {
        path: "rightsManagement",
        component: () =>
          import("@/pages/demo/systemManagement/rightsManagement.vue"),
        name: "rightsManagement",
        meta: {
          title: "权限管理",
        },
      },
    ],
  },
  {
    path: "/rolesManage",
    component: Layouts,
    redirect: "/rolesManage",
    meta: {
      title: "角色管理",
      elIcon: "Avatar",
    },
    children: [
    ],
  },
];

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: "/permission",
    component: Layouts,
    redirect: "/permission/page-level",
    name: "Permission",
    meta: {
      title: "权限演示",
      elIcon: "Lock",
      // 可以在根路由中设置角色
      roles: ["admin", "editor"],
      alwaysShow: true,
    },
    children: [
      {
        path: "page-level",
        component: () => import("@/pages/demo/permission/page-level.vue"),
        name: "PermissionPageLevel",
        meta: {
          title: "页面级",
          // 或者在子路由中设置角色
          roles: ["admin"],
        },
      },
      {
        path: "button-level",
        component: () => import("@/pages/demo/permission/button-level.vue"),
        name: "PermissionButtonLevel",
        meta: {
          title: "按钮级",
          // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
          roles: undefined,
        },
      },
    ],
  },
];

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache
    ? flatMultiLevelRoutes(constantRoutes)
    : constantRoutes,
});

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route;
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name);
      }
    });
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload();
  }
}

// 注册路由导航守卫
registerNavigationGuard(router);
