import { FormattedMessage, useModel } from 'umi';

import {
  changeAllAirHttp,
  changeAirHttp,
  setEquipeHttp,
  setallSocketHttp,
  getLampHttp,
  getFeedbackDocumentsHttp,
} from '@/services/sagacare_service/equipment';

import { Spin, Modal, message } from 'antd';
import { setallLamps } from '@/pages/Equipment/checLampStatus.js';

//开关 单个或者全部插座
export const changeSocket = (type, itemarr, status, selSocketType) => {
  //const { initialState } = useModel('@@initialState');//这里面不能这么用
  //debugger;
  const setType = status === '打开' ? 1 : 0; // 10关闭 12开启
  var paramsArr = [];

  itemarr.forEach((citem) => {
    (citem.equipList || []).forEach((item) => {
      //debugger;
      if (
        item.equipType == 'socket' &&
        (selSocketType.code == 'all' || item.infos.SocketType == selSocketType.code)
      ) {
        paramsArr.push({
          id: item.id,
          code: 'EquipSwitchSet',
          value: setType,
        });
      }
    });
  });

  if (type == 'all') {
    setTimeout(() => {
      message.success({
        content: `指令已经下发，设备会逐步${
          status === '打开' ? '开启' : '关闭'
        }，请耐心等待，勿频繁操作`,
        duration: 4,
      });
    }, 3000);
  }
  setallSocketHttp(paramsArr).then((res) => {
  
  });
};
//开关 单个或者全部灯
export const changeLight = (type, itemarr, getDeviceStatus, status) => {
  //const { initialState } = useModel('@@initialState');//这里面不能这么用
  //debugger;
  const setType = status === '打开' ? true : false; // 10关闭 12开启
  var paramsArr = [];

  itemarr.forEach((citem) => {
    (citem.equipList || []).forEach((item) => {
      if (item.equipType == 'light') {
        paramsArr.push({
          id: item.id,
          switch: setType,
        });
      }
    });
  });

  if (type == 'all') {
    setTimeout(() => {
      message.success({
        content: `指令已经下发，设备会逐步${
          status === '打开' ? '开启' : '关闭'
        }，请耐心等待，勿频繁操作`,
        duration: 4,
      });
    }, 3000);
  }

  setallLamps(paramsArr);
};

//开关 单个或者全部窗帘      0是关闭 1 是开启  2 是部分开启
export const changeCurtain = (type, itemarr, getDeviceStatus, status) => {
  const setType = status === '打开' ? 'EquipOnSet' : 'EquipOffSet';
  var paramsArr = [];
  itemarr.forEach((citem) => {
    (citem.equipList || []).forEach((item) => {
      if (item.equipType == 'curtain') {
        paramsArr.push({
          equipmentCategory: 'CFSSES',
          infoCode: setType,
          value: 1,
          objectId: item.id,
        });
      }
    });
  });

  //发送指令
  setEquipeHttp(paramsArr);
  message.success('指令已经下发');
  //这是在手动改变状态
  getDeviceStatus && getDeviceStatus();
};

//开关单个空调      0是关闭 1 是开启  2 是部分开启
export const changeAir = (sitem, index, getDeviceStatus) => {
  const itemId = sitem.airConditioner === 0 ? 12 : 10; // 10关闭 12开启
  const paramsObj = {
    objectId: sitem.spaceId, // 空间id
    valueType: 1, // 固定为1 1
    itemId: itemId,
  };
  changeAirHttp(paramsObj)
    .then((res) => {
      if (res.result == 'success') {
        message.success('指令已经下发');
        // // sitem.airConditioner = res.isClose ? 0 : 1; //改变某空间空调的状态
        // //mapCombineList[index]['airConditioner'] = res.isClose ? 0 : 1;
        // var mapCopy = JSON.parse(JSON.stringify(mapCombineList));
        // mapCopy[index]['airConditioner'] = res.isClose ? 0 : 1;
        // setMapCombineList(mapCopy);
        getDeviceStatus && getDeviceStatus();
      } else {
        message.error('操作失败，请重试');
      }
    })
    .catch((err) => {});
};

//开关全部空调
export const changeAllAir = (itemarr, getDeviceStatus, status, projectId) => {
  let paramsArr = [];
  itemarr.forEach((sitem) => {
    const itemId = status === '打开' ? 12 : 10; // 10关闭 12开启
    paramsArr.push({
      projectId: projectId,
      objectId: sitem.spaceId, // 空间id
      valueType: 1, // 固定为1 1
      itemId: itemId,
    });
  });
  setTimeout(() => {
    message.success({
      content: `指令已经下发，设备会逐步${
        status === '打开' ? '开启' : '关闭'
      }，请耐心等待，勿频繁操作`,
      duration: 4,
    });
  }, 3000);
  changeAllAirHttp(paramsArr)
    .then((res) => {
      if (res.result == 'success') {
        //message.success('指令已经下发');
        getDeviceStatus && getDeviceStatus();
      } else {
        //message.error('操作失败，请重试');
      }
    })
    .catch((err) => {
      console.log('err', err);
    });
};
// //查询 单个灯的状态
// export const getLamp = (sitem, callback, num) => {
//     num = num - 1;
//     getLampHttp(sitem.id).then((res) => {

//         if (res.result != 'success') return;
//         const arr = res.content || [];
//         //1开 0 关
//         let totalIsOpen = 0;
//         arr.forEach((item) => {
//             if (item.runStatus) {
//                 totalIsOpen = 1;
//             }

//         });
//         //说明状态改变
//         if (sitem.light !== totalIsOpen) {
//             callback && callback(totalIsOpen);
//             return;
//         } else if (num == 0) {
//             return;
//         }

//         getLamp(sitem, callback, num);

//     });
// };
//查询 单个空调的状态
// export const getAirInfo = (sitem, callback, num, projectId) => {

//     num = num - 1;
//     const paramsObj = {
//         objectId: sitem.id,
//         projectId: projectId,
//     };
//     getFeedbackDocumentsHttp(paramsObj).then((res) => {
//         //1开 0 关
//         const totalIsOpen = res.icon == 7 ? 0 : 1;

//         //说明状态改变
//         if (sitem.airConditioner !== totalIsOpen) {
//             callback && callback(totalIsOpen);
//             //debugger;
//             return;
//         } else if (num == 0) {
//             return;
//         }
//         // debugger;
//         //getAirInfo(sitem, callback, num, projectId);

//     });
// };
