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

export const routes = [
  // {
  //   path: '/',
  //   redirect: '/main/Home'
  // },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '@/views/LoginView.vue')
  },
  {
    path: '/',
    name: '/',
    redirect: '/home',
    meta: { requiresAuth: true },
    component: () => import(/* webpackChunkName: "main" */ '@/views/MainView.vue'),
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName: "monitor" */ '@/views/sub/Home.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'monitor',
        name: 'monitor',
        title: '运行监控',
        meta: { requiresAuth: true },
        component: () => import(/* webpackChunkName: "monitor" */ '@/views/monitor.vue')
      },
      {
        path: 'predict',
        name: 'predict',
        title: '预测管理',
        meta: { requiresAuth: true },
        component: () => import(/* webpackChunkName: "predict" */ '@/views/predict.vue')
      },
      {
        path: 'statisticalAnalysis',
        name: 'statisticalAnalysis',
        title: '统计分析',
        meta: { requiresAuth: true },
        component: () => import(/* webpackChunkName: "monitor" */ '@/views/statisticalAnalysis.vue')
      },
      {
        path: 'system',
        name: 'system',
        redirect: '/system/user',
        meta: { requiresAuth: true },
        component: () => import(/* webpackChunkName: "system" */ '@/views/system/index.vue'),
        children: [
          {
            path: 'user',
            name: 'user',
            title: '用户管理',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/user.vue')
          },

          {
            name: 'role',
            path: 'role',
            title: '角色管理',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/role.vue')
          },
          {
            name: 'permission',
            path: 'permission',
            title: '权限管理',
            meta: { requiresAuth: true },
            component: () =>
              import(/* webpackChunkName: "system" */ '@/views/system/permission.vue')
          },
          {
            name: 'station',
            path: 'station',
            title: '场站管理',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/station.vue')
          },
          {
            name: 'service',
            path: 'service',
            title: '服务管理',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/service.vue')
          },
          {
            path: 'policy',
            name: 'policy',
            title: '策略管理',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/policy.vue')
          },
          {
            name: 'device',
            path: 'device',
            title: '设备管理',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/device.vue')
          },
          {
            name: 'alarmlog',
            path: 'alarmlog',
            title: '告警日志',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/alarmLog.vue')
          },
          {
            name: 'syslog',
            path: 'syslog',
            title: '系统日志',
            meta: { requiresAuth: true },
            component: () => import(/* webpackChunkName: "system" */ '@/views/system/log.vue')
          }
        ]
      }
    ]
  }
]

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

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('token') // 检查用户是否已登录

  // 2. 如果访问登录页且已登录，跳转到首页
  if (to.path === '/login' && isLoggedIn) {
    next('/')
    return
  }

  // 3. 检查路由权限
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (!isLoggedIn) {
      // 未登录则重定向到登录页，并携带原路径
      next('/login')
    } else {
      next()
    }
  } else {
    next() // 无需认证的路由直接放行
  }
})

export default router
