// /**分页查询接口重试日志 */
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 + '/deliveryConfirm/query_loadbill'
export const advancedQueryUrl = business + '/deliveryConfirm/advanced_query_shipbill'

export const shipbillTableUrl = business + '/deliveryConfirm/get_shipbill'

export const goodsTableUrl = business + '/deliveryConfirm/item_detail'

export const packageTableUrl = business + '/deliveryConfirm/package_detail'

export const startArrangeGoods = (_this, data) => {
  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否开始理货?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/deliveryConfirm/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 handoverSubmit = (_this, data, close) => {
  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否托运交接?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/outsource_track/handover',
          method: 'post',
          data,
        })
          .then((response) => {
            close()
            _this.visible = false
            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) => {
            _this.visible = false
            confirmLoading = false
          })
      },
    })
  })
}

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

}
export const printUrl = business + '/deliveryConfirm/print'

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

export const startShipRegister = (_this, data, close) => {
  let confirmLoading = true
  let st = data.st
  let names = t_name[st]
  let url = ['/deliveryConfirm/start_shipment', '/deliveryConfirm/shipbill_start_shipment']
  let table = ['mainTable', 'shipbillTable'][st]
  data[names] = data.ids.split(',')
  let params = [{}, { data: { loadingBillId: _this.mainCurrent?.loadingBillId } }][st]

  data.departurePhoto = data.picture
  delete data.ids
  delete data.st
  delete data.picture
  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 = ['/deliveryConfirm/feed_back', '/deliveryConfirm/shipbill_feed_back']
  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 = ['/deliveryConfirm/abnormal_register', '/deliveryConfirm/shipbill_abnormal_register']
  data[names] = data.ids.split(',')
  data.abnormalPhoto = 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 removeItem = (_this, data) => {
  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否删除当前运单?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/deliveryConfirm/delete_detail',
          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
          })
      },
    })
  })
}