// /**分页查询接口重试日志 */
import request from '@/utils/request'

import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE, UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'
const business = UPCLOUD_RDC_TMS_BUSINESS_SERVICE

const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE

export const cargo_ownerUrl = serviceName + '/cargo_owner/popup' //货主
export const queryCustomerUrl = serviceName + '/tmsBaseCustomer/queryCustomerPopup' //客户
export const queryCustomer_consignee_Url = serviceName + '/tmsBaseCustomer/consignee/popup' //客户_收货方弹窗
export const packageTableUrl = business + '/allShipbill/listPackageData' //包裹信息

export const advancedQueryUrl = business + '/allShipbill/advanced_query_shipbill'

//商品单位明细
export const itemByIdUrl = serviceName + '/item/by/itemId'

export const queryBranchUrl = serviceName + '/tmsBaseBranch/queryBranchPopup' //仓库

export const queryProviderhUrl = serviceName + '/provider/ByCargoOwner/popup' //供应商

export const queryProviderh_consignee_Url = serviceName + '/provider/shipper/popup' //供应商_收货方弹窗

// //分页查询接口重试日志 GET
export const mainTableInfoUrl = business + '/shipbill/page'

//运单详情
export const mainViewUrl = business + '/shipbill/view_id'

//运单明细查询
export const childrenTableInfoUrl = business + '/shipbill/page_detail'
export const consigneeUrl = serviceName + '/tmsBaseCustomer/consignee/popup'

//运单地址解析查询：
export const addressResolutionUrl = business + '/shipbill/address_resolution/page'

//批量导入
export const mainImportUrl = business + '/shipbill/import'
export const mainDownUrl = serviceName + '/sysimport/templet/downloadTemplateById'
//供应商发货方批量导入
export const childrenImportUrl = business + '/shipbill/batch_import_shipper'

//选商品
export const getGoodsUrl = serviceName + '/item/byCargoOwner/page'

function responseThen(response) {
  if (response.success) {
    this.visible = false
    this.$message.success(response.message)
  } else {
    this.$exception(response)
  }
  setTimeout(() => {
    this.confirmLoading = false
  }, 1000)
  return response
}


//订单新增|修改 保存
export const getItemUnit = (_this, data, st) => {
  return request({
    url: itemByIdUrl,
    method: 'post',
    data,
  })
}

//订单新增|修改 保存
export const mainSaveData = (_this, data, st, ok = false) => {
  let arr = [null, null, '/shipbill/add', '/shipbill/modify', '/shipbill/copy']
  if (ok) {
    return request({
      url: business + arr[st],
      method: 'post',
      data,
    })
  }
  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否保存运单?',
      confirmLoading,
      onOk(clear) {
        _this.$emit('loading', true)
        confirmLoading = true
        return request({
          url: business + arr[st],
          method: 'post',
          data,
        }).then((response) => {
          if (response.success) {
            clear()
            _this.$message.success(response.message)
          } else {
            _this.$exception(response)
          }
          setTimeout(() => {
            confirmLoading = false
            _this.$emit('loading', false)

          }, 100)
          return resolve(response)
        })
          .finally((response) => {
            _this.$emit('loading', false)
            confirmLoading = false
          })

      },
    })
  })
}

//运单批量提交
export const dataSubmit = (_this, data) => {
  // data.returnOrderFlag = data.consigneeReturnOrderFlag
  // data.returnOrderDeadline = data.consigneeReturnOrderDeadline
  // delete data.consigneeReturnOrderFlag
  // delete data.consigneeReturnOrderDeadline

  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否提交运单?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/shipbill/submit',
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              _this.$refs.mainTable.reload()
              _this.$message.success(response.message)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 100)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false

          })
      },
    })
  })
}

//订单新增|修改 保存提交
export const mainSaveDataSubmit = (_this, data, st, ok = false) => {
  let arr = [null, null, '/shipbill/add_submit', '/shipbill/modify_submit', '/shipbill/copy_submit']
  /**0=不显示,1=查看,2=新建,3=修改,4=复制 */
  // /**0=不显示, 1=新建，2=修改 ，3=查看 ，4=复制  */,
  if (ok) {
    return request({
      url: business + arr[st],
      method: 'post',
      data,
    })
  }
  let confirmLoading = true

  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否保存并提交?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        _this.$emit('loading', true)

        return request({
          url: business + arr[st],
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              _this.$message.success(response.message)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
              _this.$emit('loading', false)

            }, 100)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
            _this.$emit('loading', false)

          })
      },
    })
  })
}





export const deleteMainItem = (context, id) => {
  let confirmLoading = false
  return new Promise((resolve) => {
    context.$confirm({
      title: '确认是否作废!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/shipbill/invalid',
          method: 'post',
          data: id,
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$refs.mainTable.reload()
            } else {
              context.$exception(response)
            }
            confirmLoading = false
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}

export const deleteChildrenItem = (context, ids) => {
  let confirmLoading = false
  return new Promise((resolve) => {
    context.$confirm({
      title: '删除之后,不能再启用,确认是否删除!',
      confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        return request({
          url: business + '/provider/batch_delete_shipper',
          method: 'post',
          data: ids,
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$refs.childrenTable.reload()
            } else {
              context.$exception(response)
            }
            confirmLoading = false
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}
//接单,拒绝
export const toggleReject = (context, shipperIds, status) => {
  let confirmLoading = false
  return new Promise((resolve) => {
    context.$confirm({
      title: '是否' + (status > 0 ? '运单接单' : '运单拒绝') + '?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + (status > 0 ? '/shipbill/receive' : '/shipbill/reject'),
          method: 'post',
          data: shipperIds,
        }).then((response) => {
          if (response.success) {
            clear()
          }
          context.$exception(response)
          confirmLoading = false
          return resolve(response)
        })
      },
    })
  })
}

// 定位
export const Location = (context, data) => {
  let confirmLoading = false
  return new Promise((resolve) => {
    context.$confirm({
      title: '是否定位?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: serviceName + '/common/shipbill/location',
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$refs.mainTable.reload()
            } else {
              context.$exception(response)
            }
            confirmLoading = false
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
};

