function exportZipFile(fileContent) {
  var data = JSON.parse(fileContent);
  console.log(data);
  var zip = buildZipFile(data.patients);
  if (data.isPureWeb) {
    zip.generateAsync({ type: 'blob' }).then(function (content) {
      // see FileSaver.js
      saveAs(content, data.fileName);
    });
  } else {
    zip.generateAsync({ type: 'base64' }).then(function (content) {
      var shell = window['FisShellApi'];
      if (shell) {
        shell.exportZipFile(content, data.fileName);
      }
    });
  }
}

function buildZipFile(patients) {
  var zip = new JSZip();
  patients.forEach((patient) => {
    var patientFolder = zip.folder(patient.name);
    buildExamsFolder(patientFolder, patient.exams);
  });
  return zip;
}

function buildExamsFolder(patientFolder, exams) {
  var examFolder = patientFolder.folder(exams.name);
  exams.forEach((exam) => {
    buildReportsFolder(examFolder, exam.reports);
    buildImagesFolder(examFolder, exam.examImages);
  });
}
function buildReportsFolder(examFolder, reports) {
  var reportsFolder = examFolder.folder(reports.name);
  reports.reportFiles.forEach((report) => {
    for (var i = 0; i < 100; i++) {
      var fileName = i + report.fileName;
      reportsFolder.file(fileName, report.fileBase64Data, { base64: true });
    }
  });
}
function buildImagesFolder(examFolder, examImages) {
  var imagesFolder = examFolder.folder(examImages.name);
  examImages.imageFiles.forEach((image) => {
    for (var i = 0; i < 100; i++) {
      var fileName = i + image.fileName;
      imagesFolder.file(fileName, image.fileBase64Data, { base64: true });
    }
  });
}

function canvasDataURL(path, obj, callback) {
  obj = JSON.parse(obj);
  var img = new Image();
  img.src = 'data:image/png;base64,' + path;
  img.setAttribute('crossOrigin', 'Anonymous');
  img.onload = function () {
    var that = this;
    // 默认按比例压缩
    var w = that.width,
      h = that.height,
      scale = w / h;
    w = obj.width || w;
    h = obj.height || w / scale;
    var quality = 100;
    //生成canvas
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    // 创建属性节点
    var anw = document.createAttribute('width');
    anw.nodeValue = w;
    var anh = document.createAttribute('height');
    anh.nodeValue = h;
    canvas.setAttributeNode(anw);
    canvas.setAttributeNode(anh);
    ctx.drawImage(that, 0, 0, w, h);

    // 图像质量
    if (obj.quality && obj.quality <= 1 && obj.quality > 0) {
      quality = obj.quality;
    }
    // quality值越小，所绘制出的图像越模糊
    var base64 = canvas.toDataURL('image/png', quality);
    // 回调函数返回base64的值
    //console.log(base64);
    callback(base64);
  };
}
