/**
 * 路由配置
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/auth/Login.vue'),
    meta: {
      title: '登录',
      hidden: true
    }
  },
  {
    path: '/',
    component: () => import('@/layouts/MainLayout.vue'),
    redirect: '/assets/list',
    children: [
      // 后台管理
      {
        path: '/admin',
        name: 'Admin',
        redirect: '/admin/users',
        meta: {
          title: '后台管理',
          icon: 'Setting'
        },
        children: [
          {
            path: 'users',
            name: 'UserManagement',
            component: () => import('@/views/admin/UserManagement.vue'),
            meta: {
              title: '用户管理',
              icon: 'User'
            }
          },
          {
            path: 'roles',
            name: 'RoleManagement',
            component: () => import('@/views/admin/RoleManagement.vue'),
            meta: {
              title: '角色权限',
              icon: 'Lock'
            }
          },
          {
            path: 'device-types',
            name: 'DeviceTypeManagement',
            component: () => import('@/views/admin/DeviceTypeManagement.vue'),
            meta: {
              title: '设备类型',
              icon: 'Grid'
            }
          },
          {
            path: 'organizations',
            name: 'OrganizationManagement',
            component: () => import('@/views/admin/OrganizationManagement.vue'),
            meta: {
              title: '机构网点',
              icon: 'OfficeBuilding'
            }
          }
        ]
      },
      // 资产管理
      {
        path: '/assets',
        name: 'Assets',
        redirect: '/assets/list',
        meta: {
          title: '资产管理',
          icon: 'Box'
        },
        children: [
          {
            path: 'list',
            name: 'AssetList',
            component: () => import('@/views/assets/AssetList.vue'),
            meta: {
              title: '资产列表',
              icon: 'List'
            }
          },
          {
            path: 'create',
            name: 'AssetCreate',
            component: () => import('@/views/assets/AssetCreate.vue'),
            meta: {
              title: '资产入库',
              icon: 'Plus'
            }
          },
          {
            path: 'allocation',
            name: 'AssetAllocation',
            component: () => import('@/views/assets/AssetAllocation.vue'),
            meta: {
              title: '资产分配',
              icon: 'Share'
            }
          },
          {
            path: 'scan',
            name: 'AssetScan',
            component: () => import('@/views/assets/AssetScan.vue'),
            meta: {
              title: '扫码查询',
              icon: 'Camera'
            }
          },
          {
            path: 'maintenance',
            name: 'MaintenanceManagement',
            component: () => import('@/views/assets/MaintenanceManagement.vue'),
            meta: {
              title: '维修管理',
              icon: 'Tools'
            }
          },
          {
            path: 'statistics',
            name: 'StatisticsDashboard',
            component: () => import('@/views/assets/StatisticsDashboard.vue'),
            meta: {
              title: '统计报表',
              icon: 'DataAnalysis'
            }
          }
        ]
      },
      // 调拨管理
      {
        path: '/allocation',
        name: 'Allocation',
        redirect: '/allocation/transfers',
        meta: {
          title: '调拨管理',
          icon: 'Sort'
        },
        children: [
          {
            path: 'transfers',
            name: 'TransferList',
            component: () => import('@/views/allocation/TransferList.vue'),
            meta: {
              title: '资产调拨',
              icon: 'ArrowRight'
            }
          },
          {
            path: 'recoveries',
            name: 'RecoveryList',
            component: () => import('@/views/allocation/RecoveryList.vue'),
            meta: {
              title: '资产回收',
              icon: 'ArrowLeft'
            }
          }
        ]
      },
      // 系统管理
      {
        path: '/system',
        name: 'System',
        redirect: '/system/config',
        meta: {
          title: '系统管理',
          icon: 'Setting'
        },
        children: [
          {
            path: 'config',
            name: 'SystemConfig',
            component: () => import('@/views/system/SystemConfig.vue'),
            meta: {
              title: '系统配置',
              icon: 'Tools'
            }
          },
          {
            path: 'logs',
            name: 'OperationLog',
            component: () => import('@/views/system/OperationLog.vue'),
            meta: {
              title: '操作日志',
              icon: 'Document'
            }
          },
          {
            path: 'notification',
            name: 'NotificationCenter',
            component: () => import('@/views/system/NotificationCenter.vue'),
            meta: {
              title: '消息通知',
              icon: 'Bell'
            }
          }
        ]
      },
      // 示例页面
      {
        path: '/examples',
        name: 'Examples',
        redirect: '/examples/charts',
        meta: {
          title: '示例',
          icon: 'Document'
        },
        children: [
          {
            path: 'charts',
            name: 'ChartsExample',
            component: () => import('@/views/examples/ChartsExample.vue'),
            meta: {
              title: '图表示例',
              icon: 'PieChart'
            }
          }
        ]
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '404',
      hidden: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior() {
    return { top: 0 }
  }
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 设置页面标题
  document.title = `${to.meta.title || '资产管理系统'} - 资产管理系统`

  // 不需要登录的页面
  const whiteList = ['/login']

  if (userStore.isLoggedIn) {
    // 已登录
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    // 未登录,直接跳转到登录页,不显示任何提示
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next({ path: '/login', query: { redirect: to.fullPath } })
    }
  }
})

router.afterEach(() => {
  // 路由跳转后的处理
})

export default router
