<?php $rows = $rows ?? []; $q = $q ?? ''; $status = $status ?? ''; ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h4 class="mb-1">سجل الشيكات</h4>
    <div class="text-muted small">شيكات العملاء المستلمة وتظهيرها للموردين وحالات الرجوع</div>
  </div>
  <div class="d-flex gap-2">
    <?php if (can('checks.create')): ?><a class="btn btn-primary" href="<?= e(url($check_receive_url ?? '/finance/checks/receive')) ?>"><i class="fa-solid fa-plus"></i> استلام شيك</a><?php endif; ?>
  </div>
</div>
<div class="card mb-3"><div class="card-body">
  <form class="row g-2 align-items-end" method="get">
    <div class="col-md-5"><label class="form-label">بحث</label><input class="form-control" name="q" value="<?= e($q) ?>" placeholder="رقم الشيك / العميل / البنك"></div>
    <div class="col-md-3"><label class="form-label">الحالة</label>
      <select name="status" class="form-select">
        <option value="">الكل</option>
        <option value="received" <?= $status==='received'?'selected':'' ?>>مستلم</option>
        <option value="endorsed" <?= $status==='endorsed'?'selected':'' ?>>مُظهر</option>
        <option value="returned" <?= $status==='returned'?'selected':'' ?>>مرتد/مُعاد</option>
        <option value="settled_cash" <?= $status==='settled_cash'?'selected':'' ?>>تمت تسويته نقدًا</option>
        <option value="replaced" <?= $status==='replaced'?'selected':'' ?>>تم استبداله</option>
        <option value="canceled" <?= $status==='canceled'?'selected':'' ?>>ملغي</option>
      </select>
    </div>
    <div class="col-md-2 d-grid"><button class="btn btn-outline-primary">بحث</button></div>
    <div class="col-md-2 d-grid"><a class="btn btn-outline-secondary" href="<?= e(url($checks_url ?? '/finance/checks')) ?>">إعادة ضبط</a></div>
  </form>
</div></div>
<div class="card"><div class="card-body p-0"><div class="table-responsive">
  <table class="table table-striped table-hover align-middle mb-0">
    <thead class="table-light"><tr><th>#</th><th>رقم الشيك</th><th>العميل الأصلي</th><th>البنك</th><th>الاستحقاق</th><th class="text-end">القيمة</th><th>الحالة</th><th>عند من</th><th></th></tr></thead>
    <tbody>
      <?php if (!$rows): ?><tr><td colspan="9" class="text-center text-muted py-4">لا توجد شيكات</td></tr><?php else: foreach ($rows as $r): ?>
      <?php $cls = strtolower((string)$r['current_status'])==='endorsed'?'primary':(strtolower((string)$r['current_status'])==='returned'?'danger':'success'); ?>
      <tr>
        <td><?= (int)$r['id'] ?></td>
        <td class="fw-bold"><?= e((string)$r['check_no']) ?></td>
        <td><?= e((string)$r['drawer_name']) ?></td>
        <td><?= e((string)($r['bank_name'] ?: '-')) ?></td>
        <td><?= e((string)($r['due_date'] ?: '-')) ?></td>
        <td class="text-end"><?= e(format_money((float)$r['amount'])) ?></td>
        <td><span class="badge text-bg-<?= $cls ?>"><?= e(check_status_label((string)$r['current_status'])) ?></span></td>
        <td><?= e(check_holder_label((string)$r['current_holder_type'])) ?><?= !empty($r['endorsed_party_name']) && strtolower((string)$r['current_status'])==='endorsed' ? ' - '.e((string)$r['endorsed_party_name']) : '' ?></td>
        <td class="text-end text-nowrap"><div class="btn-group btn-group-sm"><a class="btn btn-outline-secondary" href="<?= e((url($check_view_base_url ?? '/finance/checks/view')) . '?id='.(int)$r['id']) ?>">عرض</a><a class="btn btn-outline-dark" href="<?= e(url('/prints/check?id='.(int)$r['id'])) ?>" target="_blank" rel="noopener">طباعة</a></div></td>
      </tr>
      <?php endforeach; endif; ?>
    </tbody>
  </table>
</div></div></div>
