// /**分页查询接口重试日志 */
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 + '/waitLoadingAndUnloading/query_shipbill'
export const mainTableNextInfoUrl = business + '/waitLoadingAndUnloading/query_receiver'
export const mainTableAppointmentUrl = business + '/waitLoadingAndUnloading/queryAppointmentTypePage'
export const shipbillTableUrl = business + '/waitLoadingAndUnloading/receiver_get_shipbill'
export const advancedQueryUrl = business + '/waitLoadingAndUnloading/advanced_query_shipbill'

export const goodsTableUrl = business + '/waitLoadingAndUnloading/item_detail'
export const packageTableUrl = business + '/waitLoadingAndUnloading/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 + '/waitLoadingAndUnloading/started_work',
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              _this.clearChildrenTableAll()
              _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 completeArrangeGoods = (_this, data) => {
  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否装卸完成?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: business + '/waitLoadingAndUnloading/complete_work',
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              _this.clearChildrenTableAll()
              _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 exportUrls = {
  allUrl: business + '/waitLoadingAndUnloading/export_all_shipbill',
  currentUrl: business + '/waitLoadingAndUnloading/export_page_shipbill',
  selectUrl: business + '/waitLoadingAndUnloading/export_select_line_shipbill',
  ids:[]

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

//系统参数配置查询
export const getSysConfig = (_this, data) => {
  return request({
    url: serviceName + '/sysParamConfig/query',
    method: 'post',
    data: { pageSize: 10, pageNum: 1 },
  }).then((response) => {
    if (response.success) {
      _this.sysConfig = response.data
    } else {
      _this.$exception(response)
    }

    //  return resolve(response)
  })
};


//通派车单查商品（已扫、未扫）
export const queryLoadingBill = (_this, data) => {
  _this.loading=true
  return request({
    url: business  + '/waitLoadingAndUnloading/queryShipbillDetailByLoadingBill',
    method: 'post',
    params:data,
  }).then((response) => {
    if (response.success) {
    } else {
     // _this.$exception(response)
    }
    return response
  }).finally(() => {
    _this.loading = false
    _this.$nextTick(() => {
      _this.$refs?.scanInput?.focus();
      let form = _this.form();
      form.resetFields()


    })

  })
}

//通派车单查运单（已扫、未扫）
export const queryShipbillByLoadingBill = (_this, data) => {
  _this.loading=true
  return request({
    url: business  + '/waitLoadingAndUnloading/queryShipbillByLoadingBill',
    method: 'post',
    params:data,
  }).then((response) => {
    if (response.success) {
    } else {
     // _this.$exception(response)
    }
    return response
  }).finally(() => {
    _this.loading = false
    _this.$nextTick(() => {
      _this.$refs?.scanInput?.focus();
      let form = _this.form();
      form.resetFields()


    })

  })
}
//装车扫描
export const loadingScan = (_this, data) => {
  _this.loading=true

  return request({
    url: business + '/' + _this.$props.portName + '/loadingScan',
    method: 'post',
    data,
  }).then((response) => {
    if (response.success) {
    } else {
     // _this.$exception(response)
    }
    return response
  }).finally(() => {
    _this.loading = false
    _this.$nextTick(() => {
      _this.$refs?.scanInput?.focus();
      let form = _this.form();
      form.resetFields()
    })

  })
}

export const loading_confirm = (_this, data) => {
  let confirmLoading = true
  let title = '派车作业全部完成，确认装车?'
  if (data.noScannedShipbillList.length > 0) {
    title = `派车单【${data.noScannedShipbillList}】中存在${data.noScannedShipbillList.length}个未扫描的运单，是否装车！`
  }


  return new Promise((resolve) => {
    _this.$confirm({
      title,
      confirmLoading,
      content() {
        return _this._c('a-form-item', {
          attrs: {
            labelCol: { span: 8 },
            wrapperCol:{ span: 16 },
            label: "容器是否随车"
          }
        }, [_this._c('a-radio-group', {
          on: {
            change({target}) {
              data.containerWithVehicle= target.value
            }
          },
          attrs: {
            defaultValue:data.containerWithVehicle,
            options: [{ label: '是', value: 1 }, { label: '否', value: 0 }]
          }
        })])
      },
      onOk(clear) {
        confirmLoading = true

        return request({
          url: business + '/' + _this.$props.portName + '/loadingConfirm',
          method: 'post',
          data,
        })
          .then((response) => {
            close()
            _this.visible = false
            if (response.success) {
              clear()
              _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 billLoadingConfirm = (_this, data) => {
  let title = '派车作业全部完成，确认装车?'
  if (data.noScannedShipbillDetailList.length > 0) {
    title = `派车单【${data.loadingBillId}】中存在${data.noScannedShipbillDetailList.length}个未扫描的商品，是否装车！`
  }
  let confirmLoading = true

  return new Promise((resolve) => {
    _this.$confirm({
      title,
      confirmLoading,
      content() {
        return _this._c('a-form-item', {
          attrs: {
            labelCol: { span: 8 },
            wrapperCol:{ span: 16 },
            label: "容器是否随车"
          }
        }, [_this._c('a-radio-group', {
          on: {
            change({target}) {
              data.containerWithVehicle= target.value
            }
          },
          attrs: {
            defaultValue:data.containerWithVehicle,
            options: [{ label: '是', value: 1 }, { label: '否', value: 0 }]
          }
        })])
      },
      onOk(clear) {
        confirmLoading = true

        return request({
          url: business + '/' + _this.$props.portName + '/loadingConfirm',
          method: 'post',
          data,
        })
          .then((response) => {
            close()
            _this.visible = false
            if (response.success) {
              clear()
              _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 billScanInfoUrl = business + '/waitLoadingAndUnloading/queryLoadingBillScanInfo';

export const getBillScanInfo = (_this, data) => {
  _this.loading=true
  _this.billDataSource=[]
  return request({
    url: billScanInfoUrl,
    method: 'post',
    data,
  }).then((response) => {
    if (response.success) {
      _this.billDataSource = response.data?.scannedLoadingBillDTOList??[]
    } else {
      _this.$exception(response)
    }
    return response
  }).finally(() => {


    _this.loading = false
    _this.$nextTick(() => {
      _this.$refs?.scanInput?.focus();
      let form = _this.form();
      form.resetFields()
    })

  })
}


