// /**分页查询接口重试日志 */
import request from '@/utils/request'
import qs from 'qs'
// initDictionary([{ objectcode: 'CONTAINER_TYPE' }, { objectcode: 'MAINDATA_STATUS' }, { objectcode: 'DEVICE_TYPE' }]).then(res => {
//   this.options.CONTAINER_TYPE = res.CONTAINER_TYPE.map(item => {
//     item.value = +item.value
//     return item
//   })
//   this.options.MAINDATA_STATUS = res.MAINDATA_STATUS.map(item => {
//     item.value = +item.value
//     return item
//   })
//   this.options.DEVICE_TYPE = res.DEVICE_TYPE.map(item => {
//     item.value = +item.value
//     return item
//   })
// })

 import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE,UPCLOUD_RDC_TMS_BMS_SERVICE } from '@/config/serviceSettings.js'
const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE

export const add_item = () => {}
export const import_item = () => {}

export const enable_item = () => {}

export const disable_item = () => {}

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

//导入url
export const importUrl = serviceName + '/driver/batchImportDriver'
export const mainDownUrl = serviceName + '/driver/download_template'

export const cargo_ownerUrl = serviceName + '/cargo_owner/popup' //货主
export const queryCarriersUrl = serviceName + '/carrier/popup' //承运商
// //分页查询接口重试日志 GET
export const mainTableInfoUrl = serviceName + '/driver/paging_query'

//车辆绑定查询
export const getBindCarUrl = serviceName + '/driver/vehicle_binding'

//查所有车辆
export const getBindAll = (_this, data) => {
  _this.confirmLoading = true;
  return request({
    url: serviceName+'/vehicle/popup',
    //getBindCarUrl,
    method: 'post',
    data
  }).then((response) => {
      _this.confirmLoading = false;

      if (response.success) {
        return response;
      } else {
        _this.$exception(response);
        return false;
      }
    })
    .catch((response) => {
      _this.confirmLoading = false;
    });
};

export const getBMSUrl=UPCLOUD_RDC_TMS_BMS_SERVICE + '/expenseManagement/findListNoPage/v1.0'

// 费用管理查询
export const costList = (_this, data) => {
  if (!_this.$auth('bms')) {
    return new Promise((resolve, reject) => { reject() });
  }

  return request({
    url: getBMSUrl,
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  });
};
export const costFeeSave = (_this,data) => {
  return request({
    url:UPCLOUD_RDC_TMS_MAINDATA_SERVICE+ '/carrier/feeSave',
    method: 'post',
    data }).then(response => {
      _this.confirmLoading = false
      if (response.success) {
        return response
      } else {
        _this.$exception(response)
        return false
      }
    })
}

//车辆查询
export const getVehicle = (_this, data) => {
  _this.confirmLoading = true
  return request({
    url: getBindCarUrl,
    method: 'post',
    data
  })
    .then(response => {
      _this.confirmLoading = false

      if (response.success) {
        return response
      } else {
        _this.$exception(response)
        return false
      }
    })
    .catch(response => {
      _this.confirmLoading = false
    })
}

//证件资料新增
export const paper_add_url = serviceName + '/driver/paper_add' //证件新增
export const paper_update_url = serviceName + '/driver/paper_modify' //证件修改
//保存资料
export const savePaper = function(_this, st, data) {
  _this.confirmLoading = false
  return request({
    url: st > 1 ? paper_update_url : paper_add_url,
    method: 'post',
    data
  }).then(responseThen.bind(_this))
}
//证件删除
// export const savePaper = function(_this, st, data) {
//   _this.confirmLoading = false;
//   return request({
//     url: st > 1 ? paper_update_url : paper_add_url,
//     method: 'post',
//     data
//   }).then(responseThen.bind(_this));
// };
export const getLinkUrl = serviceName + '/driver/line_check' //司机常跑线路查看

export const getCertificateUrl = serviceName + '/driver/paper_link_check' //证件资料

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 ? '/driver/modify' : '/driver/add'),
    method: 'post',
    data
  })
    .then(responseThen.bind(_this))
    .catch(response => {
      _this.confirmLoading = false
    })
}

export const mainIsEnable = (context, driverIds, status) => {
  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 + '/driver/whether_to_enable',
          method: 'post',
          data: {
            status,
            driverIds
          }
        }).then(response => {
          if (response.success) {
            clear()
            context.$refs.mainTable.reload()
          } else {
            context.$exception(response)
          }
          context.confirmLoading = false
          return resolve(response)
        })
      }
    })
  })
}
export const deleteMainItem = (context, id) => {
  return new Promise(resolve => {
    context.$confirm({
      title: '删除之后,不能再启用,确认是否删除!',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        return request({
          url: serviceName + '/driver/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 paperIsEnable = (context, driverPaperIds, 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 + '/driver/paper_whether_to_enable',
          method: 'post',
          data: {
            status,
            driverPaperIds
          }
        }).then(response => {
          if (response.success) {
            clear()
            context.$refs.table_3.reload({ data: { id } })
          } else {
            context.$exception(response)
          }
          context.confirmLoading = false
          return resolve(response)
        })
      }
    })
  })
}

//司机绑定车辆删除
export const removeVehicle = (context, data, id) => {
  return new Promise(resolve => {
    context.$confirm({
      title: '是否删除司机绑定车辆?',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        return request({
          url: serviceName + '/driver/vehicle_delete',
          method: 'post',
          data
        }).then(response => {
          if (response.success) {
            clear()
            context.$refs.table_1.reload({ data: { id, whether: 1 } })
          } else {
            context.$exception(response)
          }
          context.confirmLoading = false
          return resolve(response)
        })
      }
    })
  })
}

//删除证件
export const removeCard = (context, data, id) => {
  return new Promise(resolve => {
    context.$confirm({
      title: '是否删除证件?',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        if (context.st === 1) {
          data.forEach(item => {
            let index = context.dataSource_3.findIndex(row => {
              return row.id === item.paperId
            })
            if (index > -1) {
              context.dataSource_3.splice(index, 1)
            }
          })
          return clear()
        }

        return request({
          url: serviceName + '/driver/paper_delete',
          method: 'post',
          data
        }).then(response => {
          if (response.success) {
            clear()
            context.refTable.reload({ data: { id } })
          } else {
            context.$exception(response)
          }
          context.confirmLoading = false
          return resolve(response)
        })
      }
    })
  })
}
