// 角色管理相关类型定义

// API返回的角色数据类型
export interface RoleApiData {
  id: string;
  name: string;
  description?: string;
  tenant_id: string;
  created_at: string;
  updated_at: string;
}

// 页面使用的角色数据类型（兼容原有功能）
export interface Role {
  id: string;
  name: string;
  code: string;
  description?: string;
  type: RoleType;
  menuIds: string[];
  permissionIds: string[];
  defaultHomePage?: string;
  status: 'active' | 'inactive';
  createdAt: string;
  updatedAt: string;
  tenant_id?: string;
}

export type RoleType = 'system' | 'custom';

// 定义菜单权限结构（菜单与操作权限关联）
export interface MenuWithPermissions {
  id: string;
  label: string;
  children?: {
    id: string;
    label: string;
    operations: {
      id: string;
      label: string;
      key: string;
    }[];
  }[];
}

// 所有系统菜单及其操作权限
export const allSystemMenus: { id: string; label: string; menus: MenuWithPermissions[] }[] = [
  {
    id: 'machinery',
    label: '智能农机管理系统',
    menus: [
      {
        id: 'machinery-archive',
        label: '农机档案',
        children: [
          {
            id: 'machinery-entry',
            label: '农机档案录入与维护',
            operations: [
              { id: 'machinery-entry:view', label: '查看', key: 'view' },
              { id: 'machinery-entry:add', label: '新增', key: 'add' },
              { id: 'machinery-entry:edit', label: '编辑', key: 'edit' },
              { id: 'machinery-entry:delete', label: '删除', key: 'delete' },
              { id: 'machinery-entry:export', label: '导出', key: 'export' },
            ],
          },
          {
            id: 'machinery-classification',
            label: '农机分类与标签管理',
            operations: [
              { id: 'machinery-classification:view', label: '查看', key: 'view' },
              { id: 'machinery-classification:edit', label: '编辑', key: 'edit' },
            ],
          },
          {
            id: 'machinery-qrcode',
            label: '农机二维码管理',
            operations: [
              { id: 'machinery-qrcode:view', label: '查看', key: 'view' },
              { id: 'machinery-qrcode:generate', label: '生成', key: 'generate' },
              { id: 'machinery-qrcode:print', label: '打印', key: 'print' },
            ],
          },
        ],
      },
      {
        id: 'driver-archive',
        label: '驾驶员档案',
        children: [
          {
            id: 'driver-info',
            label: '驾驶员信息管理',
            operations: [
              { id: 'driver-info:view', label: '查看', key: 'view' },
              { id: 'driver-info:add', label: '新增', key: 'add' },
              { id: 'driver-info:edit', label: '编辑', key: 'edit' },
              { id: 'driver-info:delete', label: '删除', key: 'delete' },
            ],
          },
          {
            id: 'driver-task',
            label: '驾驶员任务管理',
            operations: [
              { id: 'driver-task:view', label: '查看', key: 'view' },
              { id: 'driver-task:assign', label: '分配任务', key: 'assign' },
              { id: 'driver-task:cancel', label: '取消任务', key: 'cancel' },
            ],
          },
        ],
      },
      {
        id: 'monitoring',
        label: '设备实时监控与定位',
        children: [
          {
            id: 'realtime-location',
            label: '实时位置追踪',
            operations: [
              { id: 'realtime-location:view', label: '查看', key: 'view' },
              { id: 'realtime-location:track', label: '追踪', key: 'track' },
            ],
          },
          {
            id: 'work-status',
            label: '工作状态监控',
            operations: [
              { id: 'work-status:view', label: '查看', key: 'view' },
              { id: 'work-status:control', label: '远程控制', key: 'control' },
            ],
          },
        ],
      },
      {
        id: 'fault-diagnosis',
        label: '远程诊断与故障预警',
        children: [
          {
            id: 'fault-warning',
            label: '故障诊断与预警',
            operations: [
              { id: 'fault-warning:view', label: '查看', key: 'view' },
              { id: 'fault-warning:handle', label: '处理', key: 'handle' },
              { id: 'fault-warning:export', label: '导出', key: 'export' },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 'field',
    label: '地块信息管理系统',
    menus: [
      {
        id: 'field-info',
        label: '地块信息',
        children: [
          {
            id: 'field-list',
            label: '地块列表',
            operations: [
              { id: 'field-list:view', label: '查看', key: 'view' },
              { id: 'field-list:add', label: '新增', key: 'add' },
              { id: 'field-list:edit', label: '编辑', key: 'edit' },
              { id: 'field-list:delete', label: '删除', key: 'delete' },
            ],
          },
          {
            id: 'field-map',
            label: '地块地图',
            operations: [
              { id: 'field-map:view', label: '查看', key: 'view' },
              { id: 'field-map:draw', label: '绘制', key: 'draw' },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 'operation',
    label: '农事操作管理系统',
    menus: [
      {
        id: 'operation-plan',
        label: '作业计划',
        children: [
          {
            id: 'plan-list',
            label: '计划列表',
            operations: [
              { id: 'plan-list:view', label: '查看', key: 'view' },
              { id: 'plan-list:add', label: '新增', key: 'add' },
              { id: 'plan-list:edit', label: '编辑', key: 'edit' },
              { id: 'plan-list:delete', label: '删除', key: 'delete' },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 'asset',
    label: '农业资产管理系统',
    menus: [
      {
        id: 'asset-inventory',
        label: '资产清单',
        children: [
          {
            id: 'inventory-list',
            label: '清单列表',
            operations: [
              { id: 'inventory-list:view', label: '查看', key: 'view' },
              { id: 'inventory-list:manage', label: '管理', key: 'manage' },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 'ai-model',
    label: 'AI作物模型精准决策系统',
    menus: [
      {
        id: 'ai-models',
        label: 'AI模型',
        children: [
          {
            id: 'model-list',
            label: '模型列表',
            operations: [
              { id: 'model-list:view', label: '查看', key: 'view' },
              { id: 'model-list:run', label: '运行', key: 'run' },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 'irrigation',
    label: '水肥一体化控制系统',
    menus: [
      {
        id: 'irrigation-control',
        label: '灌溉控制',
        children: [
          {
            id: 'zone-control',
            label: '区域控制',
            operations: [
              { id: 'zone-control:view', label: '查看', key: 'view' },
              { id: 'zone-control:control', label: '控制', key: 'control' },
            ],
          },
        ],
      },
    ],
  },
  {
    id: 'config',
    label: '中心配置管理系统',
    menus: [
      {
        id: 'tenant-management',
        label: '租户管理',
        children: [
          {
            id: 'enterprise-audit',
            label: '企业审核',
            operations: [
              { id: 'enterprise-audit:view', label: '查看', key: 'view' },
              { id: 'enterprise-audit:audit', label: '审核', key: 'audit' },
            ],
          },
          {
            id: 'enterprise-info',
            label: '企业信息',
            operations: [
              { id: 'enterprise-info:view', label: '查看', key: 'view' },
              { id: 'enterprise-info:edit', label: '编辑', key: 'edit' },
            ],
          },
        ],
      },
      {
        id: 'user-management',
        label: '用户管理',
        children: [
          {
            id: 'employee-management',
            label: '员工管理',
            operations: [
              { id: 'employee-management:view', label: '查看', key: 'view' },
              { id: 'employee-management:add', label: '新增', key: 'add' },
              { id: 'employee-management:edit', label: '编辑', key: 'edit' },
              { id: 'employee-management:delete', label: '删除', key: 'delete' },
            ],
          },
          {
            id: 'role-management',
            label: '角色管理',
            operations: [
              { id: 'role-management:view', label: '查看', key: 'view' },
              { id: 'role-management:manage', label: '管理', key: 'manage' },
            ],
          },
        ],
      },
      {
        id: 'system-params',
        label: '系统参数',
        children: [
          {
            id: 'system-settings',
            label: '系统设置',
            operations: [
              { id: 'system-settings:view', label: '查看', key: 'view' },
              { id: 'system-settings:edit', label: '编辑', key: 'edit' },
            ],
          },
        ],
      },
      {
        id: 'system-monitor',
        label: '系统监控',
        children: [
          {
            id: 'login-log',
            label: '登录日志',
            operations: [
              { id: 'login-log:view', label: '查看', key: 'view' },
              { id: 'login-log:export', label: '导出', key: 'export' },
            ],
          },
          {
            id: 'operation-log',
            label: '操作日志',
            operations: [
              { id: 'operation-log:view', label: '查看', key: 'view' },
              { id: 'operation-log:export', label: '导出', key: 'export' },
            ],
          },
        ],
      },
    ],
  },
];

// 统计数据
export interface RoleManagementStats {
  label: string;
  value: number;
  color: string;
  bg: string;
}

// API响应接口
export interface RolesApiResponse {
  data: RoleApiData[];
  total: number;
  page: number;
  size: number;
  total_pages: number;
  has_next: boolean;
  has_prev: boolean;
}

// 查询参数接口
export interface RolesQueryParams {
  page?: number;
  size?: number;
  sort_order?: 'asc' | 'desc';
}

// 翻页状态
export interface PaginationState {
  page: number;
  size: number;
  total: number;
  totalPages: number;
  hasNext: boolean;
  hasPrev: boolean;
}

// 筛选条件
export interface RoleFilters {
  searchKeyword: string;
}

// 表单数据
export interface RoleFormData {
  name?: string;
  code?: string;
  description?: string;
  type?: RoleType;
  status?: 'active' | 'inactive';
  defaultHomePage?: string;
  menuIds?: string[];
  permissionIds?: string[];
}