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

const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE;

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

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





//供应商批量导入
export const mainImportUrl = serviceName + '/provider/batch_import';
export const mainDownUrl = serviceName + '/provider/provider_download_template';
//14890 供应商发货方导入模板下载
export const childrenDownUrl = serviceName + '/provider/shipper_download_template';




//供应商发货方批量导入
export const childrenImportUrl = serviceName + '/provider/batch_import_shipper';




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 mainSaveData = (_this, data, st) => {
  _this.confirmLoading = true;

  return request({
    url: serviceName + (st===3 ? '/provider/modify' : '/provider/add'),
    method: 'post',
    data
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false;
    });
};

export const childrenSaveData = (_this, data, st) => {
  _this.confirmLoading = true;

  return request({
    url: serviceName + (st=== 3 ? '/provider/modify_shipper' : '/provider/add_shipper'),
    method: 'post',
    data
  })
    .then(responseThen.bind(_this))
    .catch((response) => {
      _this.confirmLoading = false;
    });
};

export const mainIsEnable = (context, providerIds, status) => {
  return new Promise((resolve) => {
    context.$confirm({
      title: '是否' + (status > 0 ? '启用' : '停用') + '?',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        context.confirmLoading = true;
        return request({
          url: serviceName + '/provider/whether_to_enable',
          method: 'post',
          data: {
            status,
            providerIds
          }
        }).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) => {
  let confirmLoading = false;
  return new Promise((resolve) => {
    context.$confirm({
      title: '删除之后,不能再启用,确认是否删除!',
      confirmLoading,
      onOk(clear) {
        context.confirmLoading = true;
        return request({
          url: serviceName + '/provider/batch_delete',
          method: 'post',
          data: id
        })
          .then((response) => {
            if (response.success) {
              clear();
              context.$refs.mainTable.reload();
            } else {
              context.$exception(response);
            }
            confirmLoading = false;
            return resolve(response);
          })
          .catch((error) => {
            confirmLoading = false;
          });
      }
    });
  });
};


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

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


