<?php $check=$check??null; $movements=$movements??[]; if(!$check) return; $status=strtolower((string)$check['current_status']); $badge=$status==='endorsed'?'primary':($status==='returned'?'danger':'success'); ?>
<div class="page-actions-bar">
    <a class="btn btn-outline-secondary btn-back" data-shortcut="back" href="<?= e(url($checks_url ?? '/finance/checks')) ?>">رجوع</a>
    <a class="btn btn-outline-dark btn-print" data-shortcut="print" href="<?= e(url('/prints/check?id='.(int)$check['id'])) ?>" target="_blank" rel="noopener">طباعة <span class="shortcut-hint">F4</span></a>
    <?php if (can('checks.endorse') && can_endorse_check($check)): ?><a class="btn btn-primary" href="<?= e((url($check_endorse_base_url ?? '/finance/checks/endorse')) . '?id='.(int)$check['id']) ?>">تظهير لمورد</a><?php endif; ?>
    <?php if (can('checks.return') && can_return_check($check)): ?><a class="btn btn-outline-danger" href="<?= e((url($check_return_base_url ?? '/finance/checks/return')) . '?id='.(int)$check['id']) ?>">رجوع شيك</a><?php endif; ?>
    <?php if (can('checks.settle') && can_settle_check_cash($check)): ?><a class="btn btn-success" href="<?= e((url($check_settle_cash_base_url ?? '/finance/checks/settle-cash')) . '?id='.(int)$check['id']) ?>">تسوية نقدية</a><?php endif; ?>
    <?php if (can('checks.replace') && can_replace_check($check)): ?><a class="btn btn-outline-primary" href="<?= e((url($check_replace_base_url ?? '/finance/checks/replace')) . '?id='.(int)$check['id']) ?>">استبدال بشيك جديد</a><?php endif; ?>
  </div>
<div class="summary-strip">
  <div class="summary-card"><div class="card-body"><div class="label">الحالة</div><div class="value"><span class="badge text-bg-<?= $badge ?>"><?= e(check_status_label((string)$check['current_status'])) ?></span></div></div></div>
  <div class="summary-card"><div class="card-body"><div class="label">عند من</div><div class="value"><?= e(check_holder_label((string)$check['current_holder_type'])) ?></div></div></div>
  <div class="summary-card"><div class="card-body"><div class="label">القيمة</div><div class="value"><?= e(format_money((float)$check['amount'])) ?></div></div></div>
  <div class="summary-card"><div class="card-body"><div class="label">البنك</div><div class="value"><?= e((string)($check['bank_name'] ?: '-')) ?></div></div></div>
</div>
<div class="row g-3">
  <div class="col-md-5">
    <div class="card content-card"><div class="card-header fw-bold">ملخص الشيك</div><div class="card-body">
      <div class="mb-2"><span class="text-muted small">العميل الأصلي</span><div class="fw-bold"><?= e((string)$check['drawer_name']) ?></div></div>
      <div class="mb-2"><span class="text-muted small">آخر مورد مُظهر إليه</span><div><?= e((string)($check['supplier_name'] ?: '-')) ?></div></div>
      <div class="mb-2"><span class="text-muted small">تاريخ الاستلام</span><div><?= e((string)$check['receive_date']) ?></div></div>
      <div class="mb-2"><span class="text-muted small">تاريخ الإصدار</span><div><?= e((string)($check['issue_date'] ?: '-')) ?></div></div>
      <div class="mb-2"><span class="text-muted small">تاريخ الاستحقاق</span><div><?= e((string)($check['due_date'] ?: '-')) ?></div></div>
      <div class="mb-2"><span class="text-muted small">ملاحظات</span><div><?= e((string)($check['notes'] ?: '-')) ?></div></div>
    </div></div>
  </div>
  <div class="col-md-7">
    <div class="card content-card"><div class="card-header fw-bold">حركات الشيك</div><div class="card-body p-0"><div class="table-responsive">
      <table class="table table-bordered align-middle mb-0">
        <thead class="table-light"><tr><th>#</th><th>الحركة</th><th>التاريخ</th><th>الطرف</th><th class="text-end">القيمة</th><th>ملاحظات</th></tr></thead>
        <tbody>
          <?php if (!$movements): ?><tr><td colspan="6" class="text-center text-muted py-4">لا توجد حركات</td></tr><?php else: foreach($movements as $i=>$m): ?>
          <?php $mlMap = ['receive'=>'استلام','endorse'=>'تظهير','return'=>'رجوع','cash_settlement'=>'تسوية نقدية','replace'=>'استبدال']; $ml = $mlMap[$m['movement_type']] ?? (string)$m['movement_type']; ?>
          <tr>
            <td><?= $i+1 ?></td><td><?= e($ml) ?></td><td><?= e((string)$m['movement_date']) ?></td><td><?= e((string)($m['party_name'] ?: '-')) ?></td><td class="text-end"><?= e(format_money((float)$m['amount'])) ?></td><td><?= e((string)($m['notes'] ?: '-')) ?></td>
          </tr>
          <?php endforeach; endif; ?>
        </tbody>
      </table>
    </div></div></div>
  </div>
</div>
