<?php $this->start('body'); ?>
<?php $rows = $this->data['rows']; $suppliers = $this->data['suppliers']; $filters = $this->data['filters']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-money-bill-transfer me-2 text-muted"></i>Supplier Payments</h5>
    <div class="text-muted small">Payments made to suppliers, allocated to bills.</div>
  </div>
  <a href="<?= PROOT; ?>SupplierPayments/record" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i>Record payment</a>
</div>

<?= flashHtml(); ?>

<form method="GET" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2 d-flex flex-wrap gap-2 align-items-end">
    <div><label class="form-label small mb-1 text-uppercase text-muted">Search</label><input type="text" name="q" class="form-control form-control-sm" value="<?= htmlspecialchars($filters['q']); ?>" placeholder="pay no · ref · supplier"></div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">Supplier</label>
      <select name="vendor_id" class="form-select form-select-sm">
        <option value="0">All</option>
        <?php foreach ($suppliers as $s): ?><option value="<?= (int)$s['c0d']; ?>" <?= (int)$filters['vendor_id'] === (int)$s['c0d'] ? 'selected' : ''; ?>><?= htmlspecialchars($s['vendor_name']); ?></option><?php endforeach; ?>
      </select>
    </div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">Method</label>
      <select name="method" class="form-select form-select-sm">
        <?php foreach (['' => 'All', 'bank_transfer' => 'Bank', 'mobile_money' => 'MoMo', 'cash' => 'Cash', 'cheque' => 'Cheque', 'fuel_card' => 'Fuel card'] as $k => $v): ?>
          <option value="<?= $k; ?>" <?= $filters['method'] === $k ? 'selected' : ''; ?>><?= $v; ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">From</label><input type="date" name="from_date" value="<?= htmlspecialchars($filters['from_date']); ?>" class="form-control form-control-sm"></div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">To</label>  <input type="date" name="to_date"   value="<?= htmlspecialchars($filters['to_date']);   ?>" class="form-control form-control-sm"></div>
    <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-search"></i></button>
  </div>
</form>

<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr><th>Payment</th><th>Date</th><th>Supplier</th><th>Method</th><th>Reference</th>
          <th class="money">Amount</th><th class="money">Allocated</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="9" class="text-center text-muted py-5">No payments yet.</td></tr>
        <?php else: ?>
          <?php foreach ($rows as $r):
            $bg = ['recorded'=>'success','void'=>'dark'][$r['status']] ?? 'light';
            $unalloc = (float)$r['amount'] - (float)$r['amount_allocated'];
          ?>
            <tr>
              <td class="font-monospace small"><?= htmlspecialchars($r['payment_no']); ?></td>
              <td class="small"><?= humanDate($r['payment_date']); ?></td>
              <td class="small"><?= htmlspecialchars($r['vendor_name'] ?? '—'); ?></td>
              <td class="small text-muted"><?= htmlspecialchars(str_replace('_', ' ', $r['payment_method'])); ?></td>
              <td class="small text-muted"><?= htmlspecialchars($r['reference'] ?: '—'); ?></td>
              <td class="money font-monospace small"><?= number_format((float)$r['amount'], 2); ?></td>
              <td class="money font-monospace small">
                <?= number_format((float)$r['amount_allocated'], 2); ?>
                <?php if ($unalloc > 0.005): ?><div class="small text-warning">unalloc <?= number_format($unalloc, 2); ?></div><?php endif; ?>
              </td>
              <td><span class="badge bg-<?= $bg; ?>"><?= htmlspecialchars($r['status']); ?></span></td>
              <td class="text-end"><a href="<?= PROOT; ?>SupplierPayments/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-arrow-right"></i></a></td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?= $this->raw('pg_html'); ?>
<?php $this->end(); ?>
