import { request } from 'umi';
import { projectObj } from '@/config/api.js';
import UserStorage from '@/config/sagacare/sagacare_user';
import Request from 'umi-request';

function getProjectId() {
  //const id = window.localStorage.getItem('localProjectId')
  const id = projectObj.projectId;
  return id;
}

function commonParams() {
  var userObj = UserStorage.getInstance();
  const user = userObj.getUser();
  var pubname = 'sgadmin';
  return `openid=${user.id}&pubname=${pubname}&projectId=${getProjectId()}&userId=${
    user.id
  }&userName=${user.name}&userPhone=${user.phone}`;
}

// function commonParams() {
//     return `openid=9a1ecfbacb6b4f249bf2dd3ec7793ead&pubname=sagacareAndtenantslink&projectId=${getProjectId()}&userName=%E5%AE%89%E5%B0%8F%E9%9C%9E&userPhone=17611228068&userId=9a1ecfbacb6b4f249bf2dd3ec7793ead`;
// }

//窗帘  设置设备
export const setEquipeHttp = (params) => {
  return request(`/sgadmin/duoduo-service/duoduoenv-service/ctrl/set?${commonParams()}`, {
    method: 'POST',
    from: 'sagacare',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params),
    headers: {
      'Content-Type': 'application/json',
    },
  });
};

//窗帘 获取设备
export const getObjectDataEqpGroupHttp = (params) => {
  return request(
    `/sgadmin/duoduo-service/duoduoenv-service/object/objectDataEqpGroup/query?${commonParams()}`,
    {
      method: 'POST',
      from: 'sagacare',
      isNotShowErrorToast: true,
      errorSave: true,
      data: JSON.stringify(params),
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );
};

// 插座的状态
export const getSocketHttp = (params) => {
  return request(
    `/sgadmin/duoduo-service/duoduoenv-service/ctrl/queryEquipmentStatusInfo?${commonParams()}&spaceId=${
      params.spaceId
    }&equipType=${params.equipType}`,
    {
      //timeout: 1000,
      skipErrorHandler: true,
      method: 'GET',
      from: 'sagacare',
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );
};
//设置插座
export const setSocketHttp = (params) => {
  return request(`/sgadmin/duoduo-service/duoduoenv-service/ctrl/set?${commonParams()}`, {
    method: 'POST',
    from: 'sagacare',
    isNotShowErrorToast: true,
    errorSave: true,
    data: JSON.stringify(params),
    headers: {
      'Content-Type': 'application/json',
    },
  });
};
// 灯的状态
export const getLampHttp = (params) => {
  return Request(
    `/sgadmin/duoduo-service/duoduoenv-service/light/status/query?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    {
      //timeout: 1000,
      skipErrorHandler: true,
      method: 'GET',
      from: 'sagacare',
      headers: {
        'Content-Type': 'application/json',
      },
      cancelToken: params.token,
    },
  );
};
// 批量 开关插座
export const setallSocketHttp = (params) => {
  return request(`/sgadmin/duoduo-service/duoduoenv-service/ctrl/set?${commonParams()}`, {
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    from: 'sagacare',
    data: JSON.stringify(params),
    headers: {
      'Content-Type': 'application/json',
    },
  });
};

// 批量 开关灯
export const setallLampHttp = (params) => {
  return request(`/sgadmin/duoduo-service/duoduoenv-service/light/onoff/set?${commonParams()}`, {
    method: 'POST',
    isNotShowErrorToast: true,
    errorSave: true,
    from: 'sagacare',
    data: JSON.stringify(params),
    headers: {
      'Content-Type': 'application/json',
    },
  });
};

// 开关失败时 定时查询灯状态
export const getStatusHttp = (params) => {
  return request(
    `/sgadmin/duoduo-service/duoduoenv-service/light/onoff/setSatus?${commonParams()}`,
    {
      method: 'POST',
      isNotShowErrorToast: true,
      errorSave: true,
      from: 'sagacare',
      data: JSON.stringify(params),
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );
};

//  空调 - 调节文案/空调信息
export const getFeedbackDocumentsHttp = (params) => {
  return request(
    `/sgadmin/duoduo-service/duoduoenv-service/custom/feedback/documents?${commonParams()}`,
    {
      isNotShowErrorToast: true,
      errorSave: true,
      from: 'sagacare',
      method: 'POST',
      data: JSON.stringify(params),
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );
};

//  空调 调节
export const changeAirHttp = (params) => {
  return request(
    `/sgadmin/duoduo-service/duoduoenv-service/custom/feedbackCreate/v2?${commonParams()}`,
    {
      method: 'POST',
      from: 'sagacare',
      isNotShowErrorToast: true,
      errorSave: true,
      data: JSON.stringify({ ...params, projectId: getProjectId() }),
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );
};

// 空间环境质量
export const getSpaceAdjustList = (params) => {
  return request(`/sgadmin/duoduo-service/duoduoenv-service/space/property?${commonParams()}`, {
    method: 'POST',
    from: 'sagacare',
    isNotShowErrorToast: true,
    data: params,
    headers: {
      'Content-Type': 'application/json',
    },
  });
};
//  批量空调 调节
export const changeAllAirHttp = (body) => {
  return request(
    `/sgadmin/duoduo-service/duoduoenv-service/custom/feedbackCreate/v2Batch?${commonParams()}`,
    {
      method: 'POST',
      //timeout:100000,
      skipErrorHandler: true,
      from: 'sagacare',
      isNotShowErrorToast: false,
      data: body,
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );
};

// 查询空间空调信息
export const querySpaceConditioners = (params) => {
  var userObj = UserStorage.getInstance();
  const user = userObj.getUser();
  var pubname = 'sgadmin';
  var paramsss = `openid=${user.id}&pubname=${pubname}&projectId=Pj1101080255&userId=${user.id}&userName=${user.name}&userPhone=${user.phone}`;

  return request(
    `/sgadmin/duoduo-service/duoduoenv-service/ctrl/querySpaceConditioners?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    {
      from: 'sagacare',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    },
  );
};

//  手动控制 下发指令
export const changeManualTempHttp = (params) => {
  var userObj = UserStorage.getInstance();
  const user = userObj.getUser();
  var pubname = 'sgadmin';
  var paramsss = `openid=${user.id}&pubname=${pubname}&projectId=Pj1101080255&userId=${user.id}&userName=${user.name}&userPhone=${user.phone}`;

  return request(`/sgadmin/duoduo-service/duoduoenv-service/ctrl/set?${commonParams()}`, {
    method: 'POST',
    from: 'sagacare',
    data: params,
    headers: {
      'Content-Type': 'application/json',
    },
  });
};

//  获取地暖
export const querySapceFloorHeating = (params) => {
  return Request(
    `/sgadmin/duoduo-service/duoduoenv-service/ctrl/querySapceFloorHeating?${commonParams()}&spaceId=${
      params.spaceId
    }`,
    {
      skipErrorHandler: true,
      method: 'GET',
      from: 'sagacare',
      headers: {
        'Content-Type': 'application/json',
      },
      cancelToken: params.token,
    },
  );
};
