<?php
// vars: $rows, $q, $status, $from, $to
?>

<div class="d-flex justify-content-between align-items-center mb-3">
  <h3 class="mb-0">مدفوعات الطرفين</h3>
  <?php if (can('supplier_payments.create')): ?>
    <a class="btn btn-primary" href="<?= e($supplier_payment_form_url ?? url('/purchases/supplier-payments/form')) ?>">
      <i class="fa-solid fa-plus me-1"></i>سند سداد جديد
    </a>
  <?php endif; ?>
</div>

<form class="card card-body mb-3" method="get">
  <div class="row g-2">
    <div class="col-md-4">
      <label class="form-label">بحث</label>
      <input class="form-control" name="q" value="<?= e($q) ?>" placeholder="رقم السند / مورد / مرجع">
    </div>
    <div class="col-md-2">
      <label class="form-label">الحالة</label>
      <select class="form-select" name="status">
        <option value="">الكل</option>
        <option value="DRAFT" <?= $status==='DRAFT'?'selected':'' ?>>مسودة</option>
        <option value="POSTED" <?= $status==='POSTED'?'selected':'' ?>>مرحل</option>
      </select>
    </div>
    <div class="col-md-2">
      <label class="form-label">من</label>
      <input type="date" class="form-control" name="from" value="<?= e($from) ?>">
    </div>
    <div class="col-md-2">
      <label class="form-label">إلى</label>
      <input type="date" class="form-control" name="to" value="<?= e($to) ?>">
    </div>
    <div class="col-md-2 d-flex align-items-end">
      <button class="btn btn-outline-secondary w-100"><i class="fa-solid fa-filter me-1"></i>تصفية</button>
    </div>
  </div>
</form>

<div class="card">
  <div class="table-responsive">
    <table class="table table-striped align-middle mb-0">
      <thead>
        <tr>
          <th>#</th>
          <th>رقم السند</th>
          <th>التاريخ</th>
          <th>الطرف</th>
          <th class="text-end">المبلغ</th>
          <th>الطريقة</th>
          <th>الحالة</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach (($rows ?? []) as $r): ?>
          <tr>
            <td><?= e((string)$r['id']) ?></td>
            <td><?= e((string)($r['pay_no'] ?? '')) ?></td>
            <td><?= e((string)($r['pay_date'] ?? '')) ?></td>
            <td><?= e((string)($r['party_name'] ?? '')) ?></td>
            <td class="text-end fw-bold"><?= e(number_format((float)($r['amount'] ?? 0),2)) ?></td>
            <td><?= e((string)($r['method'] ?? '')) ?></td>
            <td>
              <?php if (($r['status'] ?? '') === 'POSTED'): ?>
                <span class="badge bg-success">POSTED</span>
              <?php else: ?>
                <span class="badge bg-secondary">DRAFT</span>
              <?php endif; ?>
            </td>
            <td class="text-end">
              <a class="btn btn-sm btn-outline-primary" href="<?= e(($supplier_payment_view_base_url ?? url('/purchases/supplier-payments/view')) . '?id='.(int)$r['id']) ?>">عرض</a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?>
          <tr><td colspan="8" class="text-center text-muted py-4">لا توجد نتائج</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
