<?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');
$paymentType = (string)($row['payment_type'] ?? 'party');
$typeText = $paymentType === 'expense' ? 'مصروف' : 'سداد طرف';
$payMethod = strtolower((string)($row['pay_method'] ?? 'cash'));
$methodText = $payMethod==='cheque' ? 'شيك' : ($payMethod==='bank_transfer' ? 'تحويل بنكي' : 'نقدي');
$sourceName = ((string)($row['account_source_type'] ?? 'cashbox')) === 'bank_account' ? (string)($row['bank_account_name'] ?? '') : (string)($row['cashbox_name'] ?? '');
$voucherNo = trim((string)($row['voucher_no'] ?? $meta['no'] ?? ''));
$voucherDate = trim((string)substr((string)($row['voucher_date'] ?? $meta['date'] ?? ''), 0, 10));
$payee = trim((string)($row['payee_name'] ?? ($row['party_name'] ?? ''))) ?: '-';
$purpose = trim((string)($row['purpose'] ?? ''));
$refNo = trim((string)($row['ref_no'] ?? ''));
$notes = trim((string)($row['notes'] ?? ''));
$amount = (float)($row['amount'] ?? 0);
$amountWords = amount_to_arabic_words($amount);
$status = strtoupper((string)($row['status'] ?? 'POSTED'));
$noteLines = [];
if ($purpose !== '') {
    $noteLines[] = 'البيان: ' . $purpose;
}
if ($notes !== '') {
    $noteLines[] = 'ملاحظات: ' . $notes;
}
if ($status === 'CANCELED') {
    $noteLines[] = 'تاريخ الإلغاء: ' . (string)($row['canceled_at'] ?? '-');
    $noteLines[] = 'سبب الإلغاء: ' . (string)($row['cancel_reason'] ?? '-');
}
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($voucherNo !== '' ? $voucherNo : '-') ?></strong></div>
  <div><span>التاريخ</span><strong><?= e($voucherDate !== '' ? $voucherDate : '-') ?></strong></div>
  <div><span>نوع السند</span><strong><?= e($typeText) ?></strong></div>
  <div><span>طريقة الصرف</span><strong><?= e($methodText) ?></strong></div>
  <div><span>الصرف من</span><strong><?= e($sourceName !== '' ? $sourceName : '-') ?></strong></div>
  <div><span>المبلغ</span><strong><?= e(format_money($amount)) ?></strong></div>
  <div><span>المستفيد</span><strong><?= e($payee) ?></strong></div>
  <?php if ($paymentType === 'expense' && !empty($row['expense_item_name'])): ?><div><span>بند المصروف</span><strong><?= e((string)$row['expense_item_name']) ?></strong></div><?php endif; ?>
  <?php if ($paymentType === 'expense' && !empty($row['expense_category_name'])): ?><div><span>مجموعة المصروف</span><strong><?= e((string)$row['expense_category_name']) ?></strong></div><?php endif; ?>
  <?php if ($refNo !== ''): ?><div><span>رقم مرجعي</span><strong><?= e($refNo) ?></strong></div><?php endif; ?>
  <?php if ($status === 'CANCELED'): ?><div><span>الحالة</span><strong>ملغي</strong></div><?php endif; ?>
</div>
  <?php elseif ($sectionKey === 'notes' && $noteLines !== []): ?>
<div class="receipt-note-box"><div class="receipt-note-title"><?= e($section['title']) ?></div><?php foreach ($noteLines as $line): ?><div><?= e($line) ?></div><?php endforeach; ?></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>Payment 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($typeText) ?></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($sourceName !== '' ? $sourceName : '-') ?></div></div>
          <div class="detail-item"><div class="detail-label">المستفيد</div><div class="detail-value"><?= e($payee) ?></div></div>
          <?php if ($paymentType === 'expense' && !empty($row['expense_item_name'])): ?><div class="detail-item"><div class="detail-label">بند المصروف</div><div class="detail-value"><?= e((string)$row['expense_item_name']) ?></div></div><?php endif; ?>
          <?php if ($paymentType === 'expense' && !empty($row['expense_category_name'])): ?><div class="detail-item"><div class="detail-label">مجموعة المصروف</div><div class="detail-value"><?= e((string)$row['expense_category_name']) ?></div></div><?php endif; ?>
          <?php if ($refNo !== ''): ?><div class="detail-item"><div class="detail-label">رقم مرجعي</div><div class="detail-value"><?= e($refNo) ?></div></div><?php endif; ?>
          <?php if ($status === 'CANCELED'): ?><div class="detail-item"><div class="detail-label">الحالة</div><div class="detail-value">ملغي</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">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($printOptions, 'body', 'hero_totals', 'VOUCHER NO')) ?></div>
        <div class="stat-value"><?= e($voucherNo !== '' ? $voucherNo : '-') ?></div>
        <div class="stat-note">بتاريخ <?= e($voucherDate !== '' ? $voucherDate : '-') ?></div>
      </div>
    </div>
    <?php endif; ?>
  </div>
</section>
      <?php continue; ?>
  <?php elseif ($sectionKey === 'notes'): ?>
<section class="print-section">
  <div class="note-box<?= $noteLines === [] ? ' is-muted' : '' ?>">
    <?php if ($noteLines !== []): ?><strong><?= e($section['title']) ?></strong>

<?php foreach ($noteLines as $idx => $line): ?><?= $idx > 0 ? "\n\n" : '' ?><?= e($line) ?><?php endforeach; ?><?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($voucherNo !== '' ? $voucherNo : '-') ?></td></tr>
      <tr><td class="label">التاريخ</td><td class="val"><?= e($voucherDate !== '' ? $voucherDate : '-') ?></td></tr>
      <tr><td class="label">نوع السند</td><td class="val"><?= e($typeText) ?></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; ?>
