import request from "@/utils/request";

// 创建出货
export function createbox(data) {
  return request({
    url: "/shipment/box/create",
    method: "post",
    data: data,
  });
}

// 更新出货
export function updatebox(data) {
  return request({
    url: "/shipment/box/update",
    method: "put",
    data: data,
  });
}

// 删除出货
export function deletebox(id) {
  return request({
    url: "/shipment/box/delete?id=" + id,
    method: "delete",
  });
}

// 获得出货
export function getbox(id) {
  return request({
    url: "/shipment/box/get?id=" + id,
    method: "get",
  });
}

// 获得出货分页
export function getboxPage(query) {
  return request({
    url: "/shipment/box/page",
    method: "get",
    params: query,
  });
}

// 导出出货 Excel
export function exportboxExcel(query) {
  return request({
    url: "/shipment/box/export-excel",
    method: "get",
    params: query,
    responseType: "blob",
  });
}

// 创建费用登记
export function createCost(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-cost/update",
      method: "put",
      data,
    });
  }

  return request({
    url: "/ecw/box-cost/create",
    method: "post",
    data,
  });
}

// 删除费用登记
export function deleteCost(id) {
  return request({
    url: `/ecw/box-cost/delete?id=${id}`,
    method: "delete",
  });
}

// 审核详情
export function approvalDetail(data) {
  return request({
    url: "/ecw/box-approval/approvalDetail",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

// 出货操作日志列表
export function getLogList(params) {
  return request({
    url: "/ecw/box-op-log/list",
    method: "get",
    params,
  });
}

// 获得费用登记列表
export function getCostList(params) {
  return request({
    url: "/ecw/box-cost/list",
    method: "get",
    params,
  });
}

// 获得出货异常记录列表
export function getAbnormalList(params) {
  return request({
    url: "/ecw/box-abnormal/list",
    method: "get",
    params,
  });
}

// 获得制作提货单列表
export function getMakeBillList(params) {
  return request({
    url: "/shipment/make-bill-of-lading/getMakeLadingBillList",
    method: "get",
    params,
  });
}

// 制作提货单
export function makeBillService(params) {
  return request({
    url: "/shipment/make-bill-of-lading/make",
    method: "get",
    params,
  });
}

// 创建制作提货单
export function createBillService(data) {
  return request({
    url: "/shipment/make-bill-of-lading/create",
    method: "post",
    data,
  });
}

// 取消制作提货单审核
export function cancelBillService(id) {
  return request({
    url: `/shipment/make-bill-of-lading/cancel?id=${id}`,
    method: "delete",
  });
}

// 下载提货单
export function downloadBillService(params) {
  return request({
    url: "/shipment/make-bill-of-lading/download",
    responseType: "blob",
    method: "get",
    params,
  });
}

// 更新制作提货单
export function updateBillService(data) {
  return request({
    url: "/shipment/make-bill-of-lading/update",
    method: "put",
    data,
  });
}

// 删除制作提货单
export function deleteBillService(id) {
  return request({
    url: `/shipment/make-bill-of-lading/delete?id=${id}`,
    method: "delete",
  });
}

// 获得制作提货单
export function getBillService(params) {
  return request({
    url: "/shipment/make-bill-of-lading/get",
    method: "get",
    params,
  });
}

// 打包下载提货单
export function zipDownload(params) {
  return request({
    url: "/shipment/make-bill-of-lading/zipDownload",
    responseType: "blob",
    method: "get",
    params,
  });
}

/**
 * formData数据
 *
 * @param {*} params
 * @return {*}
 */
function jsonToFormData(params) {
  const formData = new FormData();
  for (const [key, value] of Object.entries(params)) {
    formData.append(key, value);
  }
  return formData;
}

// 创建制作提货单
export function getNoticeList(data) {
  return request({
    url: "/shipment/box/noticeList",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/**
 * 根据订单ID下载提货单
 *
 * @export
 * @param {*} orderId
 * @return {*}
 */
export function downloadByOrderId(orderId) {
  return request({
    url: "/shipment/make-bill-of-lading/downloadByOrderId",
    method: "get",
    params: { orderId },
    responseType: "arraybuffer",
  });
}

// 获得出货审核
export function getBoxApproval(params) {
  return request({
    url: "/ecw/box-approval/get",
    method: "get",
    params,
  });
}

// 获得制作提货单（审核中调用）
export function getBillOfLandingInProcessing(params){
  return request({
    url: "/shipment/make-bill-of-lading/getInProcessing",
    method: "get",
    params,
  });
}
// 下载agent list文件
export function downloadAgentListFiles(params) {
  return request({
    url: "/ecw/box-preload-goods/downloadAgentListFiles",
    method: "get",
    params,
  });
}

// 下载son cap文件
export function downloadSoncapFiles(params) {
  return request({
    url: "/ecw/box-preload-goods/downloadSoncapFiles",
    method: "get",
    params,
  });
}

// 下载报关单
export function downloadCustomFiles(params) {
  return request({
    url: "/ecw/box-preload-goods/downloadCustomFiles",
    method: "get",
    params,
  });
}

// 下载已装单
export function downloadLoadGoodsList(params) {
  return request({
    url: "/ecw/box-preload-goods/downloadLoadGoodsList",
    responseType: "blob",
    method: "get",
    params,
  });
}

// 下载预装单
export function downloadPreloadGoodsList(params) {
  return request({
    url: "/ecw/box-preload-goods/downloadPreloadGoodsList",
    responseType: "blob",
    method: "get",
    params,
  });
}

// 下载应收汇总
export function downloadReceivableList(params) {
  return request({
    url: "/ecw/box-preload-goods/downloadReceivableList",
    responseType: "blob",
    method: "get",
    params,
  });
}

// 下载提单copy
export function downloadLadingCopy(params) {
  return request({
    url: "/ecw/box-lading-copy/downloadLadingCopy",
    method: "get",
    params,
  });
}
