<?php use App\Modules\Sales\Application\Services\SalesReceiptsRouteContext; ?>
<?php
// variables: $receipts, $from, $to, $q
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">سندات القبض</h4>
  <?php if (can('sales.receipts.create')): ?>
    <a class="btn btn-primary" href="<?= e(url($routeContext['form'] ?? SalesReceiptsRouteContext::form())) ?>"><i class="fa-solid fa-plus"></i> إنشاء سند</a>
  <?php endif; ?>
</div>

<form class="card p-3 mb-3" method="get">
  <div class="row g-2 align-items-end">
    <div class="col-md-3">
      <label class="form-label">من</label>
      <input type="date" class="form-control" name="from" value="<?= e($from) ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">إلى</label>
      <input type="date" class="form-control" name="to" value="<?= e($to) ?>">
    </div>
    <div class="col-md-4">
      <label class="form-label">بحث</label>
      <input type="text" class="form-control" name="q" placeholder="رقم سند / اسم طرف / كود" value="<?= e($q) ?>">
    </div>
    <div class="col-md-2 d-flex gap-2">
      <button class="btn btn-outline-primary w-100" type="submit">عرض</button>
      <a class="btn btn-outline-secondary" href="<?= e(url($routeContext['index'] ?? SalesReceiptsRouteContext::index())) ?>">مسح</a>
    </div>
  </div>
</form>

<div class="card p-3">
  <div class="table-responsive">
    <table class="table table-sm 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>
          <th>الحالة</th>
          <th style="width: 1%"></th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($receipts)): ?>
          <tr><td colspan="8" class="text-center text-muted py-4">لا توجد بيانات</td></tr>
        <?php endif; ?>
        <?php foreach ($receipts as $r): ?>
          <tr>
            <td class="fw-bold"><a href="<?= e(url(($routeContext['view'] ?? [SalesReceiptsRouteContext::class, 'view'])((int)$r['id']))) ?>"><?= e($r['receipt_no']) ?></a></td>
            <td><?= e(date('Y-m-d', strtotime((string)$r['receipt_date']))) ?></td>
            <td><?= e($r['party_name']) ?></td>
            <td><?= e((string)($r['cashbox_name'] ?? '')) ?></td>
            <td class="text-end"><?= e(nf($r['amount'])) ?></td>
            <td>
              <?php
                $pm = (string)($r['pay_method'] ?? 'cash');
                echo $pm==='cheque' ? 'شيك' : ($pm==='bank_transfer' ? 'تحويل بنكي' : 'نقدي');
              ?>
            </td>
            <td><span class="badge text-bg-success"><?= e($r['status']) ?></span></td>
            <td>
              <a class="btn btn-sm btn-outline-secondary" href="<?= e(url(($routeContext['view'] ?? [SalesReceiptsRouteContext::class, 'view'])((int)$r['id']))) ?>">عرض</a>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
