export const printDocument = (htmlContent: string, docTitle: string = '图文报告') => {
  const originalTitle = document.title;
  document.title = docTitle;
  const iframe = document.createElement('iframe');
  iframe.style.position = 'fixed';
  iframe.style.right = '0';
  iframe.style.bottom = '0';
  iframe.style.width = '0';
  iframe.style.height = '0';
  iframe.style.border = '0';
  document.body.appendChild(iframe);

  const win = iframe.contentWindow;
  const doc = iframe.contentDocument || win?.document;
  if (doc && win) {
    doc.open();
    doc.write(`
      <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <title>${docTitle}</title>
        <style>
          @page { size: A4; margin: 15mm 10mm; }
          * { box-sizing: border-box; }
          body { margin: 0; padding: 0; font-family: SimSun, "Microsoft YaHei", serif; color: #1E293B; background: white; }
          .content { width: 100%; min-height: 277mm; margin: 0 auto; }
          img { max-width: 100%; height: auto; display: block; margin: 8px auto; }
          p { margin: 0; padding: 0; line-height: 1.5; }
          h1 { font-size: 20px; margin: 16px 0 12px; font-weight: 600; text-align: center; }
          strong, b { font-weight: 600; }
          u { text-decoration: underline; }
          table { width: 100%; border-collapse: collapse; margin: 16px 0; table-layout: fixed; }
          td { padding: 8px; border: 1px solid #e2e8f0; vertical-align: top; }
          .image-placeholder { border: 2px dashed #cbd5e1; border-radius: 8px; padding: 16px; margin-bottom: 8px; background: #f8fafc; min-height: 70px; display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative; }
          .image-placeholder.has-image { border: none; background: transparent; padding: 0; min-height: 0; }
          .delete-btn { display: none !important; }
          .image-placeholder:not(.has-image) { display: none !important; }
          .template-info-section { position: relative; margin-bottom: 16px; }
          .smart-field-wrapper { display: inline-flex; align-items: baseline; margin: 0; vertical-align: baseline; }
          .smart-field-wrapper .field-label { color: #64748b; user-select: none; }
          .smart-field-wrapper .field-value { min-width: 24px; padding: 0 2px; margin: 0; border: 1px solid #cbd5e1; border-radius: 2px; display: inline-block; background: #f8fafc; color: #0f172a; line-height: inherit; font-size: inherit; vertical-align: baseline; box-sizing: border-box; outline: none; text-align: center; }
          .report-signature-img { max-width: 120px; max-height: 40px; width: auto; height: auto; object-fit: contain; vertical-align: middle; display: inline-block; }
          .ai-region { border: none !important; background: transparent !important; padding: 0 !important; margin: 0 !important; }
          .ai-region > [contenteditable="false"] { display: none !important; }
          @media print {
            .smart-field-wrapper .field-value { outline: none !important; box-shadow: none !important; border: none !important; border-bottom: 1px solid #000 !important; border-radius: 0 !important; background: transparent !important; padding: 0 2px 0px 2px !important; line-height: 1 !important; }
            .smart-field-wrapper .field-value.no-underline { border-bottom: none !important; }
          }
        </style>
      </head>
      <body>
        <div class="content">${htmlContent}</div>
      </body>
      </html>
    `);
    doc.close();
    win.focus();
    setTimeout(() => {
      win.print();
      document.title = originalTitle;
      setTimeout(() => {
        if (iframe.parentNode) document.body.removeChild(iframe);
      }, 1000);
    }, 300);
  }
};
