import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { usePermissionStore } from '@/stores/permission'
import { ElMessage } from 'element-plus'

// 导入组件
import Dashboard from '@/views/Dashboard.vue'
import Monitor from '@/views/Monitor.vue'
import MonitorCenter from '@/views/MonitorCenter.vue'
import UserManagement from '@/views/system/UserManagement.vue'
import RoleManagement from '@/views/system/RoleManagement.vue'
import Government from '@/views/Government.vue'
import Finance from '@/views/Finance.vue'
import Transport from '@/views/Transport.vue'
import Risk from '@/views/Risk.vue'
import Eco from '@/views/Eco.vue'
import Gov from '@/views/Gov.vue'
import Trade from '@/views/Trade.vue'
import Login from '@/views/Login.vue'
import UserManagementOld from '@/views/UserManagement.vue'
import CattleManagement from '@/views/CattleManagement.vue'
import MallManagement from '@/views/MallManagement.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Dashboard',
    component: Dashboard,
    meta: { requiresAuth: true }
  },
  {
    path: '/monitor',
    name: 'Monitor',
    component: Monitor,
    meta: { requiresAuth: true }
  },
  {
      path: '/monitor-center',
      name: 'MonitorCenter',
      component: MonitorCenter,
      meta: { requiresAuth: true }
    },
    {
      path: '/system/users',
      name: 'UserManagement',
      component: UserManagement,
      meta: { 
        requiresAuth: true,
        permission: 'user:view'
      }
    },
    {
      path: '/system/roles',
      name: 'RoleManagement',
      component: RoleManagement,
      meta: { 
        requiresAuth: true,
        permission: 'role:view'
      }
    },
  {
    path: '/government',
    name: 'Government',
    component: Government,
    meta: { requiresAuth: true }
  },
  {
    path: '/finance',
    name: 'Finance',
    component: Finance,
    meta: { requiresAuth: true }
  },
  {
    path: '/transport',
    name: 'Transport',
    component: Transport,
    meta: { requiresAuth: true }
  },
  {
    path: '/risk',
    name: 'Risk',
    component: Risk,
    meta: { requiresAuth: true }
  },
  {
    path: '/eco',
    name: 'Eco',
    component: Eco,
    meta: { requiresAuth: true }
  },
  {
    path: '/gov',
    name: 'Gov',
    component: Gov,
    meta: { requiresAuth: true }
  },
  {
    path: '/trade',
    name: 'Trade',
    component: Trade,
    meta: { requiresAuth: true }
  },
  {
    path: '/users',
    name: 'UserManagementOld',
    component: UserManagementOld,
    meta: { requiresAuth: true }
  },
  {
    path: '/cattle',
    name: 'CattleManagement',
    component: CattleManagement,
    meta: { requiresAuth: true }
  },
  {
    path: '/mall',
    name: 'MallManagement',
    component: MallManagement,
    meta: { requiresAuth: true }
  }
]

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

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    if (!authStore.isAuthenticated) {
      next('/login')
      return
    }
    
    // 检查权限
    if (to.meta.permission) {
      const permissionStore = usePermissionStore()
      if (!permissionStore.hasPermission.value(to.meta.permission)) {
        ElMessage.error('您没有访问该页面的权限')
        next('/dashboard')
        return
      }
    }
  }
  
  next()
})

export default router