/**
 * 路由配置
 * @file routes.js
 * @description 应用路由定义
 */
import { 
  DashboardOutlined,
  UserOutlined,
  BankOutlined,
  TransactionOutlined,
  BarChartOutlined,
  SettingOutlined,
  LoginOutlined,
  FileTextOutlined,
  SafetyOutlined,
  LineChartOutlined,
  CreditCardOutlined,
  DesktopOutlined,
  TeamOutlined,
  UserSwitchOutlined,
  ProjectOutlined,
  ClockCircleOutlined,
  ToolOutlined,
  CheckCircleOutlined,
  SearchOutlined,
  DollarOutlined,
  BellOutlined,
  WarningOutlined
} from '@ant-design/icons-vue'

// 路由配置
const routes = [
  {
    path: '/',
    name: 'Root',
    meta: {
      hideInMenu: true
    }
    // 重定向逻辑已移至路由守卫中处理
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: '登录',
      requiresAuth: false,
      hideInMenu: true
    }
  },
  {
    path: '/test-projects',
    name: 'TestProjects',
    component: () => import('@/views/TestProjects.vue'),
    meta: {
      title: '项目接口测试',
      requiresAuth: false,
      hideInMenu: true
    }
  },
  {
    path: '/project-list',
    name: 'ProjectList',
    component: () => import('@/views/ProjectList.vue'),
    meta: {
      title: '项目清单',
      icon: ProjectOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller']
    }
  },
  {
    path: '/project-detail/:id',
    name: 'ProjectDetail',
    component: () => import('@/views/ProjectDetail.vue'),
    meta: {
      title: '项目详情',
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller'],
      hideInMenu: true
    }
  },
  {
    path: '/supervision-tasks',
    name: 'SupervisionTasks',
    component: () => import('@/views/SupervisionTasks.vue'),
    meta: {
      title: '监管任务',
      icon: ClockCircleOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller']
    }
  },
  {
    path: '/pending-installation',
    name: 'PendingInstallation',
    component: () => import('@/views/PendingInstallation.vue'),
    meta: {
      title: '待安装任务',
      icon: ToolOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller']
    }
  },
  {
    path: '/completed-supervision',
    name: 'CompletedSupervision',
    component: () => import('@/views/CompletedSupervision.vue'),
    meta: {
      title: '监管任务已结项',
      icon: CheckCircleOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller']
    }
  },
  {
    path: '/system-check',
    name: 'SystemCheck',
    component: () => import('@/views/SystemCheck.vue'),
    meta: {
      title: '系统日检',
      icon: SearchOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager']
    }
  },
  // {
  //   path: '/market-trends',
  //   name: 'MarketTrends',
  //   component: () => import('@/views/MarketTrends.vue'),
  //   meta: {
  //     title: '市场行情',
  //     icon: BarChartOutlined,
  //     requiresAuth: true,
  //     roles: ['admin', 'manager', 'teller', 'user']
  //   }
  // },
  {
    path: '/loan-management',
    name: 'LoanManagement',
    meta: {
      title: '贷款管理',
      icon: DollarOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller']
    },
    children: [
      {
        path: 'products',
        name: 'LoanProducts',
        component: () => import('@/views/loan/LoanProducts.vue'),
        meta: {
          title: '· 贷款商品',
          requiresAuth: true,
          roles: ['admin', 'manager', 'teller']
        }
      },
      {
        path: 'applications',
        name: 'LoanApplications',
        component: () => import('@/views/loan/LoanApplications.vue'),
        meta: {
          title: '· 贷款申请进度',
          requiresAuth: true,
          roles: ['admin', 'manager', 'teller']
        }
      },
      {
        path: 'contracts',
        name: 'LoanContracts',
        component: () => import('@/views/loan/LoanContracts.vue'),
        meta: {
          title: '· 贷款合同',
          requiresAuth: true,
          roles: ['admin', 'manager', 'teller']
        }
      },
      {
        path: 'release',
        name: 'LoanRelease',
        component: () => import('@/views/loan/LoanRelease.vue'),
        meta: {
          title: '· 贷款解押',
          requiresAuth: true,
          roles: ['admin', 'manager', 'teller']
        }
      }
    ]
  },
  // {
  //   path: '/message-notification',
  //   name: 'MessageNotification',
  //   component: () => import('@/views/MessageNotification.vue'),
  //   meta: {
  //     title: '消息通知',
  //     icon: BellOutlined,
  //     requiresAuth: true,
  //     roles: ['admin', 'manager', 'teller', 'user']
  //   }
  // },
  {
    path: '/device-warning',
    name: 'DeviceWarning',
    component: () => import('@/views/DeviceWarning.vue'),
    meta: {
      title: '设备预警',
      icon: WarningOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller', 'user']
    }
  },
  {
    path: '/employee-management',
    name: 'EmployeeManagement',
    component: () => import('@/views/EmployeeManagement.vue'),
    meta: {
      title: '员工管理',
      icon: TeamOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager']
    }
  },
  // {
  //   path: '/settings',
  //   name: 'Settings',
  //   component: () => import('@/views/Settings.vue'),
  //   meta: {
  //     title: '系统设置',
  //     icon: SettingOutlined,
  //     requiresAuth: true,
  //     roles: ['admin']
  //   }
  // },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: {
      title: '个人中心',
      icon: UserSwitchOutlined,
      requiresAuth: true,
      roles: ['admin', 'manager', 'teller', 'user']
    }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: {
      title: '页面不存在',
      requiresAuth: false,
      hideInMenu: true
    }
  }
]

// 根据用户角色过滤路由
export function filterRoutesByRole(routes, userRole) {
  return routes.filter(route => {
    // 如果路由没有meta或没有角色限制，则允许访问
    if (!route.meta || !route.meta.roles) {
      return true
    }
    
    // 检查用户角色是否在允许的角色列表中
    return route.meta.roles.includes(userRole)
  })
}

// 获取菜单项
export function getMenuItems(routes, userRole, parentPath = '') {
  const filteredRoutes = filterRoutesByRole(routes, userRole)
  
  return filteredRoutes
    .filter(route => !route.meta || !route.meta.hideInMenu)
    .map(route => {
      const fullPath = parentPath ? `${parentPath}/${route.path}` : route.path
      return {
        key: route.name,
        title: route.meta?.title || route.name,
        icon: route.meta?.icon,
        path: fullPath,
        children: route.children ? getMenuItems(route.children, userRole, fullPath) : undefined
      }
    })
}

export default routes