<?php
// Unified reports print layout
$autoPrint = isset($_GET['autoprint']) && (string)$_GET['autoprint'] === '1';
$closeAfterPrint = isset($_GET['close_after_print']) && (string)$_GET['close_after_print'] === '1';
$company = is_array($company ?? null) ? $company : [];
$meta = is_array($meta ?? null) ? $meta : [];
$footer = is_array($footer ?? null) ? $footer : [];
$filters = is_array($filters ?? null) ? $filters : [];
$companyName = trim((string)($company['name'] ?? '')) ?: 'اسم الشركة';
$companySub = trim((string)($company['subtitle'] ?? ($company['sub'] ?? '')));
$companyAddress = trim((string)($company['address'] ?? ''));
$logoPath = trim((string)($company['logo_path'] ?? ''));
$logoUrl = $logoPath !== '' ? asset(ltrim($logoPath, '/')) : '';
$contactBits = [];
if (!empty($company['phone'])) $contactBits[] = (string)$company['phone'];
if (!empty($company['email'])) $contactBits[] = (string)$company['email'];
if (!empty($company['website'])) $contactBits[] = (string)$company['website'];
if (!empty($company['tax_no'])) $contactBits[] = 'الرقم الضريبي: ' . (string)$company['tax_no'];
$reportTitle = trim((string)($title ?? 'تقرير'));
$reportDateValue = trim((string)($reportDate ?? $meta['date'] ?? date('Y-m-d')));
$generatedBy = trim((string)($userName ?? ''));
$periodFrom = trim((string)($filters['from'] ?? ''));
$periodTo = trim((string)($filters['to'] ?? ''));
$reportPeriod = ($periodFrom !== '' || $periodTo !== '') ? (($periodFrom !== '' ? $periodFrom : '...') . ' - ' . ($periodTo !== '' ? $periodTo : '...')) : trim((string)($meta['period'] ?? ''));
$printedAt = date('Y-m-d H:i');
$footerNote = trim((string)($footer['note'] ?? 'هذا التقرير صادر من مركز التقارير.'));
?><!doctype html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= e($reportTitle) ?></title>
  <link rel="stylesheet" href="<?= e(asset('assets/vendor/fonts/cairo/cairo.css')) ?>">
  <style>
    @page { size: A4 portrait; margin: 8mm 7mm 8mm; }
    *{box-sizing:border-box}
    html,body{margin:0;padding:0;background:#eef2f6;color:#10202f;font-family:'Cairo',Tahoma,Arial,sans-serif;font-size:11px;direction:rtl;overflow-x:hidden}
    .no-print{display:flex;gap:10px;justify-content:center;align-items:center;padding:14px 0}
    .no-print button{border:1px solid #d8e1ea;background:#fff;border-radius:12px;padding:10px 26px;font:800 14px 'Cairo';cursor:pointer}
    .report-sheet{width:190mm;max-width:calc(100vw - 28px);min-height:277mm;margin:0 auto 14px;background:#fff;border:1px solid #d7dee7;box-shadow:0 8px 24px rgba(15,23,42,.14);padding:8mm 7mm 7mm;overflow:hidden}
    .report-page-table{width:100%;max-width:100%;border-collapse:collapse;border-spacing:0;direction:rtl;table-layout:fixed}
    .report-page-table>thead{display:table-header-group}.report-page-table>tfoot{display:table-footer-group}
    .report-page-table>thead>tr>td,.report-page-table>tbody>tr>td,.report-page-table>tfoot>tr>td{padding:0;border:0;vertical-align:top}
    .report-top-ribbon{height:7mm;border-radius:0 0 8px 8px;background:linear-gradient(120deg,#245a8d 0%,#16324a 45%,#1f2937 66%,#f1f5f9 66.2%,#f1f5f9 100%);margin-bottom:5mm}
    .report-header{border-bottom:1px solid #d8e1ea;padding-bottom:3mm;margin-bottom:4mm}
    .report-header-grid{display:grid;grid-template-columns:minmax(0,1.16fr) minmax(0,.84fr);gap:8px;align-items:stretch;max-width:100%}
    .brand-card,.doc-card{border:1px solid #d9e3ec;border-radius:13px;background:#fbfdff;overflow:hidden;min-width:0}
    .brand-card{padding:9px 13px}.brand-wrap{display:flex;align-items:center;gap:11px;justify-content:space-between}.brand-copy{min-width:0;flex:1;text-align:right}.brand-name{font-size:20px;font-weight:900;color:#10273a;line-height:1.15}.brand-sub,.brand-meta{font-size:9.2px;color:#66798a;line-height:1.55}.brand-logo{width:56px;height:56px;border-radius:50%;border:1px solid #d7e2ec;background:#fff;display:flex;align-items:center;justify-content:center;padding:3px;flex:0 0 56px}.brand-logo img{max-width:100%;max-height:100%;object-fit:contain;border-radius:50%}
    .doc-card{padding:6px 8px}.doc-row{display:flex;justify-content:space-between;gap:6px;border-bottom:1px solid #edf2f7;padding:3px 0;font-size:9.3px;line-height:1.45;min-width:0}.doc-row:last-child{border-bottom:0}.doc-row span{color:#6b7c8d;font-weight:800;white-space:nowrap}.doc-row strong{color:#10273a;font-weight:900;text-align:left;direction:rtl;min-width:0;max-width:68%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
    .report-body-slot{padding-top:0}
    .report-footer{border-top:1px solid #d8e1ea;margin-top:5mm;padding-top:2.5mm;color:#66798a;font-size:8.4px;line-height:1.55}
    .footer-note{font-weight:800;color:#4f6376;margin-bottom:1.5mm;text-align:right}.footer-line{display:flex;justify-content:center;gap:9px;flex-wrap:wrap}.footer-line span:not(:last-child)::after{content:'|';margin-inline-start:9px;color:#c2ccd6}.footer-meta{display:flex;justify-content:space-between;margin-top:2mm;color:#4f6376;font-weight:700}
    .report-print-section{margin:0}.report-title-line{display:grid;grid-template-columns:minmax(0,1.25fr) minmax(0,.75fr);gap:8px;align-items:start;margin:0 0 3mm;max-width:100%}.report-title-line h1{margin:0 0 1mm;font-size:19px;line-height:1.25;font-weight:900;color:#10273a;border-right:4px solid #245a8d;padding-right:9px}.report-title-line p{margin:0;color:#66798a;font-size:10.5px;line-height:1.6;padding-right:13px}.report-generated-box{border:1px solid #d9e3ec;border-radius:12px;background:#fff;padding:6px 8px}.report-generated-box div{display:flex;justify-content:space-between;border-bottom:1px solid #edf2f7;padding:4px 0;font-size:9.8px}.report-generated-box div:last-child{border-bottom:0}.report-generated-box span{color:#6b7c8d;font-weight:800}.report-generated-box strong{color:#10273a;font-weight:900}
    .report-compact-panel{margin:0 0 3mm}.report-compact-panel h3,.report-table-title{margin:0 0 1.5mm;font-size:12px;font-weight:900;color:#10273a}.report-mini-table,.report-summary-table{width:100%;max-width:100%;border-collapse:collapse;table-layout:fixed}.report-mini-table th,.report-summary-table th{background:#f6f8fa;color:#6b7c8d;font-weight:900;width:15%;border:1px solid #e2e8f0;padding:4px 6px;font-size:9.2px}.report-mini-table td,.report-summary-table td{border:1px solid #e2e8f0;padding:4px 6px;font-size:9.2px;font-weight:900;color:#10273a;text-align:center}.report-mini-table th{text-align:right}.report-mini-table td{text-align:center}
    .report-print-table{width:100%;max-width:100%;border-collapse:collapse;page-break-inside:auto;border:1px solid #d9e3ec;direction:rtl;table-layout:fixed}.report-print-table thead{display:table-header-group}.report-print-table th{background:#16324a;color:#fff;font-size:8.4px;font-weight:900;padding:4.6px 3px;border:1px solid rgba(255,255,255,.14);text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.report-print-table td{font-size:8px;line-height:1.32;padding:3.2px 3px;border:1px solid #e7edf3;text-align:center;vertical-align:middle;color:#10202f;overflow:hidden;text-overflow:ellipsis;word-break:break-word}.report-print-table td:first-child,.report-print-table th:first-child{text-align:right}.report-print-table tbody tr:nth-child(even) td{background:#fbfdff}.report-print-table tr{break-inside:avoid;page-break-inside:avoid}
    @media print{
      html,body{width:auto!important;min-width:0!important;max-width:none!important;background:#fff!important;overflow:visible!important;-webkit-print-color-adjust:exact;print-color-adjust:exact}
      .no-print{display:none!important}.report-sheet{width:100%!important;max-width:100%!important;min-height:auto;margin:0;border:0;box-shadow:none;padding:0;background:#fff;overflow:visible!important}.report-top-ribbon{margin-bottom:4mm}.report-header{margin-bottom:3mm;padding-bottom:2.5mm}.report-footer{margin-top:5mm;break-inside:avoid;page-break-inside:avoid}.report-title-line,.report-compact-panel{break-inside:avoid;page-break-inside:avoid}.report-page-table,.report-header,.report-header-grid,.report-body-slot,.report-print-section,.report-print-table{max-width:100%!important}.report-print-table thead{display:table-header-group}.report-print-table tr{break-inside:avoid;page-break-inside:avoid}
    }
  </style>
</head>
<body>
  <div class="no-print"><button onclick="window.print()">طباعة</button><button onclick="window.close()">إغلاق</button></div>
  <main class="report-sheet">
    <table class="report-page-table">
      <thead><tr><td>
        <div class="report-top-ribbon"></div>
        <header class="report-header">
          <div class="report-header-grid">
            <section class="brand-card">
              <div class="brand-wrap">
                <div class="brand-copy">
                  <div class="brand-name"><?= e($companyName) ?></div>
                  <?php if ($companySub !== ''): ?><div class="brand-sub"><?= e($companySub) ?></div><?php endif; ?>
                  <?php if ($companyAddress !== ''): ?><div class="brand-sub"><?= e($companyAddress) ?></div><?php endif; ?>
                  <?php if ($contactBits): ?><div class="brand-meta"><?= e(implode(' | ', $contactBits)) ?></div><?php endif; ?>
                </div>
                <?php if ($logoUrl !== ''): ?><div class="brand-logo"><img src="<?= e($logoUrl) ?>" alt="logo"></div><?php endif; ?>
              </div>
            </section>
            <section class="doc-card">
              <div class="doc-row"><span>نوع المستند</span><strong>تقرير</strong></div>
              <div class="doc-row"><span>اسم التقرير</span><strong><?= e($reportTitle) ?></strong></div>
              <div class="doc-row"><span>التاريخ</span><strong><?= e(substr($reportDateValue,0,10)) ?></strong></div>
              <?php if ($reportPeriod !== ''): ?><div class="doc-row"><span>الفترة</span><strong><?= e($reportPeriod) ?></strong></div><?php endif; ?>
              <?php if ($generatedBy !== ''): ?><div class="doc-row"><span>المستخدم</span><strong><?= e($generatedBy) ?></strong></div><?php endif; ?>
            </section>
          </div>
        </header>
      </td></tr></thead>
      <tbody><tr><td class="report-body-slot"><?= $body ?></td></tr></tbody>
      <tfoot><tr><td>
        <footer class="report-footer">
          <div class="footer-note"><?= e($footerNote) ?></div>
          <div class="footer-line">
            <?php if ($companyName !== ''): ?><span><?= e($companyName) ?></span><?php endif; ?>
            <?php if ($companyAddress !== ''): ?><span><?= e($companyAddress) ?></span><?php endif; ?>
            <?php foreach ($contactBits as $part): ?><span><?= e($part) ?></span><?php endforeach; ?>
          </div>
          <div class="footer-meta"><span>تاريخ الطباعة: <?= e($printedAt) ?></span><span>نسخة مطابقة للطباعة و PDF</span></div>
        </footer>
      </td></tr></tfoot>
    </table>
  </main>
<?php if ($autoPrint): ?>
<script>(function(){window.addEventListener('load',function(){setTimeout(function(){window.print();},150);});<?php if ($closeAfterPrint): ?>window.addEventListener('afterprint',function(){setTimeout(function(){window.close();},250);});<?php endif; ?>})();</script>
<?php endif; ?>
</body>
</html>
