import type { BasicFetchResult, BasicPageParams } from '#/api/model';

import { requestClient } from '#/api/request';

export namespace TableApi {
  export interface PageParams extends BasicPageParams {
    name?: string;
  }

  export interface BasicRecordItem {
    tableType: string;
    name: string;
    code: string;
    config: {
      buttons: any[];
      columns: any[];
      search: any[];
    };
    databaseQueryId: number;
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export type PageResult = BasicFetchResult<RecordItem>;

  export interface ExecuteParams extends BasicPageParams {
    code: string;
    formData: any;
  }

  export const getPage = (params: PageParams) =>
    requestClient.get<PageResult>('/table/query/page', { params });

  export const getDetail = (id: number) =>
    requestClient.get<RecordItem>('/table/query/entity', {
      params: { id },
    });

  export const getInfo = (code: string) =>
    requestClient.get<RecordItem>('/table/query/info', {
      params: { code },
    });
  export const addDetail = (data: BasicRecordItem) =>
    requestClient.post('/table/query', data);

  export const editDetail = (data: RecordItem) =>
    requestClient.put('/table/query', data);

  export const deleteDetail = (id: number) =>
    requestClient.delete('/table/query', { data: { id } });

  export const postExecute = (data: ExecuteParams) =>
    requestClient.post('/table/query/execute', data);
}
