export const exportExcel = (tableClass, fileName) => {
  if (getExplorer() === 'ie') {
      //创建AX对象excel
      const curTbl = document.querySelector(tableClass).cloneNode(true);
      // eslint-disable-next-line no-undef
      let oXL = new ActiveXObject("Excel.Application");
      let oWB = oXL.Workbooks.Add();  //获取workbook对象
      let xlSheet = oWB.Worksheets(1); //激活当前sheet
      let sel = document.body.createTextRange();  //把表格中的内容移到TextRange中
      sel.moveToElementText(curTbl);
      sel.select;//全选TextRange中内容
      sel.execCommand("Copy");//复制TextRange中内容
      xlSheet.Paste();  //粘贴到活动的EXCEL中
      oXL.Visible = true;  //设置excel可见属性
      let fName = null;
      try {
          fName = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
      } catch (e) {
      } finally {
          oWB.SaveAs(fName);
          // oWB.Close(savechanges = false);
          oXL.Quit();
          oXL = null;
          // 下面代码用于解决IE call Excel的一个BUG, MSDN中提供的方法:
          // setTimeout(CollectGarbage, 1);
          // 由于不能清除(或同步)网页的受信任状态, 所以将导致SaveAs()等方法在
          // 下次调用时无效.
          window.location.reload();
      }

  } else {
      tableToExcel(tableClass, fileName, 'worksheet')
  }
}


const traverseNodes = (node, newTd) => {
  if (node.hasChildNodes) {
      const sonNodes = node.childNodes;
      const filterElement = ['button'];
      for (let sonNode of sonNodes) {
          if (!filterElement.includes(sonNode.nodeName.toLowerCase())) { // 对不必要对element过滤
              traverseNodes(sonNode, newTd);
          }

      }
  }
  return display(node, newTd);
}

const display = (node, newTd) => {
  const {nodeName, nodeValue} = node;
  let newSpan = document.createElement("span");
  newSpan.innerText = nodeValue;
  if (nodeName === 'INPUT' || nodeName === 'TEXTAREA') {  // 对 input 处理
      const {type, checked, value} = node;
      newSpan.innerText = value;
      if (type === 'radio' || type === 'checkbox') {
          console.log("type", type)
          newSpan.innerText = type === 'radio' ? (checked ? "●" : "○") : (checked ? "■" : "□");
          newSpan.style.fontSize = '16px';
          newSpan.style.paddingLeft = '15px';
      }
  }
  if (node.nodeName === 'IMG') {
      const {width, height} = node;
      newTd.appendChild(node);
      newTd.style.height = height + "px";
      newTd.style.width = width + "px";
  }
  if (newSpan.innerText.trim()) {
      newTd.appendChild(newSpan);
  }

  return newTd
}


const tableToExcel = (table, fileName, worksheet) => {
  const uri = 'data:application/vnd.ms-excel;base64,';
  // 定义文档的类型
  const template = '<html><head><meta charset="UTF-8"></head><body><table  border="1">{table}</table></body></html>';
  const base64 = function (s) {
      return window.btoa(unescape(encodeURIComponent(s)))
  };
  // 将template中的变量替换为页面内容ctx获取到的值
  const format = function (s, c) {
      return s.replace(/{(\w+)}/g, function (m, p) {
          return c[p];
      })
  }
  if (!table.nodeType) {
      table = document.querySelector(table).cloneNode(true);
  }

  let newTable = document.createElement("table");
  const trArray = table.getElementsByTagName('tr');
  for (let trItem of trArray) {
      let newTr = document.createElement("tr");
      const thArray = trItem.getElementsByTagName('th');
      const tdArray = trItem.getElementsByTagName('td');
      for (let thItem of thArray) {
          let newTh = document.createElement("th");
          const {rowSpan = 1, colSpan = 1, style} = thItem;
          traverseNodes(thItem, newTh);
          newTh.rowSpan = rowSpan; //跨行
          newTh.colSpan = colSpan; //跨列
          newTh.style = style; // 样式
          newTr.appendChild(newTh);
      }
      for (let tdItem of tdArray) {
          let newTd = document.createElement("td");
          const {rowSpan = 1, colSpan = 1, style} = tdItem;
          traverseNodes(tdItem, newTd);
          newTd.rowSpan = rowSpan; //跨行
          newTd.colSpan = colSpan; //跨列
          newTd.style = style; // 样式
          newTr.appendChild(newTd);
      }
      if (newTr.childNodes.length > 1) {
          newTable.appendChild(newTr);
      }
  }
  newTable.innerHTML = newTable.innerHTML.replace(/<td/g, "<td STYLE='MSO-NUMBER-FORMAT:\\@'");
  const ctx = {worksheet, table: newTable.innerHTML};  // 获取表单的名字和表单查询的内容
  const a = document.createElement("a");  // 虚拟一个a 标签
  // format()函数：通过格式操作使任意类型的数据转换成一个字符串
  // base64()：进行编码
  a.href = uri + base64(format(template, ctx));
  a.download = fileName + ".xls";//设置文件的名字
  a.click();// 下载
}

// 获取当前浏览器
const getExplorer = () => {
  const explorer = window.navigator.userAgent;
  if (explorer.indexOf("MSIE") >= 0) {  //ie
      return 'ie';
  }
  else if (explorer.indexOf("Firefox") >= 0) {  //firefox
      return 'Firefox';
  }
  else if (explorer.indexOf("Chrome") >= 0) {  //Chrome
      return 'Chrome';
  }
  else if (explorer.indexOf("Opera") >= 0) {   //Opera
      return 'Opera';
  }
  else if (explorer.indexOf("Safari") >= 0) { //Safari
      return 'Safari';
  }
}