<?php
$p = $payment ?? [];
$methodMap = ['CASH' => 'نقدي','TRANSFER' => 'تحويل','CHEQUE' => 'شيك'];
$method = $methodMap[strtoupper((string)($p['method'] ?? 'CASH'))] ?? 'نقدي';
$payNo = trim((string)($p['pay_no'] ?? $meta['no'] ?? ''));
$payDate = trim((string)substr((string)($p['pay_date'] ?? $meta['date'] ?? ''), 0, 10));
$supplier = trim((string)($p['supplier_name'] ?? '')) ?: '-';
$supplierCode = trim((string)($p['supplier_code'] ?? ''));
$amount = (float)($p['amount'] ?? 0);
$amountWords = amount_to_arabic_words($amount);
$refNo = trim((string)($p['ref_no'] ?? ''));
$notes = trim((string)($p['notes'] ?? ''));
$showSignatures = !array_key_exists('show_signatures', $print_options ?? []) || !empty(($print_options ?? [])['show_signatures']);
$bodySections = print_sections_ordered([
  'party_block' => ['enabled' => true, 'title' => 'بيانات السند', 'order' => 10],
  'hero_totals' => ['enabled' => true, 'title' => 'ملخص سريع', 'order' => 20],
  'notes' => ['enabled' => true, 'title' => 'ملاحظات السند', 'order' => 30],
  'totals' => ['enabled' => true, 'title' => 'ملخص السند', 'order' => 40],
], $print_options ?? [], 'body');
if (($print_format ?? 'a4') === 'receipt'):
?>
<?php foreach ($bodySections as $sectionKey => $section): if (empty($section['enabled'])) continue; ?>
  <?php if ($sectionKey === 'party_block'): ?>
    <div class="receipt-meta-grid two-col">
      <div><span>رقم السند</span><strong><?= e($payNo !== '' ? $payNo : '-') ?></strong></div>
      <div><span>التاريخ</span><strong><?= e($payDate !== '' ? $payDate : '-') ?></strong></div>
      <div><span>المورد</span><strong><?= e($supplier) ?></strong></div>
      <?php if ($supplierCode !== ''): ?><div><span>كود المورد</span><strong><?= e($supplierCode) ?></strong></div><?php endif; ?>
      <div><span>طريقة السداد</span><strong><?= e($method) ?></strong></div>
      <div><span>المبلغ</span><strong><?= e(format_money($amount)) ?></strong></div>
      <?php if ($refNo !== ''): ?><div><span>رقم مرجعي</span><strong><?= e($refNo) ?></strong></div><?php endif; ?>
    </div>
  <?php elseif ($sectionKey === 'notes' && $notes !== ''): ?>
    <div class="receipt-note-box"><div class="receipt-note-title"><?= e($section['title']) ?></div><div><?= e($notes) ?></div></div>
  <?php elseif ($sectionKey === 'totals'): ?>
    <div class="receipt-summary"><div class="grand"><span><?= e($section['title']) ?></span><strong><?= e(format_money($amount)) ?></strong></div></div>
  <?php endif; ?>
<?php endforeach; ?>
<?php return; endif; ?>

<?php $voucherHeroRendered = false; ?>
<?php foreach ($bodySections as $sectionKey => $section): if (empty($section['enabled'])) continue; ?>
  <?php if ($sectionKey === 'party_block' || $sectionKey === 'hero_totals'): ?>
    <?php if ($voucherHeroRendered) { continue; } $voucherHeroRendered = true; ?>
      <section class="print-section voucher-hero">
        <div class="hero-grid voucher-hero-grid">
          <?php if (print_section_visible($print_options ?? [], 'body', 'party_block', true)): ?>
          <div class="panel">
            <div class="panel-head"><?= e(print_section_title($print_options ?? [], 'body', 'party_block', 'بيانات السند')) ?> <small>Supplier Payment</small></div>
            <div class="panel-body"><div class="detail-grid">
              <div class="detail-item"><div class="detail-label">المورد</div><div class="detail-value"><?= e($supplier) ?></div></div>
              <div class="detail-item"><div class="detail-label">كود المورد</div><div class="detail-value"><?= e($supplierCode !== '' ? $supplierCode : '-') ?></div></div>
              <div class="detail-item"><div class="detail-label">طريقة السداد</div><div class="detail-value"><?= e($method) ?></div></div>
              <div class="detail-item"><div class="detail-label">رقم مرجعي</div><div class="detail-value"><?= e($refNo !== '' ? $refNo : '-') ?></div></div>
            </div></div>
          </div>
          <?php endif; ?>
          <?php if (print_section_visible($print_options ?? [], 'body', 'hero_totals', true)): ?>
          <div class="hero-summary voucher-summary-boxes">
            <div class="stat-box"><div class="stat-label">PAID AMOUNT</div><div class="stat-value"><?= e(format_money($amount)) ?></div></div>
            <div class="stat-box"><div class="stat-label"><?= e(print_section_title($print_options ?? [], 'body', 'hero_totals', 'PAYMENT NO')) ?></div><div class="stat-value"><?= e($payNo !== '' ? $payNo : '-') ?></div><div class="stat-note">بتاريخ <?= e($payDate !== '' ? $payDate : '-') ?></div></div>
          </div>
          <?php endif; ?>
        </div>
      </section>
      <?php continue; ?>
  <?php elseif ($sectionKey === 'notes'): ?>
    <section class="print-section"><div class="note-box<?= $notes === '' ? ' is-muted' : '' ?>"><?php if ($notes !== ''): ?><strong><?= e($section['title']) ?></strong>

<?= e($notes) ?><?php else: ?>لا توجد ملاحظات إضافية لهذا السند.<?php endif; ?></div></section>
  <?php elseif ($sectionKey === 'totals'): ?>
    <section class="print-section"><div class="summary-card"><div class="summary-head"><?= e($section['title']) ?></div><table>
      <tr><td class="label">رقم السند</td><td class="val"><?= e($payNo !== '' ? $payNo : '-') ?></td></tr>
      <tr><td class="label">التاريخ</td><td class="val"><?= e($payDate !== '' ? $payDate : '-') ?></td></tr>
      <tr><td class="label">المورد</td><td class="val"><?= e($supplier) ?></td></tr>
      <tr><td class="label">طريقة السداد</td><td class="val"><?= e($method) ?></td></tr>
      <tr class="amount-words-row"><td class="label">المبلغ بالحروف</td><td class="val amount-words"><?= e($amountWords) ?></td></tr>
      <tr class="grand"><td class="label">مبلغ السداد</td><td class="val"><?= e(format_money($amount)) ?></td></tr>
    </table></div></section>
  <?php endif; ?>
<?php endforeach; ?>

