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 {
  title: string;
  apis: ContentApis | any;
  columns: any;
  notReuqiredInit?: boolean;
  notPagination?: boolean;
  notPreview?: boolean;
  statusProp?: string;
  deleteProp?: string;
  changePwdProp?: string;
  resetPwdProp?: string;
  logProp?: string;
  mockData?: Array<any>;
  isTree?: boolean;
  treeProps?: any;
  rowKey?: string;
  payProps?: Record<string, string>;
  returnProps?: Record<string, string>;
  delTooltip?: string;
  inv?: boolean;
  isRequesetArray?: boolean;
  openStatus?: string;
  stopStatus?: string;
  showPreview?: (row: Record<string, string>) => boolean;
  showDelete?: (row: Record<string, string>) => boolean;
  showExpand?: (row: Record<string, string>) => boolean;
}

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

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

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

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

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

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

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

export type PageContentProps = ExtractPropTypes<typeof pageContentProps>;
