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

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

const serviceName = UPCLOUD_RDC_TMS_MAINDATA_SERVICE;

// //分页查询接口重试日志 GET
export const mainTableInfoUrl = serviceName + '/messageConfig/page';
export const mainViewUrl = serviceName + '/messageConfig/findById';

export const getPageView = (context, id) => {
  context.loading = true;
  return request({
    url: mainViewUrl,
    method: 'post',
    params: {
      id,
    },
  })
    .then((response) => {
      context.loading = false;
      if (response.success) {
        return response;
      } else {
        context.$exception(response);
      }
    })
    .catch((error) => {
      context.loading = false;
    });
};

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;

  return new Promise((resolve) => {
    context.$confirm({
      title: '是否保存数据!',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true;
        return request({
          url: serviceName + '/messageConfig/save',
          method: 'post',
          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, idList) => {
  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 + '/messageConfig/delete',
          method: 'post',
          headers: {
            'Content-Type': 'application/json',
          },
          data: { idList },
        })
          .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, status, idList) => {
  let title = '启用';
  let url = serviceName + '/messageConfig/updateStatus';
  if (status==='1') {
    title = '停用';
  }
  return new Promise((resolve) => {
    context.$confirm({
      title: '是否' + title + '?',
      confirmLoading: context.confirmLoading,
      onOk(clear) {
        context.confirmLoading = true;
        return request({
          url,
          method: 'post',
          data: {
            idList,
            status:status==='1'?'0':'1',
          },
        }).then((response) => {
          if (response.success) {
            clear();
            context.$refs.mainTable.reload();
          } else {
            context.$exception(response);
          }
          context.confirmLoading = false;
          return resolve(response);
        });
      },
    });
  });
};
