import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  list = '/school/school/list',
  search = '/school/school/search',
  save = '/school/school/add',
  edit = '/school/school/edit',
  deleteOne = '/school/school/delete',
  deleteBatch = '/school/school/deleteBatch',
  queryById = '/school/school/queryById',
  fullById = '/school/school/fullById',
  fullByMainCode = '/school/school/fullByMainCode',
  importExcel = '/school/school/importExcel',
  exportXls = '/school/school/exportXls',
}

export const getExportUrl = Api.exportXls;
export const getImportUrl = Api.importExcel;

export const list = (params) => defHttp.get({ url: Api.list, params });

export const search = (params) => defHttp.get({ url: Api.search, params });

export const listOrSearch = (params) => {
  if (params?.keyword) {
    return search(params);
  }
  // 避免将 keyword 透传给 /list
  const { keyword: _keyword, ...rest } = params || {};
  return list(rest);
};

export const queryById = (params) => defHttp.get({ url: Api.queryById, params });

export const fullById = (params) => defHttp.get({ url: Api.fullById, params });

export const fullByMainCode = (params) => defHttp.get({ url: Api.fullByMainCode, params });

export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess?.();
  });
};

export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess?.();
      });
    },
  });
};

export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url, params });
};
