import { TableColumnCtx } from "element-plus";
import { PropType, ExtractPropTypes } from "vue";
import PageContent from "./page-content";

/***
 * @param title 标题
 * @param apis 请求
 * @param columns 列数据
 * @param notReuqiredInit 首次加载是否需要请求列表
 * @param notPagination 不需要分页器
 * @param mockData 模拟数据
 * @param isTree 是否为tree
 * @param powers 功能权限
 * @param treeProps
 */
export interface ContentConfig {
  all?: boolean;
  columns: any;
  root?: boolean;
  title?: string;
  maxHeight?: string;
  showTitle?: boolean;
  apis: ContentApis | any;
  notPagination?: boolean;
  notPreview?: boolean;
  statusProp?: string;
  deleteProp?: any;
  isTree?: boolean;
  treeProps?: any;
  rowKey?: string;
  delTooltip?: string;
  inv?: boolean;
  isRequestArray?: boolean;
  openStatus?: string;
  stopStatus?: string;
  notNeedCompanyFilter?: boolean;
  companyProp?: string;
  superUserNoAction?: boolean;
  superUserNoPerview?: boolean;
  listNoRelation?: boolean;
  beforeRequestList?: (...rest: any[]) => BeforeRequestType;
  statusRowProp?: (row: any) => any;
  showStatus?: (isSuperUser: boolean) => boolean;
  showCreate?: (isSuperUser: boolean) => boolean;
  handleData?: (data: any, params?: any) => any;
  showPreview?: (row: Record<string, string>) => boolean;
  showDelete?: (row: Record<string, string>) => boolean;
  showExpand?: (row: Record<string, string>) => boolean;
  tableRowClassName?: (...rest: any[]) => any;
  responseCode?: number;
  noRequiredInit?: boolean;
  spanMethod?: (
    data: {
      row: any;
      rowIndex: number;
      column: TableColumnCtx<any>;
      columnIndex: number;
    },
    tableData: any
  ) =>
    | number[]
    | {
        rowspan: number;
        colspan: number;
      };
}

interface ContentApiResponse {
  message: string;
  code: number;
  data: any;
}

type API = (data: any) => Promise<ContentApiResponse>;

export type ActionType = "update" | "delete" | "create" | "preview" | "status";

export interface ContentApis {
  httpAdd?: API;
  httpList: API;
  httpDelete?: API;
  httpUpdate?: API;
  httpStatus?: API;
}

export type PageContentInstance = typeof PageContent & {
  getPageData: (params?: any) => void;
  onBeforeAction: (
    type: ActionType,
    data?: any,
    responseCallback?: () => void
  ) => void;
  onSearch: () => void;
  getBasicParams: () => any;
  getData: () => any;
};

export enum BeforeRequestType {
  NEXT,
  EMPTY,
  BREAK
}

export const pageContentProps = {
  lockKey: {
    type: [String, Array] as PropType<String | Array<String>>,
    default: ""
  },
  contentConfig: {
    type: Object as PropType<ContentConfig>,
    required: true
  },
  onGetReuslt: {
    type: Function
  },
  onGetHandleResult: {
    type: Function
  },
  pageSize: {
    type: Number,
    default: 15
  },
  powers: {
    type: Array as PropType<string[]>,
    default: () => []
  }
} as const;

export const actionProps = {
  config: {
    type: Object as PropType<{ api: API }>
  },
  isRequestArray: {
    type: Boolean
  },
  statusConfig: {
    type: Object
  }
};

export type PageContentProps = ExtractPropTypes<typeof pageContentProps>;
