<?php
$drawer = (string)($check['drawer_name'] ?? '-');
$drawerCode = (string)($check['drawer_code'] ?? '');
$supplier = (string)($check['supplier_name'] ?? '-');
$bankName = (string)($check['bank_name'] ?? '-');
$notes = trim((string)($check['notes'] ?? ''));
$amount = (float)($check['amount'] ?? 0);
$amountWords = function_exists('amount_to_arabic_words') ? amount_to_arabic_words($amount) : '';
$statusLabel = check_status_label((string)($check['current_status'] ?? ''));
$holderLabel = check_holder_label((string)($check['current_holder_type'] ?? ''));
$movementLabels = [
  'receive' => 'استلام',
  'endorse' => 'تظهير',
  'return' => 'رجوع',
  'cash_settlement' => 'تسوية نقدية',
  'replace' => 'استبدال',
];
?>

<section class="print-section bank-check-hero keep-together">
  <div class="hero-grid bank-hero-grid">
    <div class="panel">
      <div class="panel-head"><span>بيانات الشيك</span><small>Check Details</small></div>
      <div class="panel-body">
        <div class="detail-grid bank-detail-grid">
          <div class="detail-item"><div class="detail-label">العميل الأصلي</div><div class="detail-value"><?= e($drawer) ?> <?= $drawerCode ? '(' . e($drawerCode) . ')' : '' ?></div></div>
          <div class="detail-item"><div class="detail-label">البنك</div><div class="detail-value"><?= e($bankName ?: '-') ?></div></div>
          <div class="detail-item"><div class="detail-label">تاريخ الاستلام</div><div class="detail-value"><?= e((string)($check['receive_date'] ?: '-')) ?></div></div>
          <div class="detail-item"><div class="detail-label">تاريخ الإصدار</div><div class="detail-value"><?= e((string)($check['issue_date'] ?: '-')) ?></div></div>
          <div class="detail-item"><div class="detail-label">تاريخ الاستحقاق</div><div class="detail-value"><?= e((string)($check['due_date'] ?: '-')) ?></div></div>
          <div class="detail-item"><div class="detail-label">آخر مورد مُظهر إليه</div><div class="detail-value"><?= e($supplier ?: '-') ?></div></div>
        </div>
      </div>
    </div>
    <div class="summary-card bank-summary-card">
      <div class="summary-head">ملخص الشيك</div>
      <table>
        <tr><td class="label">الحالة</td><td class="val"><?= e($statusLabel) ?></td></tr>
        <tr><td class="label">عند من</td><td class="val"><?= e($holderLabel) ?></td></tr>
        <tr class="grand"><td class="label">قيمة الشيك</td><td class="val"><?= e(format_money($amount)) ?></td></tr>
        <?php if ($amountWords !== ''): ?><tr class="amount-words-row"><td class="label">المبلغ بالحروف</td><td class="val amount-words"><?= e($amountWords) ?></td></tr><?php endif; ?>
      </table>
    </div>
  </div>
</section>

<section class="print-section">
  <div class="section-heading">حركات الشيك</div>
  <div class="doc-table-wrap">
    <table class="table table-compact bank-table">
      <thead>
        <tr>
          <th style="width:44px">#</th>
          <th>الحركة</th>
          <th style="width:105px">التاريخ</th>
          <th>الطرف</th>
          <th style="width:110px">القيمة</th>
          <th>ملاحظات</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($movements)): ?>
          <tr><td colspan="6" class="text-center text-muted py-4">لا توجد حركات</td></tr>
        <?php else: $i=0; foreach ($movements as $m): $i++; ?>
          <tr>
            <td class="text-center"><?= $i ?></td>
            <td class="strong"><?= e($movementLabels[$m['movement_type']] ?? (string)$m['movement_type']) ?></td>
            <td class="text-center"><?= e((string)($m['movement_date'] ?: '-')) ?></td>
            <td><?= e((string)($m['party_name'] ?: '-')) ?></td>
            <td class="text-left strong"><?= e(format_money((float)($m['amount'] ?? 0))) ?></td>
            <td><?= e((string)($m['notes'] ?: '-')) ?></td>
          </tr>
        <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</section>

<?php if ($notes !== '' && $notes !== '-'): ?>
<section class="print-section keep-together">
  <div class="section-heading">ملاحظات</div>
  <div class="note-box"><?= e($notes) ?></div>
</section>
<?php endif; ?>
