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 settlementCreate(data) {
  return request({
    url: "/ecw/box-settlement/create",
    method: "post",
    data,
  });
}

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

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

/**
 * 获得订单预装分页
 *
 * @export
 * @param {*} params
 * @return {*}
 */
export function preloadPage(params) {
  return request({
    url: "/ecw/order/preloadPage",
    method: "get",
    params,
  });
}

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

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

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

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

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

/**
 * 服务提示消息回调
 *
 * @export
 * @param {*} result
 * @param {*} _vue
 * @return {*}
 */
export function serviceMsg(result, _vue) {
  return new Promise((resolve, reject) => {
    const { code, msg } = result;
    if (code === 0) {
      _vue.$message.success("操作成功");
      resolve();
    } else {
      _vue.$message.error(msg);
      reject();
    }
  });
}

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