import { WeaTools } from "ecCom";
import { postExportFetch, postFetch } from "../util/request";
import { convertToUrlString } from "../util/url";

//工资单-工资单发放列表
export const getPayrollList = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/list", params);
};
// 工资单发放进度条
export const getPayrollIssuanceProgressBar = (id) => {
  return WeaTools.callApi("/api/bs/hrmsalary/progress/getRate?cacheKey=SALARY_GRANT_PROGRESS_" + id, "get", {});
};

//工资单-获取table提示信息
export const getPayrollInfo = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/send/getBaseInfo", "get", params);
};

//工资单-获取工资单发放高级搜索
export const getPaySa = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/send/getInfoSearchCondition", "get", params);
};

//工资单-工资单发放
export const grantPayroll = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/grant", params);
};
//工资单-批量工资单发放
export const batGrantPayroll = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/batGrant", params);
};

//工资单-工资单撤回
export const withdrawPayroll = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/withdraw", params);
};
//工资单-批量工资单撤回
export const batWithdrawPayroll = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/batWithdraw", params);
};

//工资单-工资单发放详情列表
export const getPayrollDetailList = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/detailList", params);
};

//工资单-工资单发放详情列表的高级搜索
export const getPayrollDetailSa = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/getDetailSearchCondition", params);
};

//工资单-导出-工资单发放信息列表
export const exportPayroll = params => {
  fetch("/api/bs/hrmsalary/salaryBill/send/exportInfoList", {
    method: "POST",
    mode: "cors",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(params)
  }).then(res =>
    res.blob().then(blob => {
      var filename = `工资单发放.xlsx`;
      var a = document.createElement("a");
      var url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = filename;
      a.click();
      window.URL.revokeObjectURL(url);
    })
  );
};

//工资单-工资单模板列表
export const getPayrollTemplateList = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/list", params);
};

//工资单-获取薪资账套下拉列表
export const getPayrollTemplateLedgerList = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/template/selectSalarySobList", "POST", params);
};

//工资单-获取工资单基础设置表单
export const getPayrollBaseForm = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/template/getBaseForm", "get", params);
};

//工资单-获取工资单显示设置表单
export const getPayrollShowForm = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/template/getShowForm", "get", params);
};

//工资单-获取工资单薪资项目设置
export const getPayrollItemList = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/template/getSalaryItemSet", "get", params);
};

//工资单-更改默认使用
export const changePayrollDefaultUse = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/defaultUse", params);
};

//工资单-新建工资单
export const savePayroll = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/save", params);
};

//工资单-编辑工资单
export const updatePayroll = params => {
  const { background, ...extra } = params;
  return postFetch("/api/bs/hrmsalary/salaryBill/template/update", { background: _.isNil(background) ? "" : background, ...extra });
};

//工资单-复制工资单
export const duplicatePayroll = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/copy", params);
};

//工资单-删除工资单
export const deletePayroll = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/delete", params);
};

// 工资单发放-工资单发放信息列表
export const getInfoList = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/infoList", params);
};

// 工资单发放-导出-工资单发放详情列表
export const exportDetailList = params => {
  fetch("/api/bs/hrmsalary/salaryBill/send/exportDetailList", {
    method: "POST",
    mode: "cors",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify(params)
  }).then(res =>
    res.blob().then(blob => {
      var filename = `工资单发放详情列表.xlsx`;
      var a = document.createElement("a");
      var url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = filename;
      a.click();
      window.URL.revokeObjectURL(url);
    })
  );
};

// 工资单发放-工资单批量发放信息列表
export const batchSendInfoList = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/batchSendInfoList", params);
};

// 工资单发放-工资单批量撤回信息列表
export const batchWithdrawInfoList = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/batchWithdrawInfoList", params);
};

// 工资单发放-按钮状态
export const getSendBtnStatus = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/send/getSendBtnStatus", "get", params);
};

// 工资单发放-扩展按钮点击
export const grantProxy = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/grantProxy", params);
};

// 工资单发放-新建时获取补发工资单模板薪资项目设置
export const getReplenishForm = params => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/getReplenishForm", params);
};

// 工资单发放-获取规则设置下拉框
export const getReplenishRuleSetOptions = ({ salarySobId }) => {
  return fetch(`/api/bs/hrmsalary/salaryBill/template/getReplenishRuleSetOptions?salarySobId=${salarySobId}`, {
    method: "GET",
    mode: "cors",
    headers: {
      "Content-Type": "application/json"
    }
  }).then(res => res.json());
};

//添加发送和撤回人员范围列表
export const sendRangeList = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/range/list", params);
};
//创建范围
export const sendRangeSave = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/range/save", params);
};
//刪除范围
export const sendRangeDelete = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/range/delete", params);
};
//工资单模板-获取模板可用的分组
export const getAvailableSalaryGroupSet = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/getAvailableSalaryGroupSet", params);
};
//工资单模板-获取模板分组下可用的薪资项目
export const getAvailableSalaryItemSet = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/template/getAvailableSalaryItemSet", params);
};
//工资单发放-查看详情页面列表合计行数据
export const salaryBillSendSum = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/send/sum", params);
};
//工资单发放-发送短信验证码
export const sendMobileCode = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/sendMobileCode", params);
};
//工资单-验证方式
export const payrollCheckType = params => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/payrollCheckType", "GET", params);
};
//工资单-反馈验证
export const feedBackSalaryBill = async params => {
  const { header, ...payload } = params;
  const res = await fetch(`/api/bs/hrmsalary/salaryBill/feedBackSalaryBill?${convertToUrlString(payload)}`, {
    method: "GET",
    mode: "cors",
    headers: { "Content-Type": "application/json", ...header }
  });
  return await res.json();
  // return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/feedBackSalaryBill", "GET", params);
};
//工资单-确认
export const confirmSalaryBill = async params => {
  const { header, ...payload } = params;
  const res = await fetch(`/api/bs/hrmsalary/salaryBill/confirmSalaryBill?${convertToUrlString(payload)}`, {
    method: "GET",
    mode: "cors",
    headers: { "Content-Type": "application/json", ...header }
  });
  return await res.json();
  // return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/confirmSalaryBill", "GET", params);
};

// 工资单基础设置-获取设置列表
export const getSalaryBillBaseSetForm = (id) => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/baseSet/getForm", "get", {});
};
//工资单基础设置-保存工资单基础设置（设置水印）
export const salaryBillBaseSetSave = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/baseSet/save", params);
};
//工资单基础设置-保存工资单基础设置（水印预览）
export const salaryBillBaseSetPreviewWaterMark = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/baseSet/previewWaterMark", params);
};
// 工资单基础设置-工资单变量
export const getSmsSalaryItemSet = (params) => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/template/getSmsSalaryItemSet", "GET", params);
};
// 工资单导出PDF-检查
export const genPdfBeforeExport = (params) => {
  return WeaTools.callApi("/api/bs/hrmsalary/salaryBill/genPdfBeforeExport", "GET", params);
};
//工资单预览
export const salaryBillPreview = (params) => {
  return postFetch("/api/bs/hrmsalary/salaryBill/preview", params);
};
// 工资单查看详情导出-重构
export const exportDetailList_reconfig = (params) => {
  return postExportFetch("/api/bs/hrmsalary/salaryBill/send/exportDetailList", params);
};

