import type { FormItemOptions, FormModelValue, FormProProps } from '@/components/formPro'
import type { DataTableColumn, DataTableInst, DataTableProps, DataTableRowKey, DataTableSlots } from 'naive-ui'
import type { Ref } from 'vue'

/**
 * 分页参数
 */
export interface PageParams<T extends FormModelValue = {}> {
  params: T & { [key: string]: any }
  page: {
    current: number
    size: number
  }
}

/**
 * 分页对象
 */
export interface Page {
  current: number
  size: number
  total: number
}

/**
 * 分页结果
 */
export interface PageResult<T> {
  current: string
  records: T[]
  size: string
  total: string
}

export type SearchFormProps<PARAMS> = Omit<FormProProps<PARAMS>, 'formItemOptions' | 'value' | 'onUpdate:value' | 'onUpdateValue'>

export type TableProProps<ROW extends BaseTableRowRecord = any, PARAMS extends FormModelValue = any> = RemoveProperty<DataTableProps, 'rowKey' | 'data' | 'pagination' | 'loading' | 'columns'> & {
  rowKey?: keyof ROW
  tableTitle?: string
  columns?: DataTableColumn<ROW>[]
  searchFormProps?: SearchFormProps<PARAMS>
  searchFormOptions: FormItemOptions<PARAMS>
  requestAuto?: boolean
  requestApi: (params: PARAMS | PageParams<PARAMS>) => Promise<JsonResult<ROW[] | PageResult<ROW>>>
  requestError?: (errorMsg: any) => void
  dataCallback?: (data: ROW[]) => ROW[]
  isPageTable: boolean
  pagination: {
    pageSizes: number[]
    pageSize: number
    pageSlot: number[]
  }
}

export type TableProSlots<ROW extends BaseTableRowRecord = any> = DataTableSlots & {
  headerExtra?: () => any
  selectionExtra: (scope: { selectKeys: DataTableRowKey[]; selectRows: ROW[] }) => any
}

export type TableProInst<T = Record<string, any>> = Omit<DataTableInst, 'filter' | 'clearFilter'> & {
  searchParams: Ref<FormModelValue>
  requestGetTableData: (needValidate: boolean) => void
  resetTable: () => void
  selectKeys: DataTableRowKey[]
  selectRows: T[]
  clearSelect: () => void
}
