import { Fn, ColumnProps, TableRowSelection, Pagination, TableCustomRecord } from '@/types/table'
import { propTypes } from '@/utils/propTypes'
import { PropType } from 'vue'
import type { ElButton } from "element-plus"
import { DEFAULT_FILTER_FN, DEFAULT_SORT_FN, FETCH_SETTING, DEFAULT_SIZE } from './const'
import { SorterResult } from "@/types/table"

type TableType = 'default' | 'selection' | 'index' | 'expand'



export const tableProps = {
  clickToRowSelect: { type: Boolean, default: true },
  inset: Boolean,
  showIndexColumn: Boolean,
  type: String as PropType<TableType>,
  sortFn: {
    type: Function as PropType<(sortInfo: SorterResult) => any>,
    default: DEFAULT_SORT_FN
  },
  filterFn: {
    type: Function as PropType<(data: Partial<Recordable<string[]>>) => any>,
    default: DEFAULT_FILTER_FN
  },
  schema: { type: Array as PropType<any[]>, default: [] },
  totalData: Object,

  showTableSetting: Boolean,
  striped: { type: Boolean, default: true },
  showSummary: Boolean,
  summaryFunc: {
    type: [Function, Array] as PropType<(...arg: any[]) => any[]>,
    default: null
  },
  summaryData: {
    type: Array as PropType<Recordable[]>,
    default: null
  },
  tableKey: {
    type: String as PropType<string>,
    default: ''
  },
  indentSize: propTypes.number.def(24),
  // 对齐方式
  align: propTypes.string
    .validate((v: string) => ['left', 'center', 'right'].includes(v))
    .def('center'),
  api: {
    type: Function as PropType<(...arg: any[]) => Promise<any>>,
    default: null
  },
  beforeFetch: {
    type: Function as PropType<Fn>,
    default: null
  },
  afterFetch: {
    type: Function as PropType<Fn>,
    default: null
  },
  searchInfo: {
    type: Object as PropType<Recordable>,
    default: () => { }
  },

  fetchSetting: {
    type: Object as PropType<FetchSetting>,
    default: () => {
      return FETCH_SETTING
    }
  },
  // 立即请求接口
  immediate: { type: Boolean, default: true },
  emptyDataIsShowTable: { type: Boolean, default: true },
  // 默认的排序参数
  defSort: {
    type: Object as PropType<Recordable>,
    default: null
  },
  stripe: {
    type: Boolean,
    default: false
  },

  columns: {
    type: Array as PropType<ColumnProps[]>,
    default: () => []
  },
  indexColumnProps: {
    type: Object as PropType<ColumnProps>,
    default: null
  },
  actionColumn: {
    type: Object as PropType<ColumnProps>,
    default: null
  },
  headerAlign: {
    type: String as PropType<'left' | 'center' | 'right'>,
    default: 'center'
  },
  title: {
    type: [String, Function] as PropType<string | ((data: Recordable) => string)>,
    default: null
  },
  titleHelpMessage: {
    type: [String, Array] as PropType<string | string[]>
  },
  maxHeight: propTypes.number,
  reserveSelection: propTypes.bool.def(false),
  dataSource: {
    type: Array as PropType<Recordable[]>,
    default: null
  },
  rowKey: {
    type: [String, Function] as PropType<string | ((record: Recordable) => string)>,
    default: ''
  },
  customRender: {
    type: Function as PropType<Fn>,
  },

  currentRowKey: {
    type: [String, Number],
    default: ""
  },
  rowSelection: {
    type: Object as PropType<TableRowSelection>,
    default: null
  },
  bordered: propTypes.bool,
  pagination: {
    type: [Object, Boolean] as PropType<Pagination | Boolean>,
    default: null
  },
  loading: propTypes.bool,
  rowClassName: {
    type: Function as PropType<(record: TableCustomRecord<any>, index: number) => string>
  },



}

export interface PopConfirm {
  title: string
  confirmButtonText?: string
  cancelButtonText?: string
  confirm: Fn
  cancel?: Fn
}

export interface ActionItem {
  onClick?: Fn
  label?: string
  type?: 'success' | 'danger' | 'warning' | 'info'
  popConfirm?: PopConfirm
  disabled?: boolean
  divider?: boolean
  // 权限编码控制是否显示
  auth?: string | string[]
  text?: string
  // 业务控制是否显示
  ifShow?: boolean | ((action: ActionItem) => boolean)
}

export interface FetchSetting {
  // 请求接口当前页数
  pageField: string
  // 每页显示多少条
  sizeField: string
  // 请求结果列表字段  支持 a.b.c
  listField: string
  // 请求结果总数字段  支持 a.b.c
  totalField: string
}