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

const { createConfirm } = useMessage();

enum Api {
  list = '/wb.manage/wbCompanyInfo/list',
  save='/wb.manage/wbCompanyInfo/add',
  edit='/wb.manage/wbCompanyInfo/edit',
  deleteOne = '/wb.manage/wbCompanyInfo/delete',
  deleteBatch = '/wb.manage/wbCompanyInfo/deleteBatch',
  importExcel = '/wb.manage/wbCompanyInfo/importExcel',
  exportXls = '/wb.manage/wbCompanyInfo/exportXls',

  checkCMMI =  '/wb.manage/wbCompanyInfo/checkCMMI',
  exportPdf = '/wb.manage/wbCompanyInfo/exportPdf',

  pullBatchDataFromAPI = '/wb.manage/wbCompanyInfo/pullBatchDataFromAPI',
  queryDataFromAPI = '/wb.manage/wbCompanyInfo/queryDataFromAPI',


}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({url: Api.list, params});

/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}
/**
 * 批量删除
 * @param params
 */
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();
      });
    }
  });
}
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}


export const testRemote = () => {
  let url = "https://www.cmmiinstitute.com/pars?StateId=033eec20-8497-4ff0-bf7b-c48fdcd3d67d&PageNumber=1&Handler=ApplyFilters"
  let params ={
    //"Filters.AppraisalSearch":"65010"
  }

  return defHttp.get({url: url, params});
}



export const checkCMMI = (params) => {
  let url = Api.checkCMMI
  return defHttp.post({url: url, params});
}

export const exportPdf = (params) =>{
  let options = {
    isTransformResponse:false, 
    isReturnNativeResponse:true
  }

  return defHttp.get({url: Api.exportPdf, params, responseType:'blob',},options)
  .then((response) => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download',params.name +".pdf");
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  })
  .catch((error) => {
    console.error('Failed to download pdf file:', error);
  });
}


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

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






