<?php
$printOptions = is_array($print_options ?? null) ? $print_options : [];
$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],
], $printOptions, 'body');
$pm = strtolower((string)($receipt['pay_method'] ?? $receipt['method'] ?? 'cash'));
$methodText = $pm==='cheque' ? 'شيك' : ($pm==='bank_transfer' ? 'تحويل بنكي' : 'نقدي');
$accountTarget = ((string)($receipt['account_source_type'] ?? 'cashbox')) === 'bank_account'
    ? (string)($receipt['bank_account_name'] ?? '')
    : (string)($receipt['cashbox_name'] ?? '');
$receiptNo = trim((string)($receipt['receipt_no'] ?? $meta['no'] ?? ''));
$receiptDate = trim((string)substr((string)($receipt['receipt_date'] ?? $meta['date'] ?? ''), 0, 10));
$partyName = trim((string)($receipt['party_name'] ?? '-')) ?: '-';
$partyCode = trim((string)($receipt['party_code'] ?? ''));
$amount = (float)($receipt['amount'] ?? 0);
$amountWords = amount_to_arabic_words($amount);
$refNo = trim((string)($receipt['ref_no'] ?? ''));
$notes = trim((string)($receipt['notes'] ?? ''));
$instrumentLines = [];
if ($pm === 'cheque') {
    $instrumentLines = [
        'رقم الشيك: ' . (string)($receipt['cheque_no'] ?? '-'),
        'تاريخ الاستحقاق: ' . (string)($receipt['cheque_due_date'] ?? '-'),
        'اسم المستفيد: ' . (string)($receipt['beneficiary_name'] ?? '-'),
    ];
} elseif ($pm === 'bank_transfer') {
    $instrumentLines = [
        'البنك: ' . (string)($receipt['bank_name'] ?? '-'),
        'رقم المرجع: ' . (string)($receipt['transfer_ref'] ?? '-'),
        'تاريخ التحويل: ' . (string)($receipt['transfer_date'] ?? '-'),
    ];
    if (!empty($receipt['sender_name'])) {
        $instrumentLines[] = 'اسم المحوّل: ' . (string)$receipt['sender_name'];
    }
}
$notesTitle = $pm === 'cheque' ? 'بيانات الشيك' : ($pm === 'bank_transfer' ? 'بيانات التحويل' : 'ملاحظات');
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($receiptNo !== '' ? $receiptNo : '-') ?></strong></div>
      <div><span>التاريخ</span><strong><?= e($receiptDate !== '' ? $receiptDate : '-') ?></strong></div>
      <div><span>الطرف</span><strong><?= e($partyName) ?></strong></div>
      <?php if ($partyCode !== ''): ?><div><span>الكود</span><strong><?= e($partyCode) ?></strong></div><?php endif; ?>
      <div><span>المبلغ</span><strong><?= e(format_money($amount)) ?></strong></div>
      <div><span>طريقة التحصيل</span><strong><?= e($methodText) ?></strong></div>
      <div><span>التحصيل إلى</span><strong><?= e($accountTarget !== '' ? $accountTarget : '-') ?></strong></div>
      <?php if ($refNo !== ''): ?><div><span>رقم مرجعي</span><strong><?= e($refNo) ?></strong></div><?php endif; ?>
    </div>
  <?php elseif ($sectionKey === 'notes' && ($notes !== '' || $instrumentLines !== [])): ?>
    <div class="receipt-note-box"><div class="receipt-note-title"><?= e($section['title'] !== 'ملاحظات السند' ? $section['title'] : $notesTitle) ?></div>
      <?php foreach ($instrumentLines as $line): ?><div><?= e($line) ?></div><?php endforeach; ?>
      <?php if ($notes !== ''): ?><div><?= e($notes) ?></div><?php endif; ?>
    </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($printOptions, 'body', 'party_block', true)): ?>
    <div class="panel">
      <div class="panel-head"><?= e(print_section_title($printOptions, 'body', 'party_block', 'بيانات السند')) ?> <small>Receipt Details</small></div>
      <div class="panel-body">
        <div class="detail-grid">
          <div class="detail-item"><div class="detail-label">الطرف</div><div class="detail-value"><?= e($partyName) ?><?= $partyCode !== '' ? ' (' . e($partyCode) . ')' : '' ?></div></div>
          <div class="detail-item"><div class="detail-label">طريقة التحصيل</div><div class="detail-value"><?= e($methodText) ?></div></div>
          <div class="detail-item"><div class="detail-label">التحصيل إلى</div><div class="detail-value"><?= e($accountTarget !== '' ? $accountTarget : '-') ?></div></div>
          <?php if ($refNo !== ''): ?><div class="detail-item"><div class="detail-label">رقم مرجعي</div><div class="detail-value"><?= e($refNo) ?></div></div><?php endif; ?>
        </div>
      </div>
    </div>
    <?php endif; ?>
    <?php if (print_section_visible($printOptions, 'body', 'hero_totals', true)): ?>
    <div class="hero-summary voucher-summary-boxes">
      <div class="stat-box">
        <div class="stat-label">RECEIVED AMOUNT</div>
        <div class="stat-value"><?= e(format_money($amount)) ?></div>
      </div>
      <div class="stat-box">
        <div class="stat-label"><?= e(print_section_title($printOptions, 'body', 'hero_totals', 'VOUCHER NO')) ?></div>
        <div class="stat-value"><?= e($receiptNo !== '' ? $receiptNo : '-') ?></div>
        <div class="stat-note">بتاريخ <?= e($receiptDate !== '' ? $receiptDate : '-') ?></div>
      </div>
    </div>
    <?php endif; ?>
  </div>
</section>
      <?php continue; ?>
  <?php elseif ($sectionKey === 'notes'): ?>
<section class="print-section">
  <div class="note-box<?= ($notes === '' && $instrumentLines === []) ? ' is-muted' : '' ?>">
    <?php if ($notes !== '' || $instrumentLines !== []): ?>
      <strong><?= e($section['title'] !== 'ملاحظات السند' ? $section['title'] : $notesTitle) ?></strong>

      <?php foreach ($instrumentLines as $idx => $line): ?><?= $idx > 0 ? "\n\n" : '' ?><?= e($line) ?><?php endforeach; ?>
      <?php if ($notes !== ''): ?><?= $instrumentLines !== [] ? "\n\n" : '' ?><?= e($notes) ?><?php endif; ?>
    <?php else: ?>لا توجد ملاحظات إضافية لهذا السند.<?php endif; ?>
  </div>
</section>
  <?php elseif ($sectionKey === 'totals'): ?>
<section class="print-section">
  <div class="summary-card<?= ($notes === '' && $instrumentLines === []) ? ' voucher-grand-card' : '' ?>">
    <div class="summary-head"><?= e($section['title']) ?></div>
    <table>
      <tr><td class="label">رقم السند</td><td class="val"><?= e($receiptNo !== '' ? $receiptNo : '-') ?></td></tr>
      <tr><td class="label">التاريخ</td><td class="val"><?= e($receiptDate !== '' ? $receiptDate : '-') ?></td></tr>
      <tr><td class="label">طريقة التحصيل</td><td class="val"><?= e($methodText) ?></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; ?>
