/**
 * CRMEB Admin 路由配置
 * 基于 CRMEB v5 标准版路由体系 1:1 映射
 * 
 * 路由结构说明:
 * - 一级菜单(topMenu): 主侧边栏显示的顶级模块
 * - 分组(group): 二级侧边栏的分组标题
 * - 菜单项(item): 具体的页面路由
 */

/**
 * 路由记录类型定义
 */
export type RouteRecord = {
  id: string              // 路由唯一标识,对应 CRMEB 的 name
  title: string           // 显示标题
  icon?: string           // 图标(仅一级菜单)
  path: string            // 路径(用于内部路由切换)
  componentKey: string    // 组件映射key
  parentId?: string       // 父路由ID
  groupId?: string        // 所属分组ID
  auth?: string[]         // 权限标识
  hidden?: boolean        // 是否隐藏
  keepAlive?: boolean     // 是否缓存
  order?: number          // 排序
  isAffix?: boolean       // 是否固定标签
}

/**
 * 菜单分组类型
 */
import { hasAdminModuleAccess } from '@/layouts/admin/utils/role.uts'

export type MenuGroup = {
  id: string
  title: string
  order?: number
}

/**
 * 一级菜单类型
 */
export type TopMenu = {
  id: string
  title: string
  icon: string
  path: string            // 默认跳转路径
  order: number
  groups: MenuGroup[]     // 该菜单下的分组列表
}

/**
 * ============================================
 * CRMEB 路由常量配置
 * ============================================
 */

/**
 * 一级菜单配置(主侧边栏)
 */
export const topMenus: TopMenu[] = [
  {
    id: 'home',
    title: '首页',
    icon: 'home',
    path: '/pages/mall/admin/homePage/index',
    order: 1,
    groups: []
  },
  {
    id: 'shop',
    title: '店铺',
    icon: 'shop',
    path: '/pages/mall/admin/shop/manage',
    order: 2,
    groups: [
      { id: 'shop-manage', title: '', order: 1 }
    ]
  },
  {
    id: 'user',
    title: '用户',
    icon: 'user',
    path: '/pages/mall/admin/user/management/index',
    order: 3,
    groups: [
      { id: 'user-manage', title: '', order: 1 }
    ]
  },
  {
    id: 'order',
    title: '订单',
    icon: 'order',
    path: '/pages/mall/admin/order/order-management/index',
    order: 3,
    groups: [
      { id: 'order-manage', title: '', order: 1 }
    ]
  },
  {
    id: 'product',
    title: '商品',
    icon: 'product',
    path: '/pages/mall/admin/product/statistic',
    order: 4,
    groups: [
      { id: 'product-manage', title: '', order: 1 }
    ]
  },
  {
    id: 'marketing',
    title: '营销',
    icon: 'marketing',
    path: '/pages/mall/admin/marketing/coupon/list',
    order: 5,
    groups: [
      { id: 'marketing-coupon', title: '优惠券', order: 1 },
      { id: 'marketing-integral', title: '积分管理', order: 2 },
      { id: 'marketing-lottery', title: '抽奖管理', order: 3 },
      { id: 'marketing-bargain', title: '砍价管理', order: 4 },
      { id: 'marketing-combination', title: '拼团管理', order: 5 },
      { id: 'marketing-seckill', title: '秒杀管理', order: 6 },
      { id: 'marketing-member', title: '付费会员', order: 7 },
      { id: 'marketing-live', title: '直播管理', order: 8 },
      { id: 'marketing-recharge', title: '用户充值', order: 9 },
      { id: 'marketing-checkin', title: '每日签到', order: 10 },
      { id: 'marketing-other', title: '其他', order: 11 }
    ]
  },
  {
    id: 'distribution',
    title: '分销',
    icon: 'share',
    path: '/pages/mall/admin/distribution/promoter/index',
    order: 6,
    groups: [
      { id: 'distribution-manage', title: '', order: 1 },
      { id: 'distribution-division', title: '事业部', order: 2 }
    ]
  },
  {
    id: 'kefu',
    title: '客服',
    icon: 'customer-service',
    path: '/pages/mall/admin/kefu/list',
    order: 7,
    groups: [
      { id: 'kefu-manage', title: '', order: 1 }
    ]
  },
  {
    id: 'finance',
    title: '财务',
    icon: 'finance',
    path: '/pages/mall/admin/finance/transaction_stats',
    order: 8,
    groups: [
      { id: 'finance-data', title: '', order: 1 },
      { id: 'finance-ops', title: '财务操作', order: 2 },
      { id: 'finance-record', title: '财务记录', order: 3 },
      { id: 'finance-commission', title: '佣金记录', order: 4 },
      { id: 'finance-balance', title: '余额记录', order: 5 }
    ]
  },
  {
    id: 'cms',
    title: '内容',
    icon: 'content',
    path: '/pages/mall/admin/cms/article/list',
    order: 9,
    groups: [
      { id: 'cms-manage', title: '', order: 1 }
    ]
  },
  {
    id: 'decoration',
    title: '装修',
    icon: 'decoration',
    path: '/pages/mall/admin/decoration/home',
    order: 10,
    groups: [
      { id: 'decoration-manage', title: '', order: 1 }
    ]
  },
  {
    id: 'app',
    title: '应用',
    icon: 'app',
    path: '/pages/mall/admin/app/wechat/menu',
    order: 11,
    groups: [
      { id: 'app-wechat', title: '公众号', order: 1 },
      { id: 'app-routine', title: '小程序', order: 2 },
      { id: 'app-mobile', title: 'APP', order: 3 },
      { id: 'app-pc', title: 'PC端', order: 4 }
    ]
  },
  {
    id: 'setting',
    title: '设置',
    icon: 'setting',
    path: '/pages/mall/admin/setting/system/config',
    order: 12,
    groups: [
      { id: 'setting-system', title: '系统设置', order: 1 },
      { id: 'setting-message', title: '通知管理', order: 2 },
      { id: 'setting-auth', title: '权限管理', order: 3 },
      { id: 'setting-delivery', title: '物流设置', order: 4 },
      { id: 'setting-interface', title: '接口设置', order: 5 }
    ]
  },
  {
    id: 'maintain',
    title: '维护',
    icon: 'tool',
    path: '/pages/mall/admin/maintain/dev-config/category',
    order: 13,
    groups: [
      { id: 'maintain-dev', title: '开发配置', order: 1 },
      { id: 'maintain-security', title: '安全维护', order: 2 },
      { id: 'maintain-data', title: '数据维护', order: 3 },
      { id: 'maintain-api', title: '对外接口', order: 4 },
      { id: 'maintain-lang', title: '语言设置', order: 5 },
      { id: 'maintain-tool', title: '开发工具', order: 6 },
      { id: 'maintain-sys', title: '系统信息', order: 7 }
    ]
  }
]

/**
 * 完整路由表
 * 映射自 CRMEB router/modules/*
 */
export const routes: RouteRecord[] = [
  // ========== 首页 ==========
  {
    id: 'home_index',
    title: '主页',
    icon: 'home',
    path: '/pages/mall/admin/homePage/index',
    componentKey: 'HomeIndex',
    isAffix: true,
    order: 1
  },

  // ========== 店铺模块 ==========
  {
    id: 'shop_admin_list',
    title: '店铺列表',
    path: '/pages/mall/admin/shop/shop-manage',
    componentKey: 'ShopAdminList',
    parentId: 'shop',
    groupId: 'shop-manage',
    order: 1
  },
  {
    id: 'shop_manage',
    title: '我的店铺',
    path: '/pages/mall/admin/shop/manage',
    componentKey: 'ShopManage',
    parentId: 'shop',
    groupId: 'shop-manage',
    order: 2
  },
  {
    id: 'shop_create',
    title: '申请入驻',
    path: '/pages/mall/admin/shop/create',
    componentKey: 'ShopCreate',
    parentId: 'shop',
    groupId: 'shop-manage',
    hidden: true,
    order: 3
  },

  // ========== 用户模块 ==========
      // ========== 个人中心 ==========
    {
      id: 'home_user_center',
      title: '个人中心',
      path: '/pages/mall/admin/userCenter/index',
      componentKey: 'UserCenter',
      order: 100
    },

    {
      id: 'user_statistic',
    title: '用户统计',
    path: '/pages/mall/admin/user/statistics/index',
    componentKey: 'UserStatistic',
    parentId: 'user',
    groupId: 'user-manage',
    auth: ['admin-user-statistic-index'],
    order: 1
  },
  {
    id: 'user_list',
    title: '用户管理',
    path: '/pages/mall/admin/user/management/index',
    componentKey: 'UserList',
    parentId: 'user',
    groupId: 'user-manage',
    auth: ['admin-user-user-index'],
    order: 2
  },
  {
    id: 'user_group',
    title: '用户分组',
    path: '/pages/mall/admin/user/grouping/index',
    componentKey: 'UserGroup',
    parentId: 'user',
    groupId: 'user-manage',
    auth: ['user-user-group'],
    order: 3
  },
  {
    id: 'user_label',
    title: '用户标签',
    path: '/pages/mall/admin/user/label/index',
    componentKey: 'UserLabel',
    parentId: 'user',
    groupId: 'user-manage',
    auth: ['user-user-label'],
    order: 4
  },
  {
    id: 'user_level',
    title: '用户等级',
    path: '/pages/mall/admin/user/level/index',
    componentKey: 'UserLevel',
    parentId: 'user',
    groupId: 'user-manage',
    auth: ['user-user-level'],
    order: 5
  },
  {
    id: 'user_config',
    title: '用户配置',
    path: '/pages/mall/admin/user/config/index',
    componentKey: 'UserMemberConfig',
    parentId: 'user',
    groupId: 'user-manage',
    order: 6
  },

  // ========== 商品模块 ==========
  {
    id: 'product_statistic',
    title: '商品统计',
    path: '/pages/mall/admin/product/statistic',
    componentKey: 'ProductStatistic',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-store-storeProuduct-statistic'],
    order: 1
  },
  {
    id: 'product_productList',
    title: '商品管理',
    path: '/pages/mall/admin/product/list',
    componentKey: 'ProductList',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-store-storeProuduct-index'],
    keepAlive: true,
    order: 2
  },
  {
    id: 'product_productClassify',
    title: '商品分类',
    path: '/pages/mall/admin/product/classification/index',
    componentKey: 'ProductClassify',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-store-storeCategory-index'],
    order: 3
  },
  {
    id: 'product_productAttr',
    title: '商品规格',
    path: '/pages/mall/admin/product/specifications/index',
    componentKey: 'ProductAttr',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-store-storeProuduct-index'],
    order: 4
  },
  {
    id: 'product_paramList',
    title: '商品参数',
    path: '/pages/mall/admin/product/parameters/index',
    componentKey: 'ProductParam',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-product-param-list'],
    order: 5
  },
  {
    id: 'product_labelList',
    title: '商品标签',
    path: '/pages/mall/admin/product/labels/index',
    componentKey: 'ProductLabel',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-product-label-list'],
    order: 6
  },
  {
    id: 'product_protectionList',
    title: '商品保障',
    path: '/pages/mall/admin/product/protection/index',
    componentKey: 'ProductProtection',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-product-protection-list'],
    order: 7
  },
  {
    id: 'product_productEvaluate',
    title: '商品评论',
    path: '/pages/mall/admin/product/reviews/index',
    componentKey: 'ProductReply',
    parentId: 'product',
    groupId: 'product-manage',
    auth: ['admin-store-storeProuduct-index'],
    order: 8
  },
  {
    id: 'product_edit',
    title: '编辑商品',
    path: '/pages/mall/admin/product/edit',
    componentKey: 'ProductEdit',
    parentId: 'product',
    groupId: 'product-manage',
    hidden: true,
    auth: ['admin-store-storeProuduct-index']
  },
  {
    id: 'product_member_price',
    title: '预售会员价',
    path: '/pages/mall/admin/product/member-price',
    componentKey: 'ProductMemberPrice',
    parentId: 'product',
    groupId: 'product-manage',
    hidden: true,
    auth: ['admin-store-storeProuduct-index']
  },

  // ========== 订单模块 ==========
  {
    id: 'order_statistic',
    title: '订单统计',
    path: '/pages/mall/admin/order/statistic',
    componentKey: 'OrderStatistic',
    parentId: 'order',
    groupId: 'order-manage',
    auth: ['admin-order-statistic-index'],
    order: 1
  },
  {
    id: 'order_list',
    title: '订单管理',
    path: '/pages/mall/admin/order/order-management/index',
    componentKey: 'OrderList',
    parentId: 'order',
    groupId: 'order-manage',
    auth: ['admin-order-storeOrder-index'],
    keepAlive: true,
    order: 2
  },
  {
    id: 'order_refund',
    title: '售后订单',
    path: '/pages/mall/admin/order/refund',
    componentKey: 'OrderRefund',
    parentId: 'order',
    groupId: 'order-manage',
    auth: ['admin-order-refund-index'],
    order: 3
  },
  {
    id: 'order_cashier',
    title: '收银订单',
    path: '/pages/mall/admin/order/cashier',
    componentKey: 'OrderCashier',
    parentId: 'order',
    groupId: 'order-manage',
    auth: ['admin-order-cashier-index'],
    order: 4
  },
  {
    id: 'order_verify',
    title: '核销记录',
    path: '/pages/mall/admin/order/verify',
    componentKey: 'OrderVerify',
    parentId: 'order',
    groupId: 'order-manage',
    auth: ['admin-order-verify-index'],
    order: 5
  },
  {
    id: 'order_config',
    title: '订单配置',
    path: '/pages/mall/admin/order/config',
    componentKey: 'OrderConfig',
    parentId: 'order',
    groupId: 'order-manage',
    auth: ['admin-order-config-index'],
    order: 6
  },

  // ========== 营销模块 ==========
  // 1. 优惠券
  {
    id: 'marketing_coupon_list',
    title: '优惠券列表',
    path: '/pages/mall/admin/marketing/coupon/list',
    componentKey: 'MarketingCouponList',
    parentId: 'marketing',
    groupId: 'marketing-coupon',
    auth: ['admin-marketing-storeCoupon-index'],
    order: 1
  },
  {
    id: 'marketing_coupon_user',
    title: '用户领取记录',
    path: '/pages/mall/admin/marketing/coupon/user',
    componentKey: 'MarketingCouponUser',
    parentId: 'marketing',
    groupId: 'marketing-coupon',
    auth: ['admin-marketing-storeCouponUser-index'],
    order: 2
  },
  // 2. 积分管理
  {
    id: 'marketing_integral_statistic',
    title: '积分统计',
    path: '/pages/mall/admin/marketing/points/statistic',
    componentKey: 'MarketingIntegralStatistic',
    parentId: 'marketing',
    groupId: 'marketing-integral',
    auth: ['admin-marketing-integral-statistic'],
    order: 1
  },
  {
    id: 'marketing_integral_product',
    title: '积分商品',
    path: '/pages/mall/admin/marketing/points/product',
    componentKey: 'MarketingIntegralProduct',
    parentId: 'marketing',
    groupId: 'marketing-integral',
    auth: ['admin-marketing-integral-product'],
    order: 2
  },
  {
    id: 'marketing_integral_order',
    title: '积分订单',
    path: '/pages/mall/admin/marketing/points/order',
    componentKey: 'MarketingIntegralOrder',
    parentId: 'marketing',
    groupId: 'marketing-integral',
    auth: ['admin-marketing-integral-order'],
    order: 3
  },
  {
    id: 'marketing_integral_record',
    title: '积分记录',
    path: '/pages/mall/admin/marketing/points/record',
    componentKey: 'MarketingIntegralRecord',
    parentId: 'marketing',
    groupId: 'marketing-integral',
    auth: ['admin-marketing-integral-record'],
    order: 4
  },
  {
    id: 'marketing_integral_config',
    title: '积分配置',
    path: '/pages/mall/admin/marketing/points/config',
    componentKey: 'MarketingIntegralConfig',
    parentId: 'marketing',
    groupId: 'marketing-integral',
    auth: ['admin-marketing-integral-config'],
    order: 5
  },
  // 3. 抽奖管理
  {
    id: 'marketing_lottery_list',
    title: '抽奖列表',
    path: '/pages/mall/admin/marketing/lottery/list',
    componentKey: 'MarketingLotteryList',
    parentId: 'marketing',
    groupId: 'marketing-lottery',
    auth: ['admin-marketing-lottery-index'],
    order: 1
  },
  {
    id: 'marketing_lottery_config',
    title: '抽奖配置',
    path: '/pages/mall/admin/marketing/lottery/config',
    componentKey: 'MarketingLotteryConfig',
    parentId: 'marketing',
    groupId: 'marketing-lottery',
    auth: ['admin-marketing-lottery-config'],
    order: 2
  },
  // 4. 砍价管理
  {
    id: 'marketing_bargain_product',
    title: '砍价商品',
    path: '/pages/mall/admin/marketing/bargain/product',
    componentKey: 'MarketingBargainProduct',
    parentId: 'marketing',
    groupId: 'marketing-bargain',
    auth: ['admin-marketing-bargain-product'],
    order: 1
  },
  {
    id: 'marketing_bargain_list',
    title: '砍价列表',
    path: '/pages/mall/admin/marketing/bargain/list',
    componentKey: 'MarketingBargainList',
    parentId: 'marketing',
    groupId: 'marketing-bargain',
    auth: ['admin-marketing-bargain-index'],
    order: 2
  },
  // 5. 拼团管理
  {
    id: 'marketing_combination_product',
    title: '拼团商品',
    path: '/pages/mall/admin/marketing/combination/product',
    componentKey: 'MarketingCombinationProduct',
    parentId: 'marketing',
    groupId: 'marketing-combination',
    auth: ['admin-marketing-combination-product'],
    order: 1
  },
  {
    id: 'marketing_combination_list',
    title: '拼团列表',
    path: '/pages/mall/admin/marketing/combination/list',
    componentKey: 'MarketingCombinationList',
    parentId: 'marketing',
    groupId: 'marketing-combination',
    auth: ['admin-marketing-combination-index'],
    order: 2
  },
  {
    id: 'marketing_combination_create',
    title: '添加拼团商品',
    path: '/pages/mall/admin/marketing/combination/create',
    componentKey: 'MarketingCombinationCreate',
    parentId: 'marketing',
    groupId: 'marketing-combination',
    auth: ['admin-marketing-combination-create'],
    hidden: true
  },
  // 6. 秒杀管理
  {
    id: 'marketing_seckill_list',
    title: '秒杀列表',
    path: '/pages/mall/admin/marketing/seckill/list',
    componentKey: 'MarketingSeckillList',
    parentId: 'marketing',
    groupId: 'marketing-seckill',
    auth: ['admin-marketing-seckill-index'],
    order: 1
  },
  {
    id: 'marketing_seckill_product',
    title: '秒杀商品',
    path: '/pages/mall/admin/marketing/seckill/product',
    componentKey: 'MarketingSeckillProduct',
    parentId: 'marketing',
    groupId: 'marketing-seckill',
    auth: ['admin-marketing-seckill-product'],
    order: 2
  },
  {
    id: 'marketing_seckill_config',
    title: '秒杀配置',
    path: '/pages/mall/admin/marketing/seckill/config',
    componentKey: 'MarketingSeckillConfig',
    parentId: 'marketing',
    groupId: 'marketing-seckill',
    auth: ['admin-marketing-seckill-config'],
    order: 3
  },
  // 7. 付费会员 (从用户模块迁入)
  {
    id: 'marketing_member_type',
    title: '会员类型',
    path: '/pages/mall/admin/marketing/member/type',
    componentKey: 'MarketingMemberType',
    parentId: 'marketing',
    groupId: 'marketing-member',
    auth: ['admin-user-member-type'],
    order: 1
  },
  {
    id: 'marketing_member_right',
    title: '会员权益',
    path: '/pages/mall/admin/marketing/member/right',
    componentKey: 'MarketingMemberRight',
    parentId: 'marketing',
    groupId: 'marketing-member',
    auth: ['admin-user-member-right'],
    order: 2
  },
  {
    id: 'marketing_member_card',
    title: '卡密会员',
    path: '/pages/mall/admin/marketing/member/card',
    componentKey: 'MarketingMemberCard',
    parentId: 'marketing',
    groupId: 'marketing-member',
    auth: ['admin-user-member-card'],
    order: 3
  },
  {
    id: 'marketing_member_record',
    title: '会员记录',
    path: '/pages/mall/admin/marketing/member/record',
    componentKey: 'MarketingMemberRecord',
    parentId: 'marketing',
    groupId: 'marketing-member',
    auth: ['admin-user-member-record'],
    order: 4
  },
  {
    id: 'marketing_member_config',
    title: '会员配置',
    path: '/pages/mall/admin/marketing/member/config',
    componentKey: 'MarketingMemberConfig',
    parentId: 'marketing',
    groupId: 'marketing-member',
    auth: ['admin-user-member-config'],
    order: 5
  },
  // 8. 直播管理
  {
    id: 'marketing_live_room',
    title: '直播间管理',
    path: '/pages/mall/admin/marketing/live/room',
    componentKey: 'MarketingLiveRoom',
    parentId: 'marketing',
    groupId: 'marketing-live',
    auth: ['admin-marketing-live-room'],
    order: 1
  },
  {
    id: 'marketing_live_product',
    title: '直播商品管理',
    path: '/pages/mall/admin/marketing/live/product',
    componentKey: 'MarketingLiveProduct',
    parentId: 'marketing',
    groupId: 'marketing-live',
    auth: ['admin-marketing-live-product'],
    order: 2
  },
  {
    id: 'marketing_live_anchor',
    title: '主播管理',
    path: '/pages/mall/admin/marketing/live/anchor',
    componentKey: 'MarketingLiveAnchor',
    parentId: 'marketing',
    groupId: 'marketing-live',
    auth: ['admin-marketing-live-anchor'],
    order: 3
  },
  // 9. 用户充值
  {
    id: 'marketing_recharge_quota',
    title: '金额设置',
    path: '/pages/mall/admin/marketing/recharge/quota',
    componentKey: 'MarketingRechargeQuota',
    parentId: 'marketing',
    groupId: 'marketing-recharge',
    auth: ['admin-marketing-recharge-quota'],
    order: 1
  },
  {
    id: 'marketing_recharge_config',
    title: '充值配置',
    path: '/pages/mall/admin/marketing/recharge/config',
    componentKey: 'MarketingRechargeConfig',
    parentId: 'marketing',
    groupId: 'marketing-recharge',
    auth: ['admin-marketing-recharge-config'],
    order: 2
  },
  // 10. 每日签到
  {
    id: 'marketing_checkin_config',
    title: '签到配置',
    path: '/pages/mall/admin/marketing/checkin/config',
    componentKey: 'MarketingCheckinConfig',
    parentId: 'marketing',
    groupId: 'marketing-checkin',
    auth: ['admin-marketing-checkin-config'],
    order: 1
  },
  {
    id: 'marketing_checkin_reward',
    title: '签到奖励',
    path: '/pages/mall/admin/marketing/checkin/reward',
    componentKey: 'MarketingCheckinReward',
    parentId: 'marketing',
    groupId: 'marketing-checkin',
    auth: ['admin-marketing-checkin-reward'],
    order: 2
  },
  // 11. 渠道码 & 新人礼
  {
    id: 'marketing_channel_code',
    title: '渠道码',
    path: '/pages/mall/admin/marketing/channel/list',
    componentKey: 'MarketingChannelList',
    parentId: 'marketing',
    groupId: 'marketing-other',
    auth: ['admin-marketing-channel-index'],
    order: 1
  },
  {
    id: 'marketing_newcomer_gift',
    title: '新人礼',
    path: '/pages/mall/admin/marketing/newcomer/gift',
    componentKey: 'MarketingNewcomerGift',
    parentId: 'marketing',
    groupId: 'marketing-other',
    auth: ['admin-marketing-newcomer-index'],
    order: 2
  },

  // ========== 内容模块 ==========
  {
    id: 'cms_article',
    title: '文章管理',
    path: '/pages/mall/admin/cms/article/list',
    componentKey: 'CmsArticle',
    parentId: 'cms',
    groupId: 'cms-manage',
    auth: ['admin-cms-article-index'],
    order: 1
  },
  {
    id: 'cms_category',
    title: '文章分类',
    path: '/pages/mall/admin/cms/category/list',
    componentKey: 'CmsCategory',
    parentId: 'cms',
    groupId: 'cms-manage',
    auth: ['admin-cms-category-index'],
    order: 2
  },

  // ========== 财务模块 ==========
  {
    id: 'finance_transaction_stats',
    title: '交易统计',
    path: '/pages/mall/admin/finance/transaction_stats',
    componentKey: 'FinanceTransactionStats',
    parentId: 'finance',
    groupId: 'finance-data',
    auth: ['admin-finance-transaction-stats'],
    order: 1
  },
  {
    id: 'finance_withdrawal',
    title: '提现申请',
    path: '/pages/mall/admin/finance/withdrawal',
    componentKey: 'FinanceWithdrawal',
    parentId: 'finance',
    groupId: 'finance-ops',
    auth: ['admin-finance-withdrawal-index'],
    order: 1
  },
  {
    id: 'finance_invoice',
    title: '发票管理',
    path: '/pages/mall/admin/finance/invoice',
    componentKey: 'FinanceInvoice',
    parentId: 'finance',
    groupId: 'finance-ops',
    auth: ['admin-finance-invoice-index'],
    order: 2
  },
  {
    id: 'finance_recharge',
    title: '充值记录',
    path: '/pages/mall/admin/finance/recharge',
    componentKey: 'FinanceRecharge',
    parentId: 'finance',
    groupId: 'finance-record',
    auth: ['admin-finance-recharge-index'],
    order: 1
  },
  {
    id: 'finance_capital_flow',
    title: '资金流水',
    path: '/pages/mall/admin/finance/capital_flow',
    componentKey: 'FinanceCapitalFlow',
    parentId: 'finance',
    groupId: 'finance-record',
    auth: ['admin-finance-capital-flow-index'],
    order: 2
  },
  {
    id: 'finance_bill',
    title: '账单记录',
    path: '/pages/mall/admin/finance/bill',
    componentKey: 'FinanceBill',
    parentId: 'finance',
    groupId: 'finance-record',
    auth: ['admin-finance-bill-index'],
    order: 3
  },
  {
    id: 'finance_commission_record',
    title: '佣金记录',
    path: '/pages/mall/admin/finance/commission',
    componentKey: 'FinanceCommission',
    parentId: 'finance',
    groupId: 'finance-commission',
    auth: ['admin-finance-commission-index'],
    order: 1
  },
  {
    id: 'finance_balance_stats',
    title: '余额统计',
    path: '/pages/mall/admin/finance/balance_stats',
    componentKey: 'FinanceBalanceStats',
    parentId: 'finance',
    groupId: 'finance-balance',
    auth: ['admin-finance-balance-stats'],
    order: 1
  },
  {
    id: 'finance_balance_record',
    title: '余额记录',
    path: '/pages/mall/admin/finance/balance_record',
    componentKey: 'FinanceBalanceRecord',
    parentId: 'finance',
    groupId: 'finance-balance',
    auth: ['admin-finance-balance-record-index'],
    order: 2
  },

  // ========== 数据统计模块 ==========
  {
    id: 'statistic_index',
    title: '数据概览',
    path: '/pages/mall/admin/statistic/index',
    componentKey: 'StatisticIndex',
    parentId: 'statistic',
    groupId: 'statistic-data',
    auth: ['admin-statistic-index'],
    order: 1
  },

  // ========== 分销模块 ==========
  {
    id: 'distribution_statistic',
    title: '分销统计',
    path: '/pages/mall/admin/distribution/statistic',
    componentKey: 'DistributionStatistic',
    parentId: 'distribution',
    groupId: 'distribution-manage',
    hidden: true,
    order: 1
  },
  {
    id: 'distribution_promoter',
    title: '分销员管理',
    path: '/pages/mall/admin/distribution/promoter/index',
    componentKey: 'DistributionPromoter',
    parentId: 'distribution',
    groupId: 'distribution-manage',
    order: 2
  },
  {
    id: 'distribution_level',
    title: '分销等级',
    path: '/pages/mall/admin/distribution/level/index',
    componentKey: 'DistributionLevel',
    parentId: 'distribution',
    groupId: 'distribution-manage',
    order: 3
  },
  {
    id: 'distribution_setting',
    title: '分销设置',
    path: '/pages/mall/admin/distribution/setting/index',
    componentKey: 'DistributionSetting',
    parentId: 'distribution',
    groupId: 'distribution-manage',
    order: 4
  },
  {
    id: 'division_list',
    title: '事业部列表',
    path: '/pages/mall/admin/distribution/division/list',
    componentKey: 'DivisionList',
    parentId: 'distribution',
    groupId: 'distribution-division',
    order: 1
  },
  {
    id: 'division_agent',
    title: '代理商列表',
    path: '/pages/mall/admin/distribution/division/agent',
    componentKey: 'DivisionAgent',
    parentId: 'distribution',
    groupId: 'distribution-division',
    order: 2
  },
  {
    id: 'division_apply',
    title: '代理商申请',
    path: '/pages/mall/admin/distribution/division/apply',
    componentKey: 'DivisionApply',
    parentId: 'distribution',
    groupId: 'distribution-division',
    order: 3
  },

  // ========== 客服模块 ==========
  {
    id: 'kefu_list',
    title: '客服列表',
    path: '/pages/mall/admin/kefu/list',
    componentKey: 'KefuList',
    parentId: 'kefu',
    groupId: 'kefu-manage',
    order: 1
  },
  {
    id: 'kefu_words',
    title: '客服话术',
    path: '/pages/mall/admin/kefu/words',
    componentKey: 'KefuWords',
    parentId: 'kefu',
    groupId: 'kefu-manage',
    order: 2
  },
  {
    id: 'kefu_feedback',
    title: '用户留言',
    path: '/pages/mall/admin/kefu/feedback',
    componentKey: 'KefuFeedback',
    parentId: 'kefu',
    groupId: 'kefu-manage',
    order: 3
  },
  {
    id: 'kefu_auto_reply',
    title: '自动回复',
    path: '/pages/mall/admin/kefu/auto_reply',
    componentKey: 'KefuAutoReply',
    parentId: 'kefu',
    groupId: 'kefu-manage',
    order: 4
  },
  {
    id: 'kefu_config',
    title: '客服配置',
    path: '/pages/mall/admin/kefu/config',
    componentKey: 'KefuConfig',
    parentId: 'kefu',
    groupId: 'kefu-manage',
    order: 5
  },

  // ========== 装修模块 ==========
  {
    id: 'decoration_home',
    title: '首页装修',
    path: '/pages/mall/admin/decoration/home',
    componentKey: 'DecorationHome',
    parentId: 'decoration',
    groupId: 'decoration-manage',
    order: 1
  },
  {
    id: 'decoration_category',
    title: '商品分类',
    path: '/pages/mall/admin/decoration/category',
    componentKey: 'DecorationCategory',
    parentId: 'decoration',
    groupId: 'decoration-manage',
    order: 2
  },
  {
    id: 'decoration_user',
    title: '个人中心',
    path: '/pages/mall/admin/decoration/user',
    componentKey: 'DecorationUser',
    parentId: 'decoration',
    groupId: 'decoration-manage',
    order: 3
  },
  {
    id: 'decoration_data',
    title: '数据配置',
    path: '/pages/mall/admin/decoration/data',
    componentKey: 'DecorationData',
    parentId: 'decoration',
    groupId: 'decoration-manage',
    order: 4
  },
  {
    id: 'decoration_style',
    title: '主题风格',
    path: '/pages/mall/admin/decoration/style',
    componentKey: 'DecorationStyle',
    parentId: 'decoration',
    groupId: 'decoration-manage',
    order: 5
  },
  {
    id: 'decoration_material',
    title: '素材管理',
    path: '/pages/mall/admin/decoration/material',
    componentKey: 'DecorationMaterial',
    parentId: 'decoration',
    groupId: 'decoration-manage',
    order: 6
  },
  {
    id: 'DecorationLink',
    title: '链接管理',
    path: '/pages/mall/admin/decoration/link',
    componentKey: 'DecorationLink',
    parentId: 'decoration',
    groupId: 'decoration-manage',
    order: 7
  },

  // ========== 设置模块 (1:1 复刻 CRMEB 路由结构) ==========
  // 1. 系统设置
  { id: 'setting_system_config', title: '系统配置', path: '/pages/mall/admin/setting/system/config', componentKey: 'SettingSystemConfig', parentId: 'setting', groupId: 'setting-system', order: 1 },
  
  // 2. 通知管理
  { id: 'setting_message_index', title: '消息管理', path: '/pages/mall/admin/setting/message', componentKey: 'SettingMessageIndex', parentId: 'setting', groupId: 'setting-message', order: 1 },
  { id: 'setting_protocol_index', title: '协议设置', path: '/pages/mall/admin/setting/agreement', componentKey: 'SettingProtocolIndex', parentId: 'setting', groupId: 'setting-message', order: 2 },
  { id: 'setting_ticket_index', title: '小票配置', path: '/pages/mall/admin/setting/ticket', componentKey: 'SettingTicketIndex', parentId: 'setting', groupId: 'setting-message', order: 3 },
  
  // 3. 权限管理
  { id: 'setting_auth_admin', title: '管理员管理', path: '/pages/mall/admin/setting/auth/admin', componentKey: 'SettingAuthAdmin', parentId: 'setting', groupId: 'setting-auth', order: 1 },
  { id: 'setting_auth_role', title: '角色管理', path: '/pages/mall/admin/setting/auth/role', componentKey: 'SettingAuthRole', parentId: 'setting', groupId: 'setting-auth', order: 2 },
  { id: 'setting_auth_menu', title: '菜单管理', path: '/pages/mall/admin/setting/auth/permission', componentKey: 'SettingAuthPermission', parentId: 'setting', groupId: 'setting-auth', order: 3 },
  
  // 4. 物流设置
  { id: 'setting_delivery_courier', title: '配送员管理', path: '/pages/mall/admin/setting/delivery/staff', componentKey: 'SettingDeliveryStaff', parentId: 'setting', groupId: 'setting-delivery', order: 1 },
  { id: 'setting_delivery_pickup_list', title: '提货点', path: '/pages/mall/admin/setting/delivery/station', componentKey: 'SettingDeliveryStation', parentId: 'setting', groupId: 'setting-delivery', order: 2 },
  { id: 'setting_delivery_verifier', title: '核销员', path: '/pages/mall/admin/setting/delivery/verifier', componentKey: 'SettingDeliveryVerifier', parentId: 'setting', groupId: 'setting-delivery', order: 3 },
  { id: 'setting_delivery_freight', title: '运费模板', path: '/pages/mall/admin/setting/delivery/template', componentKey: 'SettingDeliveryTemplate', parentId: 'setting', groupId: 'setting-delivery', order: 4 },
  
  // 5. 接口设置
  { id: 'setting_api_yht_page', title: '一号通页面', path: '/pages/mall/admin/setting/interface/onepass/index', componentKey: 'SettingInterfaceOnepassIndex', parentId: 'setting', groupId: 'setting-interface', order: 1 },
  { id: 'setting_api_yht_config', title: '一号通配置', path: '/pages/mall/admin/setting/interface/onepass/config', componentKey: 'SettingInterfaceOnepassConfig', parentId: 'setting', groupId: 'setting-interface', order: 2 },
  { id: 'setting_api_storage', title: '系统存储配置', path: '/pages/mall/admin/setting/interface/storage', componentKey: 'SettingInterfaceStorage', parentId: 'setting', groupId: 'setting-interface', order: 3 },
  { id: 'setting_api_collect', title: '商品采集配置', path: '/pages/mall/admin/setting/interface/collect', componentKey: 'SettingInterfaceCollect', parentId: 'setting', groupId: 'setting-interface', order: 4 },
  { id: 'setting_api_logistics', title: '物流查询配置', path: '/pages/mall/admin/setting/interface/logistics', componentKey: 'SettingInterfaceLogistics', parentId: 'setting', groupId: 'setting-interface', order: 5 },
  { id: 'setting_api_waybill', title: '电子面单配置', path: '/pages/mall/admin/setting/interface/e-sheet', componentKey: 'SettingInterfaceESheet', parentId: 'setting', groupId: 'setting-interface', order: 6 },
  { id: 'setting_api_sms', title: '短信接口配置', path: '/pages/mall/admin/setting/interface/sms', componentKey: 'SettingInterfaceSms', parentId: 'setting', groupId: 'setting-interface', order: 7 },
  { id: 'setting_api_pay', title: '商城支付配置', path: '/pages/mall/admin/setting/interface/payment', componentKey: 'SettingInterfacePayment', parentId: 'setting', groupId: 'setting-interface', order: 8 },

  // ========== 应用模块 ==========
  // 1. 公众号
  { id: 'app_wechat_menu', title: '微信菜单', path: '/pages/mall/admin/app/wechat/menu', componentKey: 'AppWechatMenu', parentId: 'app', groupId: 'app-wechat', order: 1 },
  { id: 'app_wechat_news', title: '图文管理', path: '/pages/mall/admin/app/wechat/news', componentKey: 'AppWechatNews', parentId: 'app', groupId: 'app-wechat', order: 2 },
  { id: 'app_wechat_reply_follow', title: '关注回复', path: '/pages/mall/admin/app/wechat/reply/follow', componentKey: 'AppWechatReplyFollow', parentId: 'app', groupId: 'app-wechat', order: 3 },
  { id: 'app_wechat_reply_keyword', title: '关键字回复', path: '/pages/mall/admin/app/wechat/reply/keyword', componentKey: 'AppWechatReplyKeyword', parentId: 'app', groupId: 'app-wechat', order: 4 },
  { id: 'app_wechat_reply_invalid', title: '无效词回复', path: '/pages/mall/admin/app/wechat/reply/invalid', componentKey: 'AppWechatReplyInvalid', parentId: 'app', groupId: 'app-wechat', order: 5 },
  { id: 'app_wechat_config', title: '公众号配置', path: '/pages/mall/admin/app/wechat/config', componentKey: 'AppWechatConfig', parentId: 'app', groupId: 'app-wechat', order: 6 },

  // 2. 小程序
  { id: 'app_routine_download', title: '小程序下载', path: '/pages/mall/admin/app/routine/download', componentKey: 'AppRoutineDownload', parentId: 'app', groupId: 'app-routine', order: 1 },
  { id: 'app_routine_config', title: '小程序配置', path: '/pages/mall/admin/app/routine/config', componentKey: 'AppRoutineConfig', parentId: 'app', groupId: 'app-routine', order: 2 },

  // 3. APP
  { id: 'app_mobile_config', title: 'APP配置', path: '/pages/mall/admin/app/mobile/config', componentKey: 'AppMobileConfig', parentId: 'app', groupId: 'app-mobile', order: 1 },
  { id: 'app_mobile_version', title: '版本管理', path: '/pages/mall/admin/app/mobile/version', componentKey: 'AppMobileVersion', parentId: 'app', groupId: 'app-mobile', order: 2 },

  // 4. PC端
  { id: 'app_pc_design', title: 'PC端装修', path: '/pages/mall/admin/app/pc/design', componentKey: 'AppPcDesign', parentId: 'app', groupId: 'app-pc', order: 1 },
  { id: 'app_pc_config', title: 'PC端配置', path: '/pages/mall/admin/app/pc/config', componentKey: 'AppPcConfig', parentId: 'app', groupId: 'app-pc', order: 2 },

  // ========== 维护模块 ==========
  // 开发配置
  { id: 'maintain_dev_config', title: '配置分类', path: '/pages/mall/admin/maintain/dev-config/category', componentKey: 'MaintainDevConfig', parentId: 'maintain', groupId: 'maintain-dev', order: 1 },
  { id: 'maintain_dev_data', title: '组合数据', path: '/pages/mall/admin/maintain/dev-config/combination-data', componentKey: 'MaintainDevData', parentId: 'maintain', groupId: 'maintain-dev', order: 2 },
  { id: 'maintain_dev_task', title: '定时任务', path: '/pages/mall/admin/maintain/dev-config/cron-job', componentKey: 'MaintainDevTask', parentId: 'maintain', groupId: 'maintain-dev', order: 3 },
  { id: 'maintain_dev_auth', title: '权限维护', path: '/pages/mall/admin/maintain/dev-config/permission', componentKey: 'MaintainDevAuth', parentId: 'maintain', groupId: 'maintain-dev', order: 4 },
  { id: 'maintain_dev_module', title: '模块配置', path: '/pages/mall/admin/maintain/dev-config/module-config', componentKey: 'MaintainDevModule', parentId: 'maintain', groupId: 'maintain-dev', order: 5 },
  { id: 'maintain_dev_event', title: '自定事件', path: '/pages/mall/admin/maintain/dev-config/custom-event', componentKey: 'MaintainDevEvent', parentId: 'maintain', groupId: 'maintain-dev', order: 6 },
  
  // 安全维护
  { id: 'maintain_security_cache', title: '刷新缓存', path: '/pages/mall/admin/maintain/security/refresh-cache', componentKey: 'MaintainSecurityCache', parentId: 'maintain', groupId: 'maintain-security', order: 1 },
  { id: 'maintain_security_log', title: '系统日志', path: '/pages/mall/admin/maintain/security/system-log', componentKey: 'MaintainSecurityLog', parentId: 'maintain', groupId: 'maintain-security', order: 2 },
  { id: 'maintain_security_upgrade', title: '在线升级', path: '/pages/mall/admin/maintain/security/online-upgrade', componentKey: 'MaintainSecurityUpgrade', parentId: 'maintain', groupId: 'maintain-security', order: 3 },
  
  // 数据维护
  { id: 'maintain_data_logistics', title: '物流公司', path: '/pages/mall/admin/maintain/data/logistics', componentKey: 'MaintainDataLogistics', parentId: 'maintain', groupId: 'maintain-data', order: 1 },
  { id: 'maintain_data_city', title: '城市数据', path: '/pages/mall/admin/maintain/data/city', componentKey: 'MaintainDataCity', parentId: 'maintain', groupId: 'maintain-data', order: 2 },
  { id: 'maintain_data_clear', title: '清除数据', path: '/pages/mall/admin/maintain/data/clear', componentKey: 'MaintainDataClear', parentId: 'maintain', groupId: 'maintain-data', order: 3 },
  
  // 对外接口
  { id: 'maintain_api_account', title: '账号管理', path: '/pages/mall/admin/maintain/api/account', componentKey: 'MaintainApiAccount', parentId: 'maintain', groupId: 'maintain-api', order: 1 },
  
  // 语言设置
  { id: 'maintain_lang_list', title: '语言列表', path: '/pages/mall/admin/maintain/lang/list', componentKey: 'MaintainLangList', parentId: 'maintain', groupId: 'maintain-lang', order: 1 },
  { id: 'maintain_lang_detail', title: '语言详情', path: '/pages/mall/admin/maintain/lang/detail', componentKey: 'MaintainLangDetail', parentId: 'maintain', groupId: 'maintain-lang', order: 2 },
  { id: 'maintain_lang_region', title: '地区列表', path: '/pages/mall/admin/maintain/lang/region', componentKey: 'MaintainLangRegion', parentId: 'maintain', groupId: 'maintain-lang', order: 3 },
  { id: 'maintain_lang_config', title: '翻译配置', path: '/pages/mall/admin/maintain/lang/config', componentKey: 'MaintainLangConfig', parentId: 'maintain', groupId: 'maintain-lang', order: 4 },
  
  // 开发工具
  { id: 'maintain_tool_db', title: '数据库管理', path: '/pages/mall/admin/maintain/dev-tools/database', componentKey: 'MaintainToolDb', parentId: 'maintain', groupId: 'maintain-tool', order: 1 },
  { id: 'maintain_tool_file', title: '文件管理', path: '/pages/mall/admin/maintain/dev-tools/file', componentKey: 'MaintainToolFile', parentId: 'maintain', groupId: 'maintain-tool', order: 2 },
  { id: 'maintain_tool_api', title: '接口管理', path: '/pages/mall/admin/maintain/dev-tools/api', componentKey: 'MaintainToolApi', parentId: 'maintain', groupId: 'maintain-tool', order: 3 },
  { id: 'maintain_tool_dic', title: '数据字典', path: '/pages/mall/admin/maintain/dev-tools/data-dict', componentKey: 'MaintainToolDic', parentId: 'maintain', groupId: 'maintain-tool', order: 4 },
  
  // 系统信息
  { id: 'maintain_sys_info', title: '系统信息', path: '/pages/mall/admin/maintain/sys/info', componentKey: 'MaintainSysInfo', parentId: 'maintain', groupId: 'maintain-sys', order: 1 }
]

/**
 * ============================================
 * 工具函数
 * ============================================
 */

/**
 * 获取所有一级菜单
 */
export function getTopMenus(): TopMenu[] {
  // 基于 role 的模块过滤
  return topMenus
    .filter(m => hasAdminModuleAccess(m.id))
    .sort((a, b) => a.order - b.order)
}

/**
 * 根据一级菜单ID获取其分组列表
 */
export function getGroupsByTopMenu(topMenuId: string): MenuGroup[] {
  const menu = topMenus.find(m => m.id === topMenuId)
  return menu ? menu.groups.sort((a, b) => (a.order || 0) - (b.order || 0)) : []
}

/**
 * 根据分组ID获取该分组下的路由列表
 */
export function getRoutesByGroup(groupId: string): RouteRecord[] {
  return routes
    .filter(r => r.groupId === groupId && !r.hidden)
    .sort((a, b) => (a.order || 0) - (b.order || 0))
}

/**
 * 根据一级菜单ID获取其所有子路由(分组后)
 */
export function getRoutesByTopMenu(topMenuId: string): Map<string, RouteRecord[]> {
  const groups = getGroupsByTopMenu(topMenuId)
  const result = new Map<string, RouteRecord[]>()
  
  groups.forEach(group => {
    result.set(group.id, getRoutesByGroup(group.id))
  })
  
  return result
}

/**
 * 根据路由ID查找路由记录
 */
export function findRouteById(routeId: string): RouteRecord | null {
  return routes.find(r => r.id === routeId) || null
}

/**
 * 根据路径查找路由记录
 */
export function findRouteByPath(path: string): RouteRecord | null {
  // 标准化路径: 去除查询参数和前导斜杠
  const normalizePath = (p: string): string => {
    let result = p.startsWith('/') ? p.slice(1) : p
    const queryIndex = result.indexOf('?')
    return queryIndex >= 0 ? result.slice(0, queryIndex) : result
  }
  
  const normalizedPath = normalizePath(path)
  return routes.find(r => normalizePath(r.path) === normalizedPath) || null
}

/**
 * 构建默认打开的标签页列表
 */
export function buildDefaultTabs(): RouteRecord[] {
  return routes.filter(r => r.isAffix)
}

/**
 * 获取路由的面包屑路径
 */
export function getBreadcrumb(routeId: string): Array<{id: string, title: string}> {
  const route = findRouteById(routeId)
  if (!route) return []
  
  const breadcrumb: Array<{id: string, title: string}> = []
  
  // 添加一级菜单
  if (route.parentId) {
    const topMenu = topMenus.find(m => m.id === route.parentId)
    if (topMenu) {
      breadcrumb.push({ id: topMenu.id, title: topMenu.title })
    }
  }
  
  // 添加当前路由
  breadcrumb.push({ id: route.id, title: route.title })
  
  return breadcrumb
}
