/**
 * 财务应付明细-API
 */
import { Idparams, ResultInterface } from "@/api/base/BaseDataModel";
import http from "@/http/http";
import { UploadFileParams } from "@/types/utils";
import { cloneDeep } from "lodash";
import {
  BillShareParam,
  FinancialBillDetailShareDto,
  FinancialBillDetailShareParam,
  FinancialBillSummaryDto,
  FinancialBillSummaryParam,
  PartialFinancialBillSummaryParam,
  UpdateStateParams,
  uploadFileResult,
} from "./financialBillListModel";

enum Api {
  financialBillSummaryList = "/api/financialBillSummary/pageList",
  financialBillSummaryDelete = "/api/financialBillSummary",
  getFinancialBillSummaryByPId = "/api/financialBillSummary/getFinancialBillSummaryByPId",
  addBill = "/api/financialBillSummary/add",
  editBill = "/api/financialBillSummary/edit",
  uploadFile = "/api/financialBillSummary/uploadFile",
  updateStateBill = "/api/financialBillSummary/updateState",
  downloadBill = "/api/financialBillSummary/download",
  getFinancialBillSummaryVerify = "/api/financialBillSummary/verify",
  share = "/api/financialBillSummary/share",
  financialBillDetailShareShareList = "/api/FinancialBillDetailShareShare/pageList",
}

/**
 * 财务应付账单分页查询
 * @param parms
 * @returns
 */
export const api_financialBillSummaryList = async (
  parms: PartialFinancialBillSummaryParam
) => {
  return await http.post(Api.financialBillSummaryList, parms);
};

/**
 * 删除财务应付账单
 * @param parms
 * @returns
 */
export const api_financialBillSummaryDelete = async (parms: Idparams) => {
  return await http.delete(Api.financialBillSummaryDelete, parms);
};

/**
 * 根据单号查询应付账单
 * @param parms
 * @returns
 */
export const api_getFinancialBillSummaryByPId = async (parms: Idparams) => {
  return await http.get(Api.getFinancialBillSummaryByPId, parms);
};

/**
 * 新增财务应付账单
 * @param parms
 * @returns
 */
export const api_financialBillSummaryAdd = async (
  parms: Partial<FinancialBillSummaryDto>
) => {
  const cloneParms = cloneDeep(parms);
  delete cloneParms.uploadFile;
  const data: UploadFileParams = {
    name: "files",
    file: parms.uploadFile as unknown as File,
    data: {
      ...cloneParms,
    },
  };
  return await http.uploadFile(Api.addBill, data);
};

/**
 * 编辑财务应付账单
 * @param parms
 * @returns
 */
export const api_financialBillSummaryEdit = async (
  parms: Partial<FinancialBillSummaryDto>
) => {
  const cloneParms = cloneDeep(parms);
  delete cloneParms.uploadFile;
  const data: UploadFileParams = {
    name: "files",
    file: parms.uploadFile as unknown as File,
    data: {
      ...cloneParms,
    },
  };
  return await http.uploadFile(Api.editBill, data);
};

/**
 * 关账操作
 * @param parms
 * @returns
 */
export const api_financialBillSummaryUpdateState = async (
  parms: UpdateStateParams
) => {
  return await http.post(Api.updateStateBill, parms);
};

/**
 * 账单上传文件
 *  billFileType 文件类型说明
    OriginalFile (0,"原始文件"),
    BillFile (1,"账单文件");
 */
export const api_financialBillSummaryUploadApi = async (
  params: UploadFileParams
) => {
  return await http.uploadFile<uploadFileResult>(Api.uploadFile, params);
};

/**
 * 账单模版下载
 */
export const api_financialBillSummaryDownload = async (parms?: Recordable) => {
  return await http.download(Api.downloadBill, parms);
};

/**
 * 账单核账
 */
export const api_getFinancialBillSummaryVerify = async (parms: Idparams) => {
  return await http.get(Api.getFinancialBillSummaryVerify, parms);
};

/**
 * 账单分摊
 */
export const api_getFinancialBillSummaryVerifyShare = async (
  parms: BillShareParam
) => {
  return await http.post(Api.share, parms);
};

/**
 * 财务账单分摊明细
 * @param parms
 * @returns
 */
export const api_getFinancialBillDetailShareShareList = async (
  parms: FinancialBillDetailShareParam
) => {
  return await http.post<ResultInterface<FinancialBillDetailShareDto>>(Api.financialBillDetailShareShareList, parms);
};
