import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/components/Layout.vue'
import Login from '@/views/Login.vue'
import Dashboard from '@/views/Dashboard.vue'
// import UserManagement from '@/views/UserManagement.vue'
// import SupervisionDashboard from '@/views/SupervisionDashboard.vue'
import ApprovalProcess from '@/views/ApprovalProcess.vue'
// import EpidemicManagement from '@/views/EpidemicManagement.vue'
// import VisualAnalysis from '@/views/VisualAnalysis.vue'
// import FileManagement from '@/views/FileManagement.vue'
import PersonnelManagement from '@/views/PersonnelManagement.vue'
import ServiceManagement from '@/views/ServiceManagement.vue'
// import WarehouseManagement from '@/views/WarehouseManagement.vue'
// import LogManagement from '@/views/LogManagement.vue'
// 新增页面组件
import MarketPrice from '@/views/MarketPrice.vue'
import DataCenter from '@/views/DataCenter.vue'
import FarmerManagement from '@/views/FarmerManagement.vue'
import SmartWarehouse from '@/views/SmartWarehouse.vue'
import BreedImprovement from '@/views/BreedImprovement.vue'
import PaperlessService from '@/views/PaperlessService.vue'
import SlaughterHarmless from '@/views/SlaughterHarmless.vue'
import FinanceInsurance from '@/views/FinanceInsurance.vue'
import CommunicationCommunity from '@/views/CommunicationCommunity.vue'
import OnlineConsultation from '@/views/OnlineConsultation.vue'
import CattleAcademy from '@/views/CattleAcademy.vue'
import MessageNotification from '@/views/MessageNotification.vue'
import AdminDepartment from '@/views/AdminDepartment.vue'
import AdminStaff from '@/views/AdminStaff.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {  
    path: '/',
    component: Layout,
    redirect: '/index/data_center',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '仪表板' }
      },
      {
        path: 'index/data_center',
        name: 'DataCenter',
        component: DataCenter,
        meta: { title: '数据览仓' }
      },
      {
        path: 'price/price_list',
        name: 'MarketPrice',
        component: MarketPrice,
        meta: { title: '市场行情' }
      },
      {
        path: 'personnel',
        name: 'PersonnelManagement',
        component: PersonnelManagement,
        meta: { title: '人员管理' },
        children: [
          {
            path: 'department',
            name: 'AdminDepartment',
            component: AdminDepartment,
            meta: { title: '行政部门' }
          },
          {
            path: 'staff',
            name: 'AdminStaff',
            component: AdminStaff,
            meta: { title: '行政人员' }
          }
        ]
      },
      {
        path: 'farmer',
        name: 'FarmerManagement',
        component: FarmerManagement,
        meta: { title: '养殖户管理' }
      },
      {
        path: 'smart-warehouse',
        name: 'SmartWarehouse',
        component: SmartWarehouse,
        meta: { title: '智慧仓库' },
        children: [
          {
            path: 'smart-collar',
            name: 'SmartCollar',
            component: () => import('@/views/smart-warehouse/smart-hardware/SmartCollar.vue'),
            meta: { title: '智能项圈' }
          },
          {
            path: 'smart-earmark',
            name: 'SmartEarmark',
            component: () => import('@/views/smart-warehouse/smart-hardware/SmartEarmark.vue'),
            meta: { title: '智能耳标' }
          },
          {
            path: 'smart-host',
            name: 'SmartHost',
            component: () => import('@/views/smart-warehouse/smart-hardware/SmartHost.vue'),
            meta: { title: '智能主机' }
          }
        ]
      },
      {
        path: 'breed-improvement',
        name: 'BreedImprovement',
        component: BreedImprovement,
        meta: { title: '品种改良管理' }
      },
      {
        path: 'paperless',
        name: 'PaperlessService',
        component: PaperlessService,
        meta: { title: '无纸化服务' }
      },
      {
        path: 'paperless/epidemic',
        name: 'EpidemicHome',
        component: () => import('@/views/paperless/EpidemicHome.vue'),
        meta: { title: '无纸化防疫' }
      },
      {
        path: 'paperless/epidemic/epidemic-agency',
        name: 'EpidemicAgencyManagement',
        component: () => import('@/views/paperless/epidemic/epidemic-agency/EpidemicAgency.vue'),
        meta: { title: '防疫机构管理' }
      },
      {
        path: 'paperless/epidemic/epidemic-record',
        name: 'EpidemicRecordManagement',
        component: () => import('@/views/paperless/epidemic/epidemic-record/EpidemicRecordManagement.vue'),
        meta: { title: '防疫记录管理' }
      },
      {
        path: 'paperless/epidemic/vaccine-management',
        name: 'VaccineManagement',
        component: () => import('@/views/paperless/epidemic/vaccine-management/VaccineManagement.vue'),
        meta: { title: '疫苗管理' }
      },
      { 
        path: 'paperless/epidemic/epidemic-activity', 
        name: 'EpidemicActivityManagement', 
        component: () => import('@/views/paperless/epidemic/epidemic-activity/EpidemicActivityManagement.vue'), 
        meta: { title: '防疫活动管理' } 
      },
      // 无纸化检疫相关路由
      { 
        path: 'paperless/quarantine',
        name: 'QuarantineHome',
        component: () => import('@/views/paperless/QuarantineHome.vue'),
        meta: { title: '无纸化检疫' }
      },
      { 
        path: 'paperless/quarantine/declaration',
        name: 'QuarantineDeclaration',
        component: () => import('@/views/paperless/quarantine/QuarantineDeclaration.vue'),
        meta: { title: '检疫审批' }
      },
      { 
        path: 'paperless/quarantine/record-query',
        name: 'QuarantineRecordQuery',
        component: () => import('@/views/paperless/quarantine/QuarantineRecordQuery.vue'),
        meta: { title: '检疫证查询' }
      },
      { 
        path: 'paperless/quarantine/config',
        name: 'QuarantineConfig',
        component: () => import('@/views/paperless/quarantine/QuarantineConfig.vue'),
        meta: { title: '检疫站清单' }
      },
      {
        path: 'slaughter',
        name: 'SlaughterHarmless',
        component: SlaughterHarmless,
        meta: { title: '屠宰无害化' }
      },
      {
        path: 'slaughter/slaughterhouse',
        name: 'Slaughterhouse',
        component: () => import('@/views/slaughter/Slaughterhouse.vue'),
        meta: { title: '屠宰场' }
      },
      {
        path: 'slaughter/harmless/place',
        name: 'HarmlessPlace',
        component: () => import('@/views/slaughter/harmless/HarmlessPlace.vue'),
        meta: { title: '无害化场所' }
      },
      {
        path: 'slaughter/harmless/registration',
        name: 'HarmlessRegistration',
        component: () => import('@/views/slaughter/harmless/HarmlessRegistration.vue'),
        meta: { title: '无害化登记' }
      },
      {
        path: 'finance',
        name: 'FinanceInsurance',
        component: FinanceInsurance,
        meta: { title: '金融保险' }
      },
      {
        path: 'examine/index',
        name: 'ProductCertification',
        component: ApprovalProcess,
        meta: { title: '生资认证' }
      },
      {
        path: 'shengzijiaoyi',
        name: 'ProductTrade',
        component: ServiceManagement,
        meta: { title: '生资交易' }
      },
      {
        path: 'community',
        name: 'CommunicationCommunity',
        component: CommunicationCommunity,
        meta: { title: '交流社区' }
      },
      {
        path: 'consultation',
        name: 'OnlineConsultation',
        component: OnlineConsultation,
        meta: { title: '线上问诊' }
      },
      {
        path: 'academy',
        name: 'CattleAcademy',
        component: CattleAcademy,
        meta: { title: '养牛学院' }
      },
      {      
        path: 'notification',
        name: 'MessageNotification',
        component: MessageNotification,
        meta: { title: '消息通知' }
      },
      {
        path: 'device-warning',
        name: 'DeviceWarning',
        component: () => import('@/views/DeviceWarning.vue'),
        meta: { title: '设备预警' }
      },
      // {
      //   path: 'users',
      //   name: 'UserManagement',
      //   component: UserManagement,
      //   meta: { title: '用户管理' }
      // },
      // {
      //   path: 'supervision',
      //   name: 'SupervisionDashboard',
      //   component: SupervisionDashboard,
      //   meta: { title: '监管仪表板' }
      // },
      // {
      //   path: 'approval',
      //   name: 'ApprovalProcess',
      //   component: ApprovalProcess,
      //   meta: { title: '审批流程' }
      // },
      // {
      //   path: 'file',
      //   name: 'FileManagement',
      //   component: FileManagement,
      //   meta: { title: '文件管理' }
      // },
      // {
      //   path: 'service',
      //   name: 'ServiceManagement',
      //   component: ServiceManagement,
      //   meta: { title: '服务管理' }
      // },
      // {
      //   path: 'warehouse',
      //   name: 'WarehouseManagement',
      //   component: WarehouseManagement,
      //   meta: { title: '仓库管理' }
      // },
      // {
      //   path: 'log',
      //   name: 'LogManagement',
      //   component: LogManagement,
      //   meta: { title: '日志管理' }
      // },
      // {
      //   path: 'epidemic',
      //   name: 'EpidemicManagement',
      //   component: EpidemicManagement,
      //   meta: { title: '疫情管理' }
      // },
      // {      
      //   path: 'visualization',
      //   name: 'VisualAnalysis',
      //   component: VisualAnalysis,
      //   meta: { title: '可视化分析' }
      // }
      // 无纸化防疫相关路由
      {
        path: 'paperless/epidemic',
        name: 'EpidemicHome',
        component: () => import('@/views/paperless/EpidemicHome.vue'),
        meta: { title: '防疫首页' }
      },
      {
        path: 'paperless/epidemic/epidemic-agency',
        name: 'EpidemicAgencyManagement',
        component: () => import('@/views/paperless/epidemic/epidemic-agency/EpidemicAgency.vue'),
        meta: { title: '防疫机构管理' }
      },
      {
        path: 'paperless/epidemic/epidemic-record',
        name: 'EpidemicRecordManagement',
        component: () => import('@/views/paperless/epidemic/epidemic-record/EpidemicRecordManagement.vue'),
        meta: { title: '防疫记录' }
      },
      {
        path: 'paperless/epidemic/vaccine-management',
        name: 'VaccineManagement',
        component: () => import('@/views/paperless/epidemic/vaccine-management/VaccineManagement.vue'),
        meta: { title: '疫苗管理' }
      },
      {
        path: 'paperless/epidemic/epidemic-activity',
        name: 'EpidemicActivityManagement',
        component: () => import('@/views/paperless/epidemic/epidemic-activity/EpidemicActivityManagement.vue'),
        meta: { title: '防疫活动管理' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory('/government/'), // 设置基础路径
  routes
})

// 路由拦截器 - 检查登录状态
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 政府端管理系统` : '政府端管理系统'
  
  // 不需要登录的页面直接通过
  if (to.path === '/login') {
    next()
    return
  }
  
  // 检查是否登录
  const token = localStorage.getItem('token')
  if (!token) {
    // 未登录，重定向到登录页面
    next('/login')
    return
  }
  
  next()
})

export default router