import request from '@/utils/request'
import store from '@/store'
import { axios } from '@/utils/request'
import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE, UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'
//运单详情
export const shipbillViewUrl = UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/view_id'
const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE
//首页 我发起的审批流程查询
const tabUrl_1 = serviceName + '/approval_business/apply'
// 首页 待我审批的审批流程查询 POST
const tabUrl_2 = serviceName + '/approval_business/pending_approval'
// 首页 我已处理的的审批流程查询 POST
const tabUrl_3 = serviceName + '/approval_business/processed'

//查询审核节点
export const get_bus_detail_url = serviceName + '/approval_business/get_bus_detail'
// 查询审核历史 POST
export const get_bus_log_url = serviceName + '/approval_business/get_bus_log'
// /sendcar/sendcar_get_approval_info


const btnUrl = {
  1: {
    approval: serviceName + '/cargo_owner/approval', //货主流程审批 审核
    invalid: serviceName + '/cargo_owner/approval_invalid', // 货主流程审批 作废 POST
    retract: serviceName + '/cargo_owner/approval_retract', // 货主流程审批 撤回 POST
    summit: serviceName + '/cargo_owner/approval_summit', // 货主流程审批 提交 POST
  },
  2: {
    approval: serviceName + '/carrier/carrier_approval', //承运商审批 审核
    invalid: serviceName + '/carrier/carrier_approval_invalid', // 承运商审批 作废 POST
    retract: serviceName + '/carrier/carrier_approval_retract', // 承运商审批 撤回 POST
    summit: serviceName + '/carrier/carrier_approval_submit', //承运商审批 提交 POST
  },
  3: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/shipbill_approval', //运单审批 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/shipbill_approval_invalid', // 运单审批 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/shipbill_approval_retract', // 运单审批 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/shipbill_approval_submit', //运单审批 提交 POST
  },
  4: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/sendcar/sendcar_dispatch_approval', //调度 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/sendcar/sendcar_dispatch_approval_invalid', // 调度审批 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/sendcar/sendcar_dispatch_approval_retract', // 调度审批 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/sendcar/sendcar_dispatch_approval_submit', //调度审批 提交 POST
  },
  5: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/trailer/trailer_dispatch_approval', //承运商调度审批 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/trailer/trailer_dispatch_approval_invalid', // 承运商调度审批 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/trailer/trailer_dispatch_approval_retract', // 承运商调度审批 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/trailer/trailer_dispatch_approval_submit', //承运商调度审批 提交 POST
  },
  9: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitForSelfWithdrawal/vehicle_register_approval', //自提 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitForSelfWithdrawal/vehicle_register_approval_invalid', // 自提 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitForSelfWithdrawal/vehicle_register_approval_retract', // 自提 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitForSelfWithdrawal/vehicle_register_approval_submit', //自提 提交 POST
  },
  10: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitPickUpGoods/reviewApproval', //提货审批 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitPickUpGoods/invalidApproval', // 提货审批 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitPickUpGoods/retractApproval', // 提货审批 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitPickUpGoods/submitApproval', //提货审批 提交 POST
  },
  11: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_receipt/reviewApproval', //回单审批 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_receipt/invalidApproval', // 回单审批 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_receipt/retractApproval', // 回单审批 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_receipt/submitApproval', //回单审批 提交 POST
  },
  12: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_orig_receipt/reviewApproval', //正本回单审批 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_orig_receipt/invalidApproval', // 正本回单审批 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_orig_receipt/retractApproval', // 正本回单审批 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_orig_receipt/submitApproval', //正本回单审批 提交 POST
  },
  13: {
    approval: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbillEventback/reviewApproval', //事件上传 审核
    invalid: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbillEventback/invalidApproval', // 事件上传 作废 POST
    retract: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbillEventback/retractApproval', // 事件上传 撤回 POST
    summit: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbillEventback/submitApproval', //事件上传 提交 POST
  },
}



// 回单审核信息查询 POST
export const pending_receipt_approval_info = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_receipt/getApprovalInfo',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
};
// 正本回单审核信息查询 POST
export const pending_orig_receipt_approval_info = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/pending_orig_receipt/getApprovalInfo',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
};

//调度和流程审批的查询 POST
export const sendcar_get_approval_info = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/sendcar/sendcar_get_approval_info',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}
//承运商调度 POST
export const trailer_get_approval_info = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/trailer/trailer_get_approval_info',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}
// 货主资料和流程审批的查询 POST
export const cargoView = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: serviceName + '/cargo_owner/approval_view',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}
// 承运商审核信息查询 POST
export const carrierView = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: serviceName + '/carrier/get_approval_info',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}

// 运单审核信息查询 POST
export const shipbillView = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbill/get_approval_info',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}

// 自提登记信息查询 POST
export const vehicle_register_get_approval_info = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitForSelfWithdrawal/vehicle_register_get_approval_info',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}

// 提货审批信息查询 POST
export const getApprovalInfo = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/waitPickUpGoods/getApprovalInfo',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}

// 事件上传审批流信息查询 POST
export const shipbillEventback_getApprovalInfo = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/shipbillEventback/getApprovalInfo',
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false
    })
}

//审核
export const approvalSubmit = (context, index, data) => {
  let confirmLoading = false
  const { approvalBusinessId, cargoOwnerCode, cargoOwnerId } = data
  // let _data = { approvalBusinessId, cargoOwnerCode, cargoOwnerId }



  return new Promise((resolve) => {
    context.$confirm({
      title: '确认提交审核!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: btnUrl[index].approval,
          method: 'post',
          data,
        })
          .then((response) => {
            clear()
            confirmLoading = false
            if (response.success) {
              context.$message.success(response.message)
              context.back(approvalBusinessId, true)
            } else {
              context.$exception(response)
            }
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}

//作废
export const invalid = (context, index, data) => {
  let confirmLoading = false

  const { approvalBusinessId, cargoOwnerCode, cargoOwnerId, carriersId, shipbillId, loadingBillId } = data
  let _data = null
  switch (context.approvalType) {
    case 1:
      _data = { approvalBusinessId, cargoOwnerId, cargoOwnerCode }
      break
    case 2:
      _data = { approvalBusinessId, carriersId }
      break
    case 3:
      _data = { approvalBusinessId, shipbillId }
      break
    case 4: //装车单ID"
      _data = { approvalBusinessId, loadingBillId }
      break
    case 5:
      _data = { approvalBusinessId, loadingBillId }
      break
    case 9:
      _data = { approvalBusinessId, shipbillId }
      break
  }
  return new Promise((resolve) => {
    context.$confirm({
      title: '确认是否作废!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: btnUrl[index].invalid,
          method: 'post',
          data: _data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$message.success(response.message)
              context.back(approvalBusinessId, true)
            } else {
              context.$exception(response)
            }
            confirmLoading = false
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}
//撤回
export const retract = (context, index, data) => {
  const { approvalBusinessId, cargoOwnerCode, cargoOwnerId, carriersId, shipbillId, loadingBillId } = data

  let _data = null
  switch (context.approvalType) {
    case 1:
      _data = { approvalBusinessId, cargoOwnerId, cargoOwnerCode }
      break
    case 2:
      _data = { approvalBusinessId, carriersId }
      break
    case 3:
      _data = { approvalBusinessId, shipbillId }
      break
    case 4:
      //装车单ID"
      _data = { approvalBusinessId, loadingBillId }
      break
    case 5:
      //装车单ID"
      _data = { approvalBusinessId, loadingBillId }
      break
    case 9:
      //自提登记"
      _data = { approvalBusinessId, shipbillId }
      break
  }

  let confirmLoading = false
  return new Promise((resolve) => {
    context.$confirm({
      title: '确认是否撤回!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: btnUrl[index].retract,
          method: 'post',
          data: _data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$message.success(response.message)
              context.back(approvalBusinessId, true)
            } else {
              context.$exception(response)
            }
            confirmLoading = false
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}
//提交
export const submit = (context, index, data) => {
  let confirmLoading = false
  delete data.columns;
  return new Promise((resolve) => {
    context.$confirm({
      title: '确认是否提交!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: btnUrl[index].summit,
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$message.success(response.message)
              context.back(data.id, true)
            } else {
              context.$exception(response)
            }
            confirmLoading = false
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}

export const getTabUrl = (key) => {
  return { url: eval('tabUrl_' + key), method: 'post' }
}

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

//新增|修改
export const mainSaveData = (_this, data, st) => {
  _this.confirmLoading = true
  return request({
    url: serviceName + (st === 2 ? '/signconfig/save' : '/signconfig/update'),
    method: 'post',
    data,
  })
    .then(responseThen.bind(_this))
    .then((response) => {
      store.dispatch('getSign')
      return response
    })
    .catch((response) => {
      _this.confirmLoading = false
    })
}

export const deleteMainItem = (context, id) => {
  let confirmLoading = false
  return new Promise((resolve) => {
    context.$confirm({
      title: '删除之后,不能再启用,确认是否删除!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: serviceName + '/signconfig/delete',
          method: 'post',
          data: id,
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$refs.mainTable.reload()
            } else {
              context.$exception(response)
            }
            confirmLoading = false
            store.dispatch('getSign')
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}

export const customRenderOptions = (self, type) => {
  return (text, data) => {
    if (self.options[type] && Array.isArray(self.options[type])) {
      let value = self.options[type].find((item) => item.value === text)
      return value ? value.label : text
    }
    return text
  }
}

// 统计查询
export const statistical = (data) => {
  return axios.post(UPCLOUD_RDC_TMS_BUSINESS_SERVICE + '/frontPage/statistical', data)
}
