// /**分页查询接口重试日志 */
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 mainTableInfoUrl = business + '/wholeCarDelivery/page'

export const shipbillTableUrl = business + '/wholeCarDelivery/shipbill'
export const advancedQueryUrl = business + '/wholeCarDelivery/advanced_query_shipbill'

export const goodsTableUrl = business + '/wholeCarDelivery/item'

export const packageTableUrl = business + '/wholeCarDelivery/pack'

export const startArrangeGoods = (_this, data) => {
  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否开始理货?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/wholeCarDelivery/startArrangeGoods',
          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
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

export const removeItem = (_this, data) => {
  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否删除当前运单?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/wholeCarDelivery/deleteShipbill',
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              _this.mainCurrent && _this.$refs.shipbillTable.reload({ data: { loadingBillId: _this.mainCurrent?.loadingBillId } })
              _this.$message.success(response.message)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

//运输管理-启运登记
export let t_name = ['loadingBillids', 'shipbillIds']

export const startShipRegister = (_this, data, close) => {
  let confirmLoading = true
  let st = data.st
  let names = t_name[st]
  let table = ['mainTable', 'shipbillTable'][st]
  let url = ['/wholeCarDelivery/loadingStartShipRegister', '/wholeCarDelivery/shipbillStartShipRegister']
  data[names] = data.ids.split(',')
  data.goodsPicUrl = data.picture
  let params = [{}, { data: { loadingBillId: _this.mainCurrent?.loadingBillId } }][st]
  delete data.picture
  delete data.ids
  delete data.st
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否启运登记?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + url[st],
          method: 'post',
          data,
        })
          .then((response) => {
            close()
            if (response.success) {
              _this.visible = false
              clear()
              _this.$message.success(response.message)
              _this.$refs[table].reload(params)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

//送达确认
export const deliveryConfirm = (_this, data, close) => {
  let confirmLoading = true
  let st = data.st
  let names = t_name[st]
  let table = ['mainTable', 'shipbillTable'][st]
  let url = ['/wholeCarDelivery/loadingDeliveryConfirm', '/wholeCarDelivery/shipbillDeliveryConfirm']
  data[names] = data.ids.split(',')
  data.returnOrderPicUrl = data.picture2
  data.goodsPicUrl = data.picture
  let params = [{}, { data: { loadingBillId: _this.mainCurrent?.loadingBillId } }][st]
  delete data.picture
  delete data.picture2
  delete data.ids
  delete data.st
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否启运登记?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + url[st],
          method: 'post',
          data,
        })
          .then((response) => {
            close()
            if (response.success) {
              _this.visible = false
              clear()
              _this.$message.success(response.message)
              _this.$refs[table].reload(params)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

//运输管理-/ 客服反馈  POST
export const feedbackConfirm = (_this, data, close) => {
  let confirmLoading = true
  let st = data.st
  let names = t_name[st]
  let table = ['mainTable', 'shipbillTable'][st]
  let url = ['/wholeCarDelivery/loadingFeedback', '/wholeCarDelivery/shipbillFeedback']
  data[names] = data.ids.split(',')
  data.feedbackPicture = data.picture
  let params = [{}, { data: { loadingBillId: _this.mainCurrent?.loadingBillId } }][st]
  delete data.picture
  delete data.ids
  delete data.st
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否反馈信息?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + url[st],
          method: 'post',
          data,
        })
          .then((response) => {
            close()
            if (response.success) {
              _this.visible = false
              clear()
              _this.$message.success(response.message)
              _this.$refs[table].reload(params)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

//运输管理-/ 异常登记  POST
export const abnormalRegister = (_this, data, close) => {
  let confirmLoading = true
  let st = data.st
  let names = t_name[st]
  let table = ['mainTable', 'shipbillTable'][st]
  let url = ['/wholeCarDelivery/loadingAbnormalRegister', '/wholeCarDelivery/shipbillAbnormalRegister']
  data[names] = data.ids.split(',')
  data.pictureUrl = data.picture
  let params = [{}, { data: { loadingBillId: _this.mainCurrent?.loadingBillId } }][st]
  delete data.picture
  delete data.ids
  delete data.st
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否登记异常?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + url[st],
          method: 'post',
          data,
        })
          .then((response) => {
            close()
            if (response.success) {
              _this.visible = false
              clear()
              _this.$message.success(response.message)
              _this.$refs[table].reload(params)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

export const exportUrls = {
  allUrl: business + '/wholeCarDelivery/export_all_shipbill',
  currentUrl: business + '/wholeCarDelivery/export_page_shipbill',
  selectUrl: business + '/wholeCarDelivery/export_select_line_shipbill',
  ids:[]
}
export const printUrl = business + '/wholeCarDelivery/print'
