<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); $form=$form??[]; $value=static fn($k,$d='')=>$form[$k]??$d; ?>
<section class="page-header"><div><p class="eyebrow">Billing · Payment</p><h1>Record Payment</h1><p><?= $e($pageDescription??'') ?></p></div><a class="btn btn-secondary" href="/crm/public/invoices/<?= (int)$invoice['id'] ?>/edit">Back to Invoice</a></section>
<?php if(!empty($error)): ?><div class="alert alert-error"><?= $e($error) ?></div><?php endif; ?><?php if(!empty($message)): ?><div class="alert alert-success"><?= $e($message) ?></div><?php endif; ?>
<section class="card"><div class="card-header"><div><h2><?= $e($invoice['invoice_number']) ?></h2><p><?= $e($invoice['first_name'].' '.$invoice['last_name'].' — '.$invoice['patient_reference']) ?></p></div></div><div class="grid grid-3"><div><strong>Total</strong><br>KSh <?= number_format((float)$invoice['total'],2) ?></div><div><strong>Paid</strong><br>KSh <?= number_format((float)$invoice['paid_amount'],2) ?></div><div><strong>Balance</strong><br>KSh <?= number_format((float)$invoice['balance'],2) ?></div></div></section>
<section class="card"><form method="post" action="/crm/public/invoices/<?= (int)$invoice['id'] ?>/payment" class="form-stack"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><div class="grid grid-2"><label class="form-field"><span>Amount (KSh)</span><input type="text" inputmode="decimal" name="amount" required value="<?= $e($value('amount',number_format((float)$invoice['balance'],2,'.',''))) ?>"></label><label class="form-field"><span>Payment date</span><input type="date" name="payment_date" required value="<?= $e($value('payment_date',date('Y-m-d'))) ?>"></label><label class="form-field"><span>Payment method</span><select name="payment_method" required><option value="">Select method</option><?php foreach(['Cash','Bank Transfer','Card','Mobile Money','Other'] as $m): ?><option value="<?= $e($m) ?>" <?= $value('payment_method')===$m?'selected':'' ?>><?= $e($m) ?></option><?php endforeach; ?></select></label><label class="form-field"><span>Transaction / Reference</span><input type="text" name="payment_reference" value="<?= $e($value('payment_reference')) ?>"></label></div><label class="form-field"><span>Notes</span><textarea name="notes" rows="4"><?= $e($value('notes')) ?></textarea></label><div class="form-actions"><a class="btn btn-secondary" href="/crm/public/invoices/<?= (int)$invoice['id'] ?>/edit">Cancel</a><button class="btn btn-primary" type="submit">Record Payment</button></div></form></section>
