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

const glob = useGlobSetting();
const { createConfirm,createWarningModal,createMessage } = useMessage();

enum Api {
  list = '/baosong/baosongTpl/list',
  save='/baosong/baosongTpl/add',
  edit='/baosong/baosongTpl/edit',
  deleteOne = '/baosong/baosongTpl/delete',
  deleteBatch = '/baosong/baosongTpl/deleteBatch',
  importExcel = '/baosong/baosongTpl/importExcel',
  exportXls = '/baosong/baosongTpl/exportXls',
  importXml = '/baosong/baosongTpl/importXml',
  allTpls = '/baosong/baosongTpl/allTpls',
  importTplItemDes = '/baosong/baosongTpl/importTplItemDes',
  
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 导入api xml
 */
export const getImportXmlUrl = Api.importXml;

/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({url: Api.list, params});

export const allTpls = (params) =>
  defHttp.get({url: Api.allTpls, 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 importTplByXml = async(data,row, success)=> {
  
  let url =  Api.importXml
  const isReturn = (fileInfo) => {
    try {
      if (fileInfo.code === 201) {
        let { message, result: { msg, fileUrl, fileName }} = fileInfo;
        let href = glob.uploadUrl + fileUrl;
        createWarningModal({ title: message,centered: false,
          content: `<div><span>${msg}</span><br/><span>具体详情请<a href = ${href} download = ${fileName}> 点击下载 </a> </span></div>`,
        });
      } else if (fileInfo.code === 500) {
        createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
      } else {
        createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
      }
    } catch (error) {
      console.log('导入的数据异常', error);
    } finally {
      typeof success === 'function' ? success(fileInfo) : '';
    }
  };
  
   await defHttp.uploadFile({ url }, {file:data.file,data:row}, { success: isReturn });
}


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



