// /**分页查询接口重试日志 */
import request from '@/utils/request'
import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE } from '@/config/serviceSettings.js'
const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE

export const add_item = () => {}

export const enable_item = () => {}

export const disable_item = () => {}

export const show_info_item = () => {}
export const update_item = () => {}

export const importUrl = serviceName + '/deliveryman/batchImportDeliveryman'
export const mainDownUrl = serviceName + '//deliveryman/download_template'

export const cargo_ownerUrl = serviceName + '/cargo_owner/popup' //货主
export const queryCarriersUrl = serviceName + '/carrier/popup' //承运商

//承运商

//分页查询接口重试日志 GET
export const mainTableInfoUrl = serviceName + '/deliveryman/paging_query' //送货员查询
export const getCertificateUrl = serviceName + '/deliveryman/paper_link_check' //证件资料

export const paper_isEnable_url = serviceName + '/deliveryman/paper_whether_to_enable' //证件启用

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 saveMainData = (_this, data, st) => {
  let userCategory = _this.$store.getters?.userInfo?.userCategory ?? null
  if (userCategory !== 'I') {
    data.affiliationType=2
  }
  _this.confirmLoading = true
  return request({
    url: serviceName + (st > 1 ? '/deliveryman/modify' : '/deliveryman/add'),
    method: 'post',
    data
  }).then(responseThen.bind(_this)).catch(response => {
    _this.confirmLoading = false

  })
}

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

export const mainIsEnable = (context, deliverymanIds, status) => {
  return new Promise(resolve => {
    context.$confirm({
      title: '是否' + (status > 0 ? '启用' : '禁用') + '?',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        return request({
          url: serviceName + '/deliveryman/whether_to_enable',
          method: 'post',
          data: {
            status,
            deliverymanIds
          }
        }).then(response => {
          if (response.success) {
            clear()
            context.$refs.mainTable.reload()
          } else {
            context.$exception(response)
          }
          context.confirmLoading = false
          return resolve(response)
        })
      }
    })
  })
}

export const paperIsEnable = (context, deliverymanPaperIds, status, id) => {
  if (status === 1) {
    status=10
  }
  return new Promise(resolve => {
    context.$confirm({
      title: '是否' + (status > 0 ? '启用' : '禁用') + '?',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        return request({
          url: serviceName + '/deliveryman/paper_whether_to_enable',
          method: 'post',
          data: {
            status,
            deliverymanPaperIds
          }
        }).then(response => {
          if (response.success) {
            clear()
            context.$refs.table.reload({ data: { id } })
          } else {
            context.$exception(response)
          }
          context.confirmLoading = false
          return resolve(response)
        })
      }
    })
  })
}
//证件资料新增
export const paper_add_url = serviceName + '/deliveryman/paper_add' //证件新增
export const paper_update_url = serviceName + '/deliveryman/paper_modify' //证件修改
export const savePaper = function(_this, st, data) {
  _this.confirmLoading = true
  return request({
    url: st > 1 ? paper_update_url : paper_add_url,
    method: 'post',
    data
  }).then(responseThen.bind(_this)).catch((response) => {
    _this.confirmLoading = false;
  });
}

// export const getDrives = data => {
//   return request({
//     url: serviceName + '/datasource/v1/drivers',
//     method: 'get',
//     data
//   }).then(response => {
//     if (response.success) {
//       return response.data
//     } else {
//       this.$exception(response)
//       return false
//     }
//   })
// }

// export const addFormItem = (_this, data) => {
//   _this.confirmLoading = true
//   return request({
//     url: serviceName + '/datasource/v1/add',
//     method: 'post',
//     data
//   }).then(responseThen.bind(_this))
// }

// export const updateFormItem = (_this, data) => {
//   _this.confirmLoading = true
//   return request({
//     url: serviceName + '/datasource/v1/update',
//     method: 'post',
//     data
//   }).then(responseThen.bind(_this))
// }

// //删除告警记录
// export const deleteFormItem = (context, id) => {

//   return new Promise(resolve => {
//     context.$confirm({
//       title: '是否删除?',
//       confirmLoading: context.spinning,
//       onOk(clear) {
//         context.spinning = true
//         return request({
//           url: serviceName + '/datasource/v1/delete',
//           method: 'post',
//           params: {id}
//         }).then(response => {
//           if (response.success) {
//             clear()
//             context.$refs.table.reload()
//           } else {
//             context.$exception(response)
//           }
//           context.spinning=false
//           return resolve(response)
//         })
//       }

//     })
//   })
// }

// // 测试数据源 POST
// export const testDateSource =(context,data)  => {
//   return request({
//     url: serviceName + '/datasource/v1/test',
//     method: 'post',
//     data
//   }).then(response => {
//     if (response.success) {
//       context.$message.success(response.message)
//     } else {
//       context.$exception(response)
//     }
//     return response

//   })
// }

//删除证件
export const removeCard = (context, data, id) => {
  return new Promise(resolve => {
    context.$confirm({
      title: '是否删除证件?',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        if (context.st === 1) {
          data.forEach(item => {
            let index = context.dataSource.findIndex(row => {
              return row.id === item.paperId
            })
            if (index > -1) {
              context.dataSource.splice(index, 1)
            }
          })
          return clear()
        }
        context.confirmLoading = true
        return request({
          url: serviceName + '/deliveryman/paper_delete',
          method: 'post',
          data
        }).then(response => {
          if (response.success) {
            clear()
            context.$refs.table.reload({ data: { id } })
          } else {
            context.$exception(response)
          }
          context.confirmLoading = false
          return resolve(response)
        })
      }
    })
  })
}
