import React from 'react';
import PropTypes from 'prop-types';
import ExportJsonExcel from 'js-export-excel';
import JSZip from 'jszip';

// 导出Excel表格
const ExportExcel = (props) => {
  let option = {};
  option.fileName = props.name;
  option.datas = props.content;

  let toExcel = new ExportJsonExcel(option); //new
  toExcel.saveExcel();
};
ExportExcel.defaultProps = {
  name: 'excel',
  content: [],
};

ExportExcel.propTypes = {
  name: PropTypes.string, // 下载文件名称
  content: PropTypes.array, // 下载文件内容
};

// 导出压缩Excel表格
const ExportExcelZip = (props) => {
  let option = {};
  option.fileName = props.name;
  option.saveAsBlob = true;

  option.datas = props.content;

  let toExcel = new ExportJsonExcel(option); //new
  let file = toExcel.saveExcel();

  // 压缩文件
  let zip = new JSZip();

  // 多个excel 依次加入(fileName不能相同)
  zip.file(file.name, file);

  zip.generateAsync({ type: 'blob' }).then(function (content) {
    // see FileSaver.js
    saveAs(content, `${props.name}.zip`); // 下载文件
  });
};
ExportExcelZip.defaultProps = {
  name: 'excel',
  content: [],
};

ExportExcelZip.propTypes = {
  name: PropTypes.string, // 下载文件名称
  content: PropTypes.array, // 下载文件内容
};

export { ExportExcel, ExportExcelZip };