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

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

const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE

export const queryCarriersUrl = serviceName + '/carrier/popup' //承运商

// //分页查询接口重试日志 GET
export const mainTableInfoUrl = serviceName + '/container/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 ? '/container/update' : '/container/save'),
          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, ids) => {
  let confirmLoading = false

  return new Promise((resolve) => {
    context.$confirm({
      title: '删除之后,不能再启用,确认是否删除!',
      confirmLoading,
      onOk(clear) {
        context.confirmLoading = true
        return request({
          url: serviceName + '/container/delete',
          method: 'post',
          headers: {
            'Content-Type': 'application/json',
          },
          data:{ids},
        })
          .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, ids, 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 + '/container/whether_to_enable',
          method: 'post',
          data: {
            status,
            ids
          }
        }).then((response) => {
          if (response.success) {
            clear();
            context.$refs.mainTable.reload();
          } else {
            context.$exception(response);
          }
          context.confirmLoading = false;
          return resolve(response);
        });
      }
    });
  });
};
export const importUrl = serviceName + '/container/importContainer'
export const mainDownUrl = serviceName + '/container/downloadTemplate'