import React from 'react';
import ReactDOM from 'react-dom';
import Simple_table from './modal/simple_table';

const Print = (data, printTemp) => {
  //创建iframe标签
  const iframe = document.createElement('iframe');

  iframe.setAttribute(
    'style',
    'position:absolute;width:0px;height:0px;left:500px;top:500px;size:auto;margin:0mm;',
  );

  //将iframe添加到body
  document.body.appendChild(iframe);

  //创建打印内容document对象
  let doc = iframe.contentWindow.document;
  let tem = '';

  switch (printTemp) {
    case 'simple_table':
      tem = <Simple_table data={data}/>;
      break;
    default:
      tem = <Simple_table data={data}/>;
  }
  ReactDOM.render(tem, doc);

  setTimeout(() => {
    doc.close();

    //打印
    iframe.contentWindow.focus();
    iframe.contentWindow.print();

    //清理iframe
    if (navigator.userAgent.indexOf('MSIE') > 0) {
      document.body.removeChild(iframe);
    }
  }, 1000);
};

export default Print;
