import { createRouter, createWebHashHistory } from "vue-router"
import layout from "@/layout/index" //layout直接引用 其他使用路由懒加载
import store from "@/store"
import { systemManager } from "@/router/modules/systemManager.js"
import { businessCenter } from "@/router/modules/businessCenter.js"
/**
 * 关于路由配置描述
 * 1.meta && meta.title && meta.icon  则在菜单栏显示
 * 2.如果存在children , 则以el-sub-menu子菜单显示
 * 否则不在menu菜单显示
 * 如果只展示单级别菜单  需要像developer这样配置
 */
/**
 * 私有路由表
 */
export const privateRoutes = [...systemManager, ...businessCenter,]
/**
 * 公开路由表
 */
export const publicRoutes = [
  {
    path: "/",
    // 注意：带有路径“/”的记录中的组件“默认”是一个不返回 Promise 的函数
    component: layout,
    redirect: "/login",
    children: [
      {
        path: "/dutyBook/home/workbench",
        name: "dutyBook/home/workbench",
        component: () => import("@/views/dutyBook/home/workbench.vue"),
        meta: {
          title: "消防管理大屏",
          icon: "icon_ywyy",
          iconH: "gongzuotai"
        }
      },
      {
        path: "/editPassword",
        name: "editPassword",
        component: () => import("@/views/systemConfig/editPassword/index"),
        meta: {
          title: "个人中心"
        }
      },
      {
        path: "/dict/detail",
        name: "dictDetail",
        component: () => import("@/views/systemConfig/dict/detail"),
        meta: {
          title: "字典数据"
        }
      },
      {
        path: "/user/allocationUser",
        name: "allocationUser",
        component: () => import("@/views/systemConfig/role-list/allocationUser"),
        meta: {
          title: "分配用户"
        }
      },
      {
        path: "/404",
        name: "404",
        component: () => import("@/views/error-page/404")
      },
      {
        path: "/401",
        name: "401",
        component: () => import("@/views/error-page/401")
      }
    ]
  },
  {
    path: "/login",
    component: () => import("@/views/login/index") //系统登录
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/error-page/404")
  }
]

const router = createRouter({
  routes: [...publicRoutes],
  history: createWebHashHistory(),
})
router.beforeEach((to, from, next) => {
  next()
})

// 初始化路由表
export function resetRouter() {
  if (store.getters?.routeReady && store.getters?.userInfo?.permission?.menus) {
    const menus = store.getters.userInfo.permission.menus
    menus.forEach(menu => {
      router.removeRoute(menu)
    })
  }
}

export default router
