import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";
export const Layout = () => import("@/layouts/index.vue");
/**
 * 静态路由
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    meta: { hidden: true },
    component: Layout,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: { hidden: true },
    component: () => import("@/views/module_system/auth/index.vue"),
  },
  {
    path: "/401",
    name: "401",
    meta: { hidden: true, title: "401" },
    component: () => import("@/views/error/401.vue"),
  },
  {
    path: "/404",
    name: "404",
    meta: { hidden: true, title: "404" },
    component: () => import("@/views/error/404.vue"),
  },
  {
    path: "/500",
    name: "500",
    meta: { hidden: true, title: "500" },
    component: () => import("@/views/error/500.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/error/404.vue"),
    meta: { hidden: true, title: "404" },
  },
  {
    path: "/",
    name: "/",
    meta: { hidden: true },
    component: () => import("@/views/web/index.vue"),
    children: [
      { path: "", component: () => import("@/views/web/overview/index.vue") },
      {
        path: "/overview",
        component: () => import("@/views/web/overview/index.vue"),
        name: "Overview",
        meta: { title: "总览"},
      },
      {
        path: '/detail',
        name: 'Detail',
        meta: { title: '行车详情' },
        component: () => import("@/views/web/detail/index.vue"),
        children: [
          { path: "", redirect: "/detail/realtimeData" },
          {
            path: '/detail/realtimeData',
            name: 'RealtimeData',
            meta: { title: '实时数据' },
            component: () => import("@/views/web/detail/realtimeData.vue")
          },
          {
            path: '/detail/historyData',
            name: 'HistoryData',
            meta: { title: '历史数据' },
            component: () => import("@/views/web/detail/historyData.vue")
          },
          {
            path: '/detail/operationRecord',
            name: 'OperationRecord',
            meta: { title: '操作记录' },
            component: () => import("@/views/web/detail/operationRecord.vue")
          },
          {
            path: '/detail/realtimeAlarm',
            name: 'RealtimeAlarm',
            meta: { title: '实时报警' },
            component: () => import("@/views/web/detail/realtimeAlarm.vue")
          },
          {
            path: '/detail/historyAlarm',
            name: 'HistoryAlarm',
            meta: { title: '历史报警' },
            component: () => import("@/views/web/detail/historyAlarm.vue")
          },
          {
            path: '/detail/realtimeCurve',
            name: 'RealtimeCurve',
            meta: { title: '实时曲线' },
            component: () => import("@/views/web/detail/realtimeCurve.vue")
          },
          {
            path: '/detail/historyCurve',
            name: 'HistoryCurve',
            meta: { title: '历史曲线' },
            component: () => import("@/views/web/detail/historyCurve.vue")
          },
        ]
      }
    ],
  },
  // 以下内容必须放在后面
  {
    path: "/admin",
    name: "Admin",
    redirect: "/home",
    component: Layout,
    children: [
      {
        path: "/home",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Home",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "profile",
        name: "Profile",
        meta: { title: "个人中心", icon: "user", hidden: true },
        component: () => import("@/views/current/profile.vue"),
      },
      // 应用内部打开页面
      // {
      //   path: "internal-app/:appId",
      //   name: "InternalApp",
      //   meta: { title: "内部应用", icon: "Monitor", hidden: true, keepAlive: false },
      //   component: () => import("@/views/module_application/myapp/components/InternalApp.vue"),
      // },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
// 为了捕获并处理全局错误，在注册路由时添加错误处理
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
