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

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-hand-holding-usd me-2 text-muted"></i>Customer Receipts
    <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <a href="<?= PROOT; ?>CustomerReceipts/record" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> Record receipt</a>
</div>

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>CustomerReceipts/index/1" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2">
    <div class="row g-2 align-items-end">
      <div class="col-md-4"><label class="form-label small text-muted mb-1">Search</label>
        <input type="text" class="form-control form-control-sm" name="q" value="<?= htmlspecialchars($f['q']); ?>" placeholder="Receipt # · reference · customer"></div>
      <div class="col-md-2"><label class="form-label small text-muted mb-1">Method</label>
        <select name="method" class="form-select form-select-sm"><option value="">All</option>
          <?php foreach (['cash','petty_cash','mobile_money','bank_transfer','cheque'] as $m): ?>
            <option value="<?= $m; ?>" <?= $f['method']===$m?'selected':''; ?>><?= str_replace('_', ' ', $m); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><label class="form-label small text-muted mb-1">Status</label>
        <select name="status" class="form-select form-select-sm"><option value="">All</option>
          <option value="recorded" <?= $f['status']==='recorded'?'selected':''; ?>>Recorded</option>
          <option value="void"     <?= $f['status']==='void'?'selected':''; ?>>Void</option>
        </select></div>
      <div class="col-md-2"><label class="form-label small text-muted mb-1">From</label>
        <input type="date" name="from_date" class="form-control form-control-sm" value="<?= htmlspecialchars($f['from_date']); ?>"></div>
      <div class="col-md-2 d-flex gap-1">
        <button class="btn btn-sm btn-primary flex-grow-1"><i class="fas fa-filter"></i></button>
        <a href="<?= PROOT; ?>CustomerReceipts" class="btn btn-sm btn-outline-secondary"><i class="fas fa-times"></i></a>
      </div>
    </div>
  </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>Receipt #</th><th>Date</th><th>Customer</th><th class="money">Amount</th><th class="money">Allocated</th><th>Method</th><th>Reference</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r):
          $bg = $r['status'] === 'void' ? 'dark' : 'success'; ?>
          <tr>
            <td class="small"><a href="<?= PROOT; ?>CustomerReceipts/view/<?= (int)$r['c0d']; ?>" class="fw-semibold font-monospace text-decoration-none"><?= htmlspecialchars($r['receipt_no']); ?></a></td>
            <td class="small text-muted"><?= humanDate($r['receipt_date']); ?></td>
            <td class="small"><a href="<?= PROOT; ?>Customers/view/<?= (int)$r['customer_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($r['customer_name'] ?: '—'); ?></a></td>
            <td class="money"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['amount'], 2, '.', ','); ?></td>
            <td class="money small text-muted"><?= number_format((float)$r['amount_allocated'], 2, '.', ','); ?></td>
            <td class="small" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_', ' ', $r['payment_method'])); ?></td>
            <td class="small text-muted"><?= htmlspecialchars($r['reference'] ?: '—'); ?></td>
            <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span></td>
            <td class="text-end"><a href="<?= PROOT; ?>CustomerReceipts/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="9" class="text-center text-muted py-5">No receipts — <a href="<?= PROOT; ?>CustomerReceipts/record">record the first payment</a>.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if (!empty($this->data['pg_html'])): ?>
    <div class="card-footer bg-white d-flex justify-content-between py-2"><span class="small text-muted">Page <?= (int)$this->data['page']; ?> · <?= (int)$this->data['total']; ?> total</span><?= $this->data['pg_html']; ?></div>
  <?php endif; ?>
</div>

<?php $this->end(); ?>
