// components/EnhancedProTable/types.ts
import type {
  ActionType,
  ProColumns,
  ProTableProps,
} from '@ant-design/pro-components';
import type { ButtonProps } from 'antd';
import React from 'react';

export interface BaseRecord {
  [key: string]: any;
}

export interface ApiResponse<T> {
  data: T[];
  total?: number;
  success: boolean;
  message?: string;
}

export interface TableDropdownMenuItem {
  key: string;
  name: string;
  label?: React.ReactNode;
  icon?: React.ReactNode;
  danger?: boolean;
  disabled?: boolean;
}

export interface TableAction<T = any> {
  key: string;
  label: string;
  icon?: React.ReactNode;
  type?: ButtonProps['type'];
  danger?: boolean;
  disabled?: (record: T) => boolean;
  visible?: (record: T) => boolean;
  onClick: (record: T, action?: ActionType) => void;
  permission?: string;
}

export interface ToolbarAction {
  key: string;
  label: string;
  icon?: React.ReactNode;
  type?: ButtonProps['type'];
  danger?: boolean;
  disabled?: boolean;
  onClick: (selectedKeys?: React.Key[], selectedRows?: any[]) => void;
  permission?: string;
  needSelection?: boolean;
}

export interface EnhancedProTableProps<T extends BaseRecord, U = any>
  extends Omit<ProTableProps<T, U>, 'columns' | 'request' | 'toolBarRender'> {
  columns: ProColumns<T>[];
  request?: (
    params: U & { current?: number; pageSize?: number },
  ) => Promise<ApiResponse<T>>;
  loading?: boolean; // 添加 loading 属性
  enableRowClick?: boolean; //开启行内点击
  clickableRowClassName?: string;
  actions?: TableAction<T>[];
  toolbarActions?: ToolbarAction[];
  permissions?: string[];
  checkPermission?: (permission: string) => boolean;
  showIndex?: boolean;
  showSelection?: boolean;
  showActions?: boolean;
  maxActionCount?: number;
  onAdd?: () => void;
  onEdit?: (record: T) => void;
  onDelete?: (record: T) => void;
  onView?: (record: T) => void;
  onExport?: (selectedRows: T[]) => void;
  customToolbarRender?: (
    defaultActions: React.ReactNode[],
  ) => React.ReactNode[];
  customActionRender?: (
    record: T,
    defaultActions: React.ReactNode[],
  ) => React.ReactNode[];
}

export interface UseEnhancedTableOptions<T> {
  onAdd?: () => void;
  onEdit?: (record: T) => void;
  onDelete?: (record: T) => Promise<boolean>;
  onView?: (record: T) => void;
  onExport?: (selectedRows: T[]) => void;
  onBatchDelete?: (selectedKeys: React.Key[]) => Promise<boolean>;
}

export interface UseEnhancedTableReturn<T> {
  actionRef: React.MutableRefObject<ActionType | undefined>;
  selectedRowKeys: React.Key[];
  selectedRows: T[];
  loading: boolean;
  setSelectedRowKeys: (keys: React.Key[]) => void;
  setSelectedRows: (rows: T[]) => void;
  reload: () => void;
  reset: () => void;
  clearSelection: () => void;
  handleDelete: (record: T) => Promise<void>;
  handleBatchDelete: (keys: React.Key[]) => Promise<void>;
  handleExport: (rows: T[]) => void;
  actions: {
    add?: () => void;
    edit?: (record: T) => void;
    delete: (record: T) => Promise<void>;
    view?: (record: T) => void;
    export: (rows: T[]) => void;
    batchDelete: (keys: React.Key[]) => Promise<void>;
  };
}
