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: "arraybuffer",
    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: "arraybuffer",
    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;
}
