import request from "@/utils/request";

/**
 * 订舱
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function booking(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-book-sea/update",
      method: "put",
      data,
    });
  }

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

/**
 * 拖车
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function trailer(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-trailer/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-trailer/create",
    method: "post",
    data,
  });
}

/**
 * agent
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function agentSet(data) {
  return request({
    url: "/shipment/box/agentSet",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/**
 * 装柜
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function cabinetCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-cabinet/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-cabinet/create",
    method: "post",
    data,
  });
}

/**
 * 报关
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function customsCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-customs/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-customs/create",
    method: "post",
    data,
  });
}

/**
 * 配船
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function shipConfigure(data) {
  return request({
    url: "/shipment/box/shipConfigure",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/**
 * 提单补料
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function billCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-lading-bill/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-lading-bill/create",
    method: "post",
    data,
  });
}

/**
 * 驳船
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function bargeCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-barge/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-barge/create",
    method: "post",
    data,
  });
}

/**
 * 起运
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function shippingCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-shipping/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-shipping/create",
    method: "post",
    data,
  });
}

/**
 * 提单copy
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function ladingCopyCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-lading-copy/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-lading-copy/create",
    method: "post",
    data,
  });
}

/**
 * 清关文件
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function clearanceDocCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-clearance-doc/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-clearance-doc/create",
    method: "post",
    data,
  });
}

/**
 * 到港
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function arrivalCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-arrival/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-arrival/create",
    method: "post",
    data,
  });
}

/**
 * 清关
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function clearanceCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-clearance/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-clearance/create",
    method: "post",
    data,
  });
}

/**
 * 卸柜
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function unloadCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-cabinet-unload/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-cabinet-unload/create",
    method: "post",
    data,
  });
}

/**
 * 结算
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function settlementCreate(data) {
  if (data.id) {
    return request({
      url: "/ecw/box-settlement/update",
      method: "put",
      data,
    });
  }
  return request({
    url: "/ecw/box-settlement/create",
    method: "post",
    data,
  });
}

/**
 * 出货审核
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function approvalCreate(data) {
  return request({
    url: "/ecw/box-approval/create",
    method: "post",
    data,
  });
}

/**
 * 取消审核
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function approvalCancel(data) {
  return request({
    url: `/ecw/box-approval/cancel`,
    method: "delete",
    data,
  });
}

/**
 * 异常登记
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function abnormalCreate(data) {
  return request({
    url: "/ecw/box-abnormal/create",
    method: "post",
    data,
  });
}

/*****************************  预装 start **********************************/

/**
 * 获得已预装部分列表
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function secGoodsList(data) {
  return request({
    url: "/ecw/box-preload-goods/secGoodsList",
    method: "post",
    data,
  });
}

/**
 * 获得预装分页
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function preloadPage(data) {
  return request({
    url: "/ecw/box-preload-goods/preloadPage",
    method: "post",
    data,
  });
}

/**
 * 创建预装部分
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function createSection(data) {
  return request({
    url: "/ecw/box-preload-section/create",
    method: "post",
    data,
  });
}

/**
 * 创建预装部分
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function changeSection(data) {
  return request({
    url: "/ecw/box-preload-goods/changeSection",
    method: "post",
    data,
  });
}

/**
 * 查询出货单下的所有部分列表
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function getSectionList(params) {
  return request({
    url: "/ecw/box-preload-section/list",
    method: "get",
    params,
  });
}

/**
 * 装柜订单明细
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function boxGoodsDetail(data) {
  return request({
    url: "/ecw/box-preload-goods/boxGoodsDetail",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/**
 * 删除预装部分
 *
 * @export
 * @param {*} id
 * @return {*}
 */
export function deleteSection(id) {
  return request({
    url: "/ecw/box-preload-goods/deleteSection?id=" + id,
    method: "delete",
  });
}

/**
 * 删除预装货物
 *
 * @export
 * @param {*} id
 * @return {*}
 */
export function deleteGoods(id) {
  return request({
    url: "/ecw/box-preload-goods/delete?ids=" + id.join(","),
    method: "delete",
  });
}

/**
 * 预装
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function createGoods(data) {
  return request({
    url: "/ecw/box-preload-goods/create",
    method: "post",
    data,
  });
}

/*****************************  预装 end **********************************/

/*****************************  装柜 start **********************************/

/**
 * 获得装柜部分列表
 *
 * @export
 * @param {*} params
 * @return {*}
 */
export function loadSecGoodsList(params) {
  return request({
    url: "/ecw/box-preload-goods/loadSecGoodsList",
    method: "get",
    params,
  });
}

/**
 * 订单装箱编号列表
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function orderTagList(data) {
  return request({
    url: "/ecw/box-load-info/orderTagList",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/**
 * 单个装柜
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function singleCreate(data) {
  return request({
    url: "/ecw/box-load-info/singleCreate",
    method: "post",
    data,
  });
}

/**
 * 批量装柜
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function batchCreate(data) {
  return request({
    url: "/ecw/box-load-info/batchCreate",
    method: "post",
    data,
  });
}

/**
 * 单个删除已装柜标签
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function singleDelete(data) {
  return request({
    url: "/ecw/box-load-info/singleDelete",
    method: "post",
    data,
  });
}

/**
 * 批量删除已装柜标签
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function batchDelete(data) {
  return request({
    url: "/ecw/box-load-info/batchDelete",
    method: "post",
    data,
  });
}

/**
 * 移出
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function remove(data) {
  return request({
    url: "/ecw/box-load-info/remove",
    method: "post",
    data,
  });
}

/**
 * 修改柜信息
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function boxUpdate(data) {
  return request({
    url: "/ecw/box-load-info/boxUpdate",
    method: "post",
    data,
  });
}

/**
 * 拆单审核
 *
 * @param {*} params
 * @returns
 */
export function createApproval(data) {
  return request({
    url: "/ecw/box-approval/create",
    method: "post",
    data,
  });
}

/**
 * 订单详情
 *
 * @param {*} params
 * @returns
 */
export function getOrderDetailByBoxNo(data) {
  return request({
    url: "/my/shipment/searchLoadOrderByBoxNo",
    method: "post",
    data,
  });
}

/**
 * 外部仓装柜
 *
 * @param {*} params
 * @returns
 */
export function externalLoad(data) {
  return request({
    url: "/ecw/box-load-info/externalLoad",
    method: "post",
    data,
  });
}

/**
 * 获取拆单列表
 *
 * @param {*} params
 * @returns
 */
export function splitList(data) {
  return request({
    url: "/shipment/box/splitList",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/*****************************  装柜 end **********************************/

/*****************************  卸柜 start **********************************/

/**
 * 获取卸柜数据
 *
 * @export
 * @param {*} params
 * @return {*}
 */
export function loadGoodsList(params) {
  return request({
    url: "/ecw/box-preload-goods/loadGoodsList",
    method: "get",
    params,
  });
}

/**
 * 批量卸柜
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function batchUnload(data) {
  return request({
    url: "/ecw/box-load-info/batchUnload",
    method: "post",
    data,
  });
}

/**
 * 单个卸柜
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function singleUnload(data) {
  return request({
    url: "/ecw/box-load-info/singleUnload",
    method: "post",
    data,
  });
}

/**
 * 一键卸柜
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function allUnload(data) {
  return request({
    url: "/ecw/box-load-info/allUnload",
    method: "post",
    data,
  });
}

/**
 * 异常
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function createError(data) {
  return request({
    url: "/ecw/box-cabinet-unload-abnormal/create",
    method: "post",
    data,
  });
}

/*****************************  卸柜 end **********************************/

/*****************************  报关费用 start **********************************/

/**
 * 搜索出货单下的订单
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function extraCostOrder(data) {
  return request({
    url: "/ecw/box-customs-extra-cost/searchByOrderNo",
    method: "post",
    data,
  });
}

/**
 * 创建额外报关费用
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function extraCostCreate(data) {
  return request({
    url: "/ecw/box-customs-extra-cost/create",
    method: "post",
    data,
  });
}

/**
 * 更新额外报关费用
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function extraCostUpdate(data) {
  return request({
    url: "/ecw/box-customs-extra-cost/update",
    method: "put",
    data,
  });
}

/**
 * 获得额外报关费用列表
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function extraCostList(params) {
  return request({
    url: "/ecw/box-customs-extra-cost/list",
    method: "get",
    params,
  });
}

/*****************************  报关费用 end **********************************/

/*****************************  理货 start **********************************/

/**
 * 理货列表
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function getTallyList(data) {
  return request({
    url: "/shipment/box/tallyList",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/**
 * 理货移出
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function tallyRemove(data) {
  return request({
    url: "/ecw/box-preload-goods/remove",
    method: "post",
    data,
  });
}

/**
 * 理货
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function tallyLocationUpdate(data) {
  return request({
    url: "/shipment/box/batchOrderLocationUpdate",
    method: "post",
    data,
  });
}

/**
 * 理货提交
 *
 * @export
 * @param {*} data
 * @return {*}
 */
export function tallyCommit(data) {
  return request({
    url: "/shipment/box/tallyCommit",
    method: "post",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    data: jsonToFormData(data),
  });
}

/*****************************  理货 end **********************************/

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