<?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 : [];
$orderIdVal = (int) ($orderId ?? 0);
$configNameVal = (string) ($configName ?? '');
$receiptNumber = (string) ($receiptData['receipt_number'] ?? '');
$totalGross = (float) ($receiptData['total_gross'] ?? 0);
?>

<section class="card">
  <div class="order-details-head">
    <div>
      <a href="/orders/<?= $e((string) $orderIdVal) ?>" class="order-back-link">&larr; <?= $e($t('receipts.show.back')) ?></a>
      <h2 class="section-title mt-12"><?= $e($t('receipts.show.title')) ?> <?= $e($receiptNumber) ?></h2>
    </div>
    <div class="order-details-head__actions">
      <a href="/orders/<?= $e((string) $orderIdVal) ?>/receipt/<?= $e((string) ($receiptData['id'] ?? '')) ?>/print" target="_blank" class="btn btn--secondary"><?= $e($t('receipts.show.print')) ?></a>
      <a href="/orders/<?= $e((string) $orderIdVal) ?>/receipt/<?= $e((string) ($receiptData['id'] ?? '')) ?>/pdf" class="btn btn--primary"><?= $e($t('receipts.show.pdf')) ?></a>
      <a href="/orders/<?= $e((string) $orderIdVal) ?>" class="btn btn--secondary"><?= $e($t('receipts.show.back')) ?></a>
    </div>
  </div>

  <div class="form-grid-2 mt-16">
    <div>
      <h3 class="section-title"><?= $e($t('receipts.show.seller')) ?></h3>
      <dl class="order-kv mt-8">
        <dt>Firma</dt><dd><?= $e((string) ($sellerData['company_name'] ?? '-')) ?></dd>
        <dt>NIP</dt><dd><?= $e((string) ($sellerData['tax_number'] ?? '-')) ?></dd>
        <dt>Adres</dt><dd><?= $e((string) ($sellerData['street'] ?? '')) ?>, <?= $e((string) ($sellerData['postal_code'] ?? '')) ?> <?= $e((string) ($sellerData['city'] ?? '')) ?></dd>
        <dt>Telefon</dt><dd><?= $e((string) ($sellerData['phone'] ?? '-')) ?></dd>
        <dt>Email</dt><dd><?= $e((string) ($sellerData['email'] ?? '-')) ?></dd>
      </dl>
    </div>
    <?php if ($buyerData !== null): ?>
      <div>
        <h3 class="section-title"><?= $e($t('receipts.show.buyer')) ?></h3>
        <dl class="order-kv mt-8">
          <?php if (($buyerData['company_name'] ?? '') !== ''): ?>
            <dt>Firma</dt><dd><?= $e((string) $buyerData['company_name']) ?></dd>
          <?php endif; ?>
          <?php if (($buyerData['name'] ?? '') !== ''): ?>
            <dt>Nazwa</dt><dd><?= $e((string) $buyerData['name']) ?></dd>
          <?php endif; ?>
          <?php if (($buyerData['tax_number'] ?? '') !== ''): ?>
            <dt>NIP</dt><dd><?= $e((string) $buyerData['tax_number']) ?></dd>
          <?php endif; ?>
          <dt>Adres</dt><dd><?= $e((string) ($buyerData['street'] ?? '')) ?>, <?= $e((string) ($buyerData['postal_code'] ?? '')) ?> <?= $e((string) ($buyerData['city'] ?? '')) ?></dd>
          <?php if (($buyerData['phone'] ?? '') !== ''): ?>
            <dt>Telefon</dt><dd><?= $e((string) $buyerData['phone']) ?></dd>
          <?php endif; ?>
          <?php if (($buyerData['email'] ?? '') !== ''): ?>
            <dt>Email</dt><dd><?= $e((string) $buyerData['email']) ?></dd>
          <?php endif; ?>
        </dl>
      </div>
    <?php endif; ?>
  </div>

  <h3 class="section-title mt-16"><?= $e($t('receipts.show.items')) ?></h3>
  <div class="table-wrap mt-8">
    <table class="table table--details">
      <thead>
        <tr>
          <th>Lp.</th>
          <th>Nazwa</th>
          <th>Ilosc</th>
          <th>Cena</th>
          <th>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><?= $e((string) ($item['quantity'] ?? 0)) ?></td>
            <td class="text-nowrap"><?= $e(number_format((float) ($item['price'] ?? 0), 2, '.', ' ')) ?></td>
            <td class="text-nowrap"><?= $e(number_format((float) ($item['total'] ?? 0), 2, '.', ' ')) ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="4" class="text-right"><strong><?= $e($t('receipts.show.total')) ?></strong></td>
          <td><strong><?= $e(number_format($totalGross, 2, '.', ' ')) ?> PLN</strong></td>
        </tr>
      </tfoot>
    </table>
  </div>

  <div class="form-grid-2 mt-16">
    <dl class="order-kv">
      <?php $issueDateShow = (string) ($receiptData['issue_date'] ?? '-'); ?>
      <dt><?= $e($t('receipts.show.issue_date')) ?></dt><dd><?= $e(strlen($issueDateShow) >= 16 ? substr($issueDateShow, 0, 16) : $issueDateShow) ?></dd>
      <dt><?= $e($t('receipts.show.sale_date')) ?></dt><dd><?= $e((string) ($receiptData['sale_date'] ?? '-')) ?></dd>
      <dt><?= $e($t('receipts.show.config')) ?></dt><dd><?= $e($configNameVal !== '' ? $configNameVal : '-') ?></dd>
      <?php if (($receiptData['order_reference_value'] ?? null) !== null): ?>
        <dt><?= $e($t('receipts.show.reference')) ?></dt><dd><?= $e((string) $receiptData['order_reference_value']) ?></dd>
      <?php endif; ?>
    </dl>
  </div>
</section>
