<?php
$companyName = trim((string)($company['name'] ?? 'اسم الشركة'));
if ($companyName === '') { $companyName = 'اسم الشركة'; }
$itemName = (string)($item['name'] ?? '');
$barcodeValue = trim((string)($item['barcode'] ?? ''));
$showPrice = isset($showPrice) ? (bool)$showPrice : true;
$showItemName = isset($showItemName) ? (bool)$showItemName : true;
$priceText = '( ' . format_money((float)($item['price'] ?? 0)) . ' EGP )';
?><!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')) ?>">
  <style>
    :root{--label-w:<?= e((string)$widthMm) ?>mm;--label-h:<?= e((string)$heightMm) ?>mm}
    @page{size:var(--label-w) var(--label-h);margin:0}
    *{box-sizing:border-box}
    body{margin:0;background:#eef2f7;font-family:'Cairo',Tahoma,Arial,sans-serif;color:#111827}
    .toolbar{display:flex;gap:8px;justify-content:center;align-items:center;padding:10px}
    .toolbar button{padding:8px 14px;border:1px solid #d7deea;border-radius:12px;background:#fff;font:700 13px Cairo,cursive;cursor:pointer}
    .labels{display:flex;flex-direction:column;align-items:center;gap:14px;padding:0 0 20px}
    .label{width:var(--label-w);height:var(--label-h);background:#fff;display:flex;flex-direction:column;justify-content:space-between;align-items:center;padding:3mm 3.5mm 2.6mm;page-break-after:always;overflow:hidden}
    .company{font-size:3.6mm;font-weight:800;line-height:1.1;text-align:center;width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
    .item-name{font-size:3mm;font-weight:700;line-height:1.15;text-align:center;width:100%;margin-top:0.6mm;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
    .barcode-wrap{width:100%;flex:1;display:flex;align-items:center;justify-content:center;padding:1.2mm 0 .5mm}
    .barcode-wrap svg{width:100%;height:100%}
    .price{font-size:4.2mm;font-weight:800;line-height:1;text-align:center}
    .meta{font-size:2.4mm;color:#4b5563;line-height:1;text-align:center}
    @media print{
      body{background:#fff}
      .toolbar{display:none !important}
      .labels{gap:0;padding:0}
      .label{margin:0}
    }
  </style>
</head>
<body>
  <div class="toolbar no-print">
    <button type="button" onclick="window.print()">طباعة</button>
    <button type="button" onclick="window.close()">إغلاق</button>
  </div>
  <div class="labels">
    <?php for ($i = 0; $i < $qty; $i++): ?>
      <div class="label">
        <div class="company"><?= e($companyName) ?></div>
        <?php if ($showItemName && $itemName !== ''): ?>
        <div class="item-name"><?= e($itemName) ?></div>
        <?php endif; ?>
        <div class="barcode-wrap"><?= $barcodeSvg ?></div>
        <?php if ($showPrice): ?>
        <div class="price"><?= e($priceText) ?></div>
        <?php endif; ?>
      </div>
    <?php endfor; ?>
  </div>
  <?php if ($autoPrint): ?>
  <script>
    (function(){
      let closed = false;
      function finish(){
        if (closed) return; closed = 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); });
    })();
  </script>
  <?php endif; ?>
</body>
</html>
