<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); ?>
<section class="page-header"><div><p class="eyebrow">Billing · Accounts receivable</p><h1>Payments</h1><p><?= $e($pageDescription??'') ?></p></div></section>
<?php if(!empty($message)): ?><div class="alert alert-success"><?= $e($message) ?></div><?php endif; ?><?php if(!empty($error)): ?><div class="alert alert-error"><?= $e($error) ?></div><?php endif; ?>
<section class="card"><form method="get" class="filters"><label class="form-field"><span>Search</span><input type="search" name="search" value="<?= $e($filters['search']??'') ?>" placeholder="Invoice, patient, method or reference"></label><label class="form-field"><span>Status</span><select name="status"><option value="">All statuses</option><option value="posted" <?= ($filters['status']??'')==='posted'?'selected':'' ?>>Posted</option><option value="voided" <?= ($filters['status']??'')==='voided'?'selected':'' ?>>Voided</option></select></label><div class="form-actions"><button class="btn btn-secondary" type="submit">Filter</button><a class="btn btn-secondary" href="/crm/public/payments">Reset</a></div></form></section>
<section class="card"><div class="card-header"><div><h2>Payment register</h2><p><?= (int)$payments['total'] ?> payment(s)</p></div></div><?php if(empty($payments['items'])): ?><div class="empty-state"><h3>No payments found</h3><p>Payments will appear here after they are recorded.</p></div><?php else: ?><div class="table-wrap"><table class="data-table"><thead><tr><th>Date</th><th>Receipt</th><th>Invoice</th><th>Patient</th><th>Amount</th><th>Method</th><th>Reference</th><th>Status</th><th>Actions</th></tr></thead><tbody><?php foreach($payments['items'] as $p): ?><tr><td><?= $e($p['payment_date']) ?></td><td><?= $e($p['receipt_number']??'—') ?></td><td><strong><?= $e($p['invoice_number']) ?></strong></td><td><?= $e($p['first_name'].' '.$p['last_name']) ?><br><small><?= $e($p['patient_reference']) ?></small></td><td><strong>KSh <?= number_format((float)$p['amount'],2) ?></strong></td><td><?= $e($p['payment_method']) ?></td><td><?= $e($p['payment_reference']??'—') ?></td><td><span class="badge badge-muted"><?= $e(ucfirst($p['status']??'posted')) ?></span></td><td><a class="btn btn-small btn-secondary" href="/crm/public/payments/<?= (int)$p['id'] ?>/receipt">Receipt</a><?php if(($p['status']??'posted')==='posted'): ?><form method="post" action="/crm/public/payments/<?= (int)$p['id'] ?>/void" style="display:inline"><input type="hidden" name="_csrf" value="<?= $e($csrf??'') ?>"><button class="btn btn-small btn-secondary" type="submit" onclick="return confirm('Void this payment?')">Void</button></form><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div><?php if(($payments['pages']??1)>1): ?><div class="pagination"><?php for($p=1;$p<=$payments['pages'];$p++): ?><a class="<?= $p===$payments['page']?'active':'' ?>" href="?page=<?= $p ?>&search=<?= urlencode((string)($filters['search']??'')) ?>&status=<?= urlencode((string)($filters['status']??'')) ?>"><?= $p ?></a><?php endfor; ?></div><?php endif; ?><?php endif; ?></section>
