<?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>Invoices</h1><p><?= $e($pageDescription??'') ?></p></div><?php if(\App\Core\Auth::hasPermission('invoice.create')): ?><a class="btn btn-secondary" href="/crm/public/invoices/from-sessions">+ Invoice Sessions</a><a class="btn btn-primary" href="/crm/public/invoices/create">+ New Invoice</a><?php endif; ?></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 number or patient"></label><label class="form-field"><span>Status</span><select name="status"><option value="">All statuses</option><?php foreach(['unpaid'=>'Unpaid','cancelled'=>'Cancelled'] as $k=>$v): ?><option value="<?= $k ?>" <?= ($filters['status']??'')===$k?'selected':'' ?>><?= $v ?></option><?php endforeach; ?></select></label><div class="form-actions"><button class="btn btn-secondary" type="submit">Filter</button><a class="btn btn-secondary" href="/crm/public/invoices">Reset</a></div></form></section>
<section class="card"><div class="card-header"><div><h2>Invoice register</h2><p><?= (int)$invoices['total'] ?> invoice(s)</p></div></div><?php if(empty($invoices['items'])): ?><div class="empty-state"><h3>No invoices found</h3><p>Create the first invoice for a patient.</p></div><?php else: ?><div class="table-wrap"><table class="data-table"><thead><tr><th>Invoice</th><th>Patient</th><th>Date</th><th>Due</th><th>Total</th><th>Paid</th><th>Balance</th><th>Status</th><th>Actions</th></tr></thead><tbody><?php foreach($invoices['items'] as $i): ?><tr><td><strong><?= $e($i['invoice_number']) ?></strong></td><td><?= $e($i['first_name'].' '.$i['last_name']) ?><br><small><?= $e($i['patient_reference']) ?></small></td><td><?= $e($i['invoice_date']) ?></td><td><?= $e($i['due_date']??'—') ?></td><td><strong>KSh <?= number_format((float)$i['total'],2) ?></strong></td><td>KSh <?= number_format((float)$i['paid_amount'],2) ?></td><td>KSh <?= number_format(max(0,(float)$i['balance']),2) ?></td><td><span class="badge badge-muted"><?= $e(ucfirst($i['status'])) ?></span></td><td><?php if(\App\Core\Auth::hasPermission('invoice.view')): ?><a class="btn btn-small btn-secondary" href="/crm/public/invoices/<?= (int)$i['id'] ?>/edit">View</a><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div><?php if(($invoices['pages']??1)>1): ?><div class="pagination"><?php for($p=1;$p<=$invoices['pages'];$p++): ?><a class="<?= $p===$invoices['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>
