// /**分页查询接口重试日志 */
import request from '@/utils/request'
import qs from 'qs'

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

const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE
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
      }
    })
}


// //分页查询接口重试日志 GET
export const mainTableInfoUrl = serviceName + '/cargo_owner/paging_query'

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
}

//新增|修改
/**0=不显示,1=查看,2=新建,3=修改,4=复制 */
export const mainSaveData = (context, data, st) => {
  let confirmLoading = false
  if (Array.isArray(data.departmentId)) {
    data.departmentId=data.departmentId.join(',')
  }

  return new Promise((resolve) => {
    context.$confirm({
      title: '是否保存数据!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: serviceName + (st === 3 ? '/cargo_owner/modify' : '/cargo_owner/add'),
          method: 'post',
          headers: {
            'Content-Type': 'application/json',
          },
          data,
        }).then((response)=> {
          if (response.success) {
            clear()
            context.$message.success(response.message)
          } else {
            context.$exception(response)
          }
          setTimeout(() => {
           confirmLoading = false
          }, 1000)
          return resolve(response)
        }).catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}

export const deleteMainItem = (context, id) => {
  let confirmLoading = false
  id = id.map((key) => {
    return { cargoOwnerId: key }
  })
  return new Promise((resolve) => {
    context.$confirm({
      title: '删除之后,不能再启用,确认是否删除!',
      confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        return request({
          url: serviceName + '/cargo_owner/batch_delete',
          method: 'post',
          headers: {
            'Content-Type': 'application/json',
          },
          data: JSON.stringify(id),
        })
          .then((response) => {
            if (response.success) {
              clear()
              context.$refs.mainTable.reload()
            } else {
              context.$exception(response)
            }
            return resolve(response)
          })
          .catch((error) => {
            confirmLoading = false
          })
      },
    })
  })
}
export const mainIsEnable = (context, cargoOwnerIds, status) => {
  // status=String(status)

  if (status === 1||status === 0) {
    status = 10
  } else if (status == 10) {
    status=0
  }

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