<!DOCTYPE html>
<html lang="pl">
<head>
  <meta charset="UTF-8">
  <title>Paragon <?= $e((string) ($receipt['receipt_number'] ?? '')) ?></title>
  <style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: "DejaVu Sans", "Segoe UI", Arial, sans-serif; font-size: 12px; color: #1a1a1a; padding: 20mm 15mm; }
    .receipt-print { max-width: 700px; margin: 0 auto; }
    .receipt-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 20px; padding-bottom: 12px; border-bottom: 2px solid #333; }
    .receipt-header__seller { flex: 1; }
    .receipt-header__seller strong { font-size: 14px; display: block; margin-bottom: 4px; }
    .receipt-header__title { text-align: right; }
    .receipt-header__title h1 { font-size: 18px; font-weight: 700; margin-bottom: 4px; }
    .receipt-header__title .receipt-number { font-size: 14px; font-weight: 600; }
    .receipt-buyer { margin-bottom: 16px; padding: 8px 12px; border: 1px solid #ccc; }
    .receipt-buyer strong { display: block; margin-bottom: 4px; font-size: 11px; text-transform: uppercase; color: #666; }
    .receipt-items { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
    .receipt-items th { background: #f0f0f0; text-align: left; padding: 6px 8px; font-size: 11px; border-bottom: 1px solid #999; }
    .receipt-items td { padding: 5px 8px; border-bottom: 1px solid #ddd; }
    .receipt-items .text-right { text-align: right; }
    .receipt-items .text-nowrap { white-space: nowrap; }
    .receipt-items tfoot td { border-top: 2px solid #333; font-weight: 700; padding-top: 8px; }
    .receipt-meta { margin-top: 16px; font-size: 11px; color: #555; }
    .receipt-meta dt { display: inline; font-weight: 600; }
    .receipt-meta dd { display: inline; margin: 0 16px 0 4px; }
    @media print {
      body { padding: 10mm; }
      .no-print { display: none !important; }
    }
  </style>
</head>
<body>
<?php
$receiptData = is_array($receipt ?? null) ? $receipt : [];
$sellerData = is_array($seller ?? null) ? $seller : [];
$buyerData = is_array($buyer ?? null) ? $buyer : null;
$itemsList = is_array($items ?? null) ? $items : [];
$totalGross = (float) ($receiptData['total_gross'] ?? 0);
?>
<div class="receipt-print">
  <div class="receipt-header">
    <div class="receipt-header__seller">
      <strong><?= $e((string) ($sellerData['company_name'] ?? '')) ?></strong>
      <?php if (($sellerData['tax_number'] ?? '') !== ''): ?>
        <div>NIP: <?= $e((string) $sellerData['tax_number']) ?></div>
      <?php endif; ?>
      <div><?= $e((string) ($sellerData['street'] ?? '')) ?></div>
      <div><?= $e((string) ($sellerData['postal_code'] ?? '')) ?> <?= $e((string) ($sellerData['city'] ?? '')) ?></div>
      <?php if (($sellerData['phone'] ?? '') !== ''): ?>
        <div>Tel: <?= $e((string) $sellerData['phone']) ?></div>
      <?php endif; ?>
    </div>
    <div class="receipt-header__title">
      <h1>PARAGON</h1>
      <div class="receipt-number"><?= $e((string) ($receiptData['receipt_number'] ?? '')) ?></div>
    </div>
  </div>

  <?php if ($buyerData !== null): ?>
    <div class="receipt-buyer">
      <strong>Nabywca</strong>
      <?php if (($buyerData['company_name'] ?? '') !== ''): ?>
        <div><?= $e((string) $buyerData['company_name']) ?></div>
      <?php endif; ?>
      <?php if (($buyerData['name'] ?? '') !== ''): ?>
        <div><?= $e((string) $buyerData['name']) ?></div>
      <?php endif; ?>
      <?php if (($buyerData['tax_number'] ?? '') !== ''): ?>
        <div>NIP: <?= $e((string) $buyerData['tax_number']) ?></div>
      <?php endif; ?>
      <div><?= $e((string) ($buyerData['street'] ?? '')) ?>, <?= $e((string) ($buyerData['postal_code'] ?? '')) ?> <?= $e((string) ($buyerData['city'] ?? '')) ?></div>
    </div>
  <?php endif; ?>

  <table class="receipt-items">
    <thead>
      <tr>
        <th>Lp.</th>
        <th>Nazwa</th>
        <th class="text-right">Ilosc</th>
        <th class="text-right">Cena</th>
        <th class="text-right">Wartosc</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($itemsList as $idx => $item): ?>
        <tr>
          <td><?= $e((string) ($idx + 1)) ?></td>
          <td><?= $e((string) ($item['name'] ?? '')) ?></td>
          <td class="text-right text-nowrap"><?= $e((string) ($item['quantity'] ?? 0)) ?></td>
          <td class="text-right text-nowrap"><?= $e(number_format((float) ($item['price'] ?? 0), 2, '.', ' ')) ?></td>
          <td class="text-right text-nowrap"><?= $e(number_format((float) ($item['total'] ?? 0), 2, '.', ' ')) ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
    <tfoot>
      <tr>
        <td colspan="4" class="text-right">Razem brutto</td>
        <td class="text-right text-nowrap"><?= $e(number_format($totalGross, 2, '.', ' ')) ?> PLN</td>
      </tr>
    </tfoot>
  </table>

  <dl class="receipt-meta">
    <?php $issueDatePrint = (string) ($receiptData['issue_date'] ?? '-'); ?>
    <dt>Data wystawienia:</dt><dd><?= $e(strlen($issueDatePrint) >= 16 ? substr($issueDatePrint, 0, 16) : $issueDatePrint) ?></dd>
    <dt>Data sprzedazy:</dt><dd><?= $e((string) ($receiptData['sale_date'] ?? '-')) ?></dd>
    <?php if (($receiptData['order_reference_value'] ?? null) !== null): ?>
      <dt>Nr referencyjny:</dt><dd><?= $e((string) $receiptData['order_reference_value']) ?></dd>
    <?php endif; ?>
  </dl>
</div>
<script>window.print();</script>
</body>
</html>
