<?php
// A4 print layout
// Vars: $title, $meta, $company, $footer, $body
$format = 'a4';
$autoPrint = isset($_GET['autoprint']) && (string)$_GET['autoprint'] === '1';
$closeAfterPrint = isset($_GET['close_after_print']) && (string)$_GET['close_after_print'] === '1';
$isReportPrint = !empty($report_mode) || !empty(($print_options['report_mode'] ?? false));
?><!doctype html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= e($title ?? '') ?></title>
  <link rel="stylesheet" href="<?= e(asset('assets/vendor/fonts/cairo/cairo.css')) ?>">
  <link rel="stylesheet" href="<?= e(asset('assets/print/print_a4.css')) ?>">
  <?php if (($print_template ?? '') === 'quotation_v2'): ?>
    <link rel="stylesheet" href="<?= e(asset('assets/print/quotation_a4.css')) ?>">
  <?php endif; ?>
  <link rel="stylesheet" href="<?= e(asset('assets/print/purchase_invoice_a4.css')) ?>">
</head>
<body class="<?= $isReportPrint ? 'report-print-mode' : '' ?>">
  <div class="no-print print-toolbar">
    <button type="button" onclick="window.print()">طباعة</button>
    <button type="button" onclick="window.close()">إغلاق</button>
  </div>

  <div class="page-shell">
    <?php if ($isReportPrint): ?>
      <div class="page a4 page-report">
        <table class="report-paper-table">
          <thead>
            <tr><td class="report-paper-header"><?php require __DIR__ . '/partials/header.php'; ?></td></tr>
          </thead>
          <tbody>
            <tr><td class="report-paper-body"><div class="print-body report-print-body"><?= $body ?></div></td></tr>
          </tbody>
          <tfoot>
            <tr><td class="report-paper-footer"><?php require __DIR__ . '/partials/footer.php'; ?></td></tr>
          </tfoot>
        </table>
      </div>
    <?php else: ?>
      <div class="print-page-frame" aria-hidden="true">
        <div class="frame-top"></div>
        <div class="frame-side"></div>
        <div class="frame-bottom"></div>
      </div>

      <div class="page a4">
        <div class="page-content">
          <?php require __DIR__ . '/partials/header.php'; ?>
          <div class="print-body">
            <?= $body ?>
          </div>
          <?php require __DIR__ . '/partials/footer.php'; ?>
        </div>
      </div>
    <?php endif; ?>
  </div>

<?php if ($autoPrint): ?>
<script>
(function(){
  let printed = false;
  function finish(){
    if (printed) return;
    printed = true;
    if (<?= $closeAfterPrint ? 'true' : 'false' ?>) {
      setTimeout(function(){ window.close(); }, 250);
    }
  }
  window.addEventListener('afterprint', finish);
  const media = window.matchMedia ? window.matchMedia('print') : null;
  if (media && typeof media.addEventListener === 'function') {
    media.addEventListener('change', function(e){ if (!e.matches) finish(); });
  } else if (media && typeof media.addListener === 'function') {
    media.addListener(function(e){ if (!e.matches) finish(); });
  }
  window.addEventListener('load', function(){
    setTimeout(function(){ window.print(); }, 150);
    if (<?= $closeAfterPrint ? 'true' : 'false' ?>) {
      setTimeout(finish, 4000);
    }
  });
})();
</script>
<?php endif; ?>
</body>
</html>
