const { ipcRenderer } = require('electron');

const winType = 'layout';

document.addEventListener('keydown', (e) => {
  const oEvent = e || window.oEvent;
  const bCtrlKey = oEvent.ctrlKey;
  const bShiftKey = oEvent.shiftKey;
  const code = e.which || e.keyCode;
  const bDeleteKey = code === 46; // delete
  const bDKey = code === 68;// d
  const bF5Key = code === 116;
  const bEscKey = code === 27;
  if (bF5Key) {
    ipcRenderer.send('refreshWindow', winType);
  }
  if (bCtrlKey && bShiftKey && bDKey) {
    ipcRenderer.send('openDevTools', winType);
  }
  if (bCtrlKey && bShiftKey && bDeleteKey) {
    ipcRenderer.send('openClearBox', winType);
  }
  if (bEscKey) {
    // console.log(2222, e);
    ipcRenderer.send('escape', winType);
  }
}, false);

// 户型地图页面，插入的js代码片段
if (location.href && location.href.indexOf('houseTypeLib') > -1) {
  window.onload = () => {
    const allmap = document.getElementById('allmap');
    allmap.addEventListener('click', (e) => {
      const tagName = e.target.tagName.toLowerCase();
      const className = e.target.className.toLowerCase();
      if (tagName === 'span' && className === 'labelname' && $) {
        const buildName = $(e.target).html();
        const buildId = $(e.target).attr('data-id');
        const cityCode = $('.hxk-select__address').attr('data-code');
        // parent.setHouseTypeLocation(cityCode, buildName, buildId);
        console.log(buildName, buildId, cityCode);
        localStorage['8a1a84f1-202c-465a-bc9f-9ff1b41c647b'] = cityCode;
        localStorage['de95e1db-47db-4ff5-9193-150973c5f356'] = buildName;
        // ipcRenderer.sendToHost(type, data);
        const data = `8a1a84f1-202c-465a-bc9f-9ff1b41c647b=${cityCode}&de95e1db-47db-4ff5-9193-150973c5f356=${buildName}`;
        ipcRenderer.send('sendCECPData', 102, data);
      }
    }, false);
  }
}

window.ipcRenderer = ipcRenderer;
