import DonMessage from "@/utils/message";

function exportExcel(xlsx,fileSaver,filename,tableId,isClerFirstCell) {
  DonMessage.success("导出数据3")
  console.log('Success')
  /* generate workbook object from table */
  // 判断要导出的节点中是否有fixed的表格，如果有，转换excel时先将该dom移除，然后append回去，
  //  .Devicetablee要导出的是哪一个表格
  let dom=document.getElementById(tableId).cloneNode(true);
  if(dom.querySelector(".el-table__fixed-right")!=null){
    dom.removeChild(dom.querySelector(".el-table__fixed-right"))
  }
  var wb;
  let tableHeader = dom.querySelector('.el-table__header-wrapper');
  let tableBody = dom.querySelector('.el-table__body-wrapper');
  //隐藏列
  DonMessage.success("导出数据2")
  let headerDom = tableHeader.childNodes[0].querySelectorAll('th');
  for (let key in headerDom) {
    console.log(headerDom[key])
    if (headerDom[key].innerText === '操作') {
      headerDom[key].remove();
    }

  }
  let tableList = tableBody.childNodes[0].childNodes[1].querySelectorAll('td');
  for (let key = 0; key < tableList.length; key++) {
    if (tableList[key].querySelectorAll('.el-button').length > 0) {
      tableList[key].remove();

    }

   /*
    console.log(tableList[key])
    console.log('-------------------------')*/
  }


  var xlsxParam = { raw: true };
  wb = xlsx.utils.table_to_book(dom,xlsxParam);
  wb.Sheets.Sheet1['!cols'][0]
  /* get binary string as output */
  var wbout = xlsx.write(wb, {
    bookType: "xlsx",
    bookSST: true,
    type: "array",
  });
  try {
    //  name+'.xlsx'表示导出的excel表格名字
    fileSaver.saveAs(
      new Blob([wbout], { type: "application/octet-stream;charset=utf-8" }),
      filename + ".xlsx"
    );
  } catch (e) {
    if (typeof console !== "undefined") console.log(e, wbout);
  }
  return wbout;
}
export { //很关键
  exportExcel
}
