export default function printHtml(options) {
    const tempFactory = (header, container, footer) => {
        return `
        <div class="page-footer" style="width: 968px;">${footer}</div>
        <table width="968" border="0" cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <td>
                        <!--place holder for the fixed-position header-->
                        <div class="page-header-space">${header}</div>
                    </td>
                </tr>
            </thead>
    
            <tbody>
                <tr>
                    <td style="page-break-inside: avoid;">` + container + `</td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <!--place holder for the fixed-position footer-->
                        <div class="page-footer-space"></div>
                    </td>
                </tr>
            </tfoot>
        </table>`;
    }
    let result = null;
    if(options.mul) {
        result = [];
        options.arr.forEach(item => {
            console.log(item,"item");
            result.push(tempFactory(item.headerDom.innerHTML,item.containerDom.innerHTML,item.footerDom.innerHTML))
        })
    }else {
        result = "";
        result = tempFactory(options.headerDom.innerHTML,options.containerDom.innerHTML,options.footerDom.innerHTML);
    }

    let height = 230
    let style = getStyle(height);
    let container = getContainer(result,options.mul);

    document.body.appendChild(style);
    document.body.appendChild(container);

    getLoadPromise(container).then(() => {
        window.print();
        document.body.removeChild(style);
        document.body.removeChild(container);
    });
}

// 设置打印样式
function getStyle(height) {
    let styleContent = `#print-container {
        display: none;
        // width:1028px;
        // position: fixed;
        // z-index: 100;
        // top: 0px;
        // left:0px
        // background-color: #ffffff;
      }
      @media print {
          body > :not(.print-container) {
              display: none;
          }
          html,
          body {
              display: block !important;
          }
          #print-container {
              display: block;
          }
          
      }
      .page-header,
      .page-header-space {
          height: ` + height + `px;
      }
  
      .page-footer,
      .page-footer-space {
          height: 90px;
      }
  
      .page-footer {
          position: fixed;
          bottom: 0;
          width: 968px;
          // border-top: 1px solid black;
          /* for demo */
          background: yellow;
          /* for demo */
          z-index:2000;
      }
  
      .page-header {
          position: fixed;
          top: 0mm;
          width: 968px;
        //   border-bottom: 1px solid black;
          /* for demo */
          background: yellow;
          /* for demo */
           z-index:2000;
      }
  
      .page-break-after {
          page-break-after: always;
      }
      .page-break-before {
        page-break-before: always;
    }
  
      @page {
          margin: 20mm 18mm 20mm 18mm;
      }
  
      @media print {
          thead {
              display: table-header-group;
          }
  
          tfoot {
              display: table-footer-group;
          }
  
          button {
              display: none;
          }
  
          body {
              margin: 0;
          }
          .risp-page-header {
            // position: fixed;
          }
          .risp-page-nav {
            // padding-top: 64px;
          }
          input[type="checkbox" i] {
            background-color: initial;
            cursor: default;
            appearance: auto;
            box-sizing: border-box;
            margin: 3px 3px 3px 4px;
            padding: initial;
            border: initial;
          }
        }
      }
      `;
    let style = document.createElement("style");
    style.innerHTML = styleContent;
    return style;
}

// 清空打印内容
function cleanPrint() {
    let div = document.getElementById("print-container");
    if (div) {
        document.querySelector("body").removeChild(div);
    }
}

// 新建DOM，将需要打印的内容填充到DOM
function getContainer(result,mul) {
    cleanPrint();
    let container = document.createElement("div");
    container.setAttribute("id", "print-container");
    container.classList.add("print-container");
    container.classList.add("doctopdf");
    let html= "";
    if(mul) {
        result.forEach((item,i) => {
            html += `<div class="page-break-before print-container-${i}">${item}</div>`
        })
    }else {
        html = result
    }
    container.innerHTML = html;
    console.log(html);
    return container;
}

// 图片完全加载后再调用打印方法
function getLoadPromise(dom) {
    let imgs = dom.querySelectorAll("img");
    imgs = [].slice.call(imgs);

    if (imgs.length === 0) {
        return Promise.resolve();
    }

    let finishedCount = 0;
    return new Promise(resolve => {
        function check() {
            finishedCount++;
            if (finishedCount === imgs.length) {
                resolve();
            }
        }
        imgs.forEach(img => {
            img.addEventListener("load", check);
            img.addEventListener("error", check);
        });
    });
}