<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');$patientId=(int)($patientId??0); ?>
<section class="page-header"><div><p class="eyebrow">Billing · Patient rates</p><h1>Patient Billing Rates</h1><p><?= $e($pageDescription) ?></p></div></section>
<?php if(!empty($error)): ?><div class="alert alert-danger"><?= $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>Select patient</h2><p>Different patients can have different negotiated rates for the same service.</p></div></div><form method="get" class="form-actions"><label class="form-field" style="min-width:320px"><span>Patient</span><select name="patient_id" required><option value="">Select patient</option><?php foreach($patients as $p): ?><option value="<?= (int)$p['id'] ?>" <?= $patientId===(int)$p['id']?'selected':'' ?>><?= $e($p['first_name'].' '.$p['last_name'].' — '.$p['patient_reference']) ?></option><?php endforeach; ?></select></label><button class="btn btn-secondary">Load rates</button></form></section>
<?php if($patientId>0): ?><section class="card"><div class="card-header"><div><h2>Add or change rate</h2><p>The patient rate is used when selected clinical services are invoiced.</p></div></div><form method="post" class="grid grid-2"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><input type="hidden" name="patient_id" value="<?= $patientId ?>"><label class="form-field"><span>Service</span><select name="service_id" required><option value="">Select service</option><?php foreach($services as $s): ?><option value="<?= (int)$s['id'] ?>"><?= $e($s['name']) ?> — catalogue KSh <?= number_format((float)$s['price'],2) ?></option><?php endforeach; ?></select></label><label class="form-field"><span>Patient rate (KSh)</span><input name="rate" required inputmode="decimal" placeholder="3000.00"></label><label class="form-field"><span>Package label</span><input name="label" placeholder="Standard / Discount / Package A"></label><div class="form-actions"><button class="btn btn-primary">Save rate</button></div></form></section>
<section class="card"><div class="card-header"><div><h2>Current rates</h2></div></div><div class="table-wrap"><table class="data-table"><thead><tr><th>Service</th><th>Catalogue</th><th>Patient rate</th><th>Package</th><th></th></tr></thead><tbody><?php foreach($rates as $r): ?><tr><td><?= $e($r['service_name']) ?></td><td>KSh <?= number_format((float)$r['catalogue_price'],2) ?></td><td><strong>KSh <?= number_format((float)$r['rate'],2) ?></strong></td><td><?= $e($r['label']??'—') ?></td><td><form method="post" action="/crm/public/billing-rates/<?= (int)$r['id'] ?>/delete"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><input type="hidden" name="patient_id" value="<?= $patientId ?>"><button class="btn btn-small btn-secondary" onclick="return confirm('Remove this patient rate?')">Remove</button></form></td></tr><?php endforeach; ?></tbody></table></div></section><?php endif; ?>
