import { type RouteRecordRaw, createRouter, createWebHashHistory, createWebHistory } from "vue-router"

const Layout = () => import("@/layout/index.vue")

/** 常驻路由 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/user",
    component: Layout,
    redirect: "/user",
    meta: {
      roles: ["change:user"]
    },
    children: [
      {
        path: "user",
        component: () => import("@/views/user/index.vue"),
        name: "User",
        meta: {
          title: "用户管理",
          elIcon: "User",
          roles: ["change:user"]
        }
      }
    ]
  },
  {
    path: "/roles",
    component: Layout,
    redirect: "/roles",
    meta: {
      roles: ["change:roles", "show:roles"]
    },
    children: [
      {
        path: "roles",
        component: () => import("@/views/roles/index.vue"),
        name: "Roles",
        meta: {
          title: "权限组管理",
          elIcon: "Lock",
          roles: ["change:roles", "show:roles"]
        }
      }
    ]
  },
  {
    path: "/store",
    component: Layout,
    redirect: "/store",
    meta: {
      roles: ["add:store", "show:store"]
    },
    children: [
      {
        path: "store",
        component: () => import("@/views/store/index.vue"),
        name: "Store",
        meta: {
          title: "商户管理",
          elIcon: "Shop",
          roles: ["add:store", "show:store"]
        }
      }
    ]
  },
  {
    path: "/commission",
    component: Layout,
    redirect: "/commission",
    meta: {
      roles: ["show:commission", "change:commission"]
    },
    children: [
      {
        path: "commission",
        component: () => import("@/views/commission/index.vue"),
        name: "Commission",
        meta: {
          title: "佣金管理",
          elIcon: "Money",
          roles: ["show:commission", "change:commission"]
        }
      }
    ]
  },
  {
    path: "/order",
    component: Layout,
    redirect: "/order/manager",
    name: "Order",
    meta: {
      title: "订单",
      elIcon: "Files",
      roles: ["show:order", "change:order"],
      alwaysShow: true
    },
    children: [
      {
        path: "create",
        component: () => import("@/views/order/create/index.vue"),
        name: "CreateOrder",
        meta: {
          title: "创建订单",
          roles: ["add:order"],
          elIcon: "FolderAdd"
        }
      },
      {
        path: "manager",
        component: () => import("@/views/order/manager/index.vue"),
        name: "ManagerOrder",
        meta: {
          title: "管理订单",
          roles: ["show:order", "change:order"],
          elIcon: "FolderChecked"
        }
      }
    ]
  },
  {
    path: "/permission",
    component: Layout,
    redirect: "/permission",
    meta: {
      roles: ["change:root_perm"] // 可以在根路由中设置角色
    },
    children: [
      {
        path: "permission",
        component: () => import("@/views/permission/index.vue"),
        name: "Permission",
        meta: {
          title: "权限管理",
          svgIcon: "lock",
          roles: ["change:root_perm"]
        }
      }
    ]
  },
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  }
]

const router = createRouter({
  history:
    import.meta.env.VITE_ROUTER_HISTORY === "hash"
      ? createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH)
      : createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: constantRoutes
})

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

export default router
