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
  })
}

/**
 * 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;
}
