import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: () => import('@/layout/index.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '首页', icon: 'HomeFilled' }
      },
      {
        path: 'school',
        name: 'School',
        component: () => import('@/views/school/index.vue'),
        meta: { title: '学校管理', icon: 'School' }
      },
      {
        path: 'member',
        name: 'Member',
        component: () => import('@/views/member/index.vue'),
        meta: { title: '会员管理', icon: 'User' }
      },
      {
        path: 'student',
        name: 'Student',
        component: () => import('@/views/student/index.vue'),
        meta: { title: '学生管理', icon: 'UserFilled' }
      },
      {
        path: 'application',
        name: 'Application',
        component: () => import('@/views/application/index.vue'),
        meta: { title: '应用管理', icon: 'Grid' }
      },
      {
        path: 'base/grade',
        name: 'Grade',
        component: () => import('@/views/base/grade/index.vue'),
        meta: { title: '年级管理', icon: 'Collection' }
      },
      {
        path: 'base/class',
        name: 'Class',
        component: () => import('@/views/base/class/index.vue'),
        meta: { title: '班级管理', icon: 'Files' }
      },
      {
        path: 'base/subject',
        name: 'Subject',
        component: () => import('@/views/base/subject/index.vue'),
        meta: { title: '学科管理', icon: 'Reading' }
      },
      {
        path: 'base/region',
        name: 'Region',
        component: () => import('@/views/base/region/index.vue'),
        meta: { title: '区域管理', icon: 'Location' }
      },
      // 系统管理路由
      {
        path: 'system/user',
        name: 'SystemUser',
        component: () => import('@/views/system/user/index.vue'),
        meta: { title: '用户管理', icon: 'User' }
      },
      {
        path: 'system/role',
        name: 'SystemRole',
        component: () => import('@/views/system/role/index.vue'),
        meta: { title: '角色管理', icon: 'UserFilled' }
      },
      {
        path: 'system/menu',
        name: 'SystemMenu',
        component: () => import('@/views/system/menu/index.vue'),
        meta: { title: '菜单管理', icon: 'Menu' }
      },
      {
        path: 'system/dept',
        name: 'SystemDept',
        component: () => import('@/views/system/dept/index.vue'),
        meta: { title: '部门管理', icon: 'OfficeBuilding' }
      },
      {
        path: 'system/post',
        name: 'SystemPost',
        component: () => import('@/views/system/post/index.vue'),
        meta: { title: '岗位管理', icon: 'Briefcase' }
      },
      {
        path: 'system/dict',
        name: 'SystemDict',
        component: () => import('@/views/system/dict/index.vue'),
        meta: { title: '字典管理', icon: 'Reading' }
      },
      {
        path: 'system/config',
        name: 'SystemConfig',
        component: () => import('@/views/system/config/index.vue'),
        meta: { title: '参数设置', icon: 'Setting' }
      },
      {
        path: 'system/notice',
        name: 'SystemNotice',
        component: () => import('@/views/system/notice/index.vue'),
        meta: { title: '通知公告', icon: 'Bell' }
      },
      {
        path: 'system/log',
        name: 'SystemLog',
        component: () => import('@/views/system/log/index.vue'),
        meta: { title: '日志管理', icon: 'Document' }
      },
      // 系统监控路由
      {
        path: 'monitor/online',
        name: 'MonitorOnline',
        component: () => import('@/views/monitor/online/index.vue'),
        meta: { title: '在线用户', icon: 'Connection' }
      },
      {
        path: 'monitor/job',
        name: 'MonitorJob',
        component: () => import('@/views/monitor/job/index.vue'),
        meta: { title: '定时任务', icon: 'Timer' }
      },
      {
        path: 'monitor/druid',
        name: 'MonitorDruid',
        component: () => import('@/views/monitor/druid/index.vue'),
        meta: { title: '数据监控', icon: 'DataAnalysis' }
      },
      {
        path: 'monitor/server',
        name: 'MonitorServer',
        component: () => import('@/views/monitor/server/index.vue'),
        meta: { title: '服务监控', icon: 'Monitor' }
      },
      {
        path: 'monitor/cache',
        name: 'MonitorCache',
        component: () => import('@/views/monitor/cache/index.vue'),
        meta: { title: '缓存监控', icon: 'Histogram' }
      },
      {
        path: 'monitor/cacheList',
        name: 'MonitorCacheList',
        component: () => import('@/views/monitor/cacheList/index.vue'),
        meta: { title: '缓存列表', icon: 'List' }
      },
      // 系统工具路由
      {
        path: 'tool/build',
        name: 'ToolBuild',
        component: () => import('@/views/tool/build/index.vue'),
        meta: { title: '表单构建', icon: 'Edit' }
      },
      {
        path: 'tool/gen',
        name: 'ToolGen',
        component: () => import('@/views/tool/gen/index.vue'),
        meta: { title: '代码生成', icon: 'DocumentAdd' }
      },
      {
        path: 'tool/swagger',
        name: 'ToolSwagger',
        component: () => import('@/views/tool/swagger/index.vue'),
        meta: { title: '系统接口', icon: 'Link' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? `${to.meta.title} - 盘古用户平台` : '盘古用户平台'
  const token = localStorage.getItem('token')
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
