<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');$form=$form??[];$patientId=(int)($form['patient_id']??$patientId??0);$sessions=$sessions??[];$assessments=$assessments??[]; ?>
<section class="page-header"><div><p class="eyebrow">Billing · Clinical work</p><h1>Invoice Clinical Services</h1><p><?= $e($pageDescription??'Create one invoice from completed therapy services and approved assessments.') ?></p></div><a class="btn btn-secondary" href="/crm/public/invoices">Back to Invoices</a></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>Only work that has not already been invoiced is available.</p></div></div><form method="get" action="/crm/public/invoices/from-sessions" 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" type="submit">Load Billable Work</button></form></section>
<?php if($patientId>0): ?><form method="post" action="/crm/public/invoices/from-sessions" class="form-stack"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><input type="hidden" name="patient_id" value="<?= $patientId ?>">
<section class="card"><div class="card-header"><div><h2>Completed therapy services</h2><p>Every completed session with a selected service is automatically billable.</p></div></div><?php if($sessions): ?><div class="table-wrap"><table class="data-table"><thead><tr><th></th><th>Date</th><th>Service</th><th>Therapist</th><th>Duration</th><th>Price</th></tr></thead><tbody><?php foreach($sessions as $s): ?><tr><td><input type="checkbox" name="session_ids[]" value="<?= (int)$s['id'] ?>"></td><td><?= $e($s['session_date']) ?></td><td><?= $e($s['service_name']) ?></td><td><?= $e($s['therapist_name']) ?></td><td><?= $s['duration_minutes']!==null?(int)$s['duration_minutes'].' min':'—' ?></td><td>KSh <?= number_format((float)$s['service_price'],2) ?></td></tr><?php endforeach; ?></tbody></table></div><?php else: ?><div class="empty-state"><strong>No unbilled completed sessions.</strong><p>Completed sessions with services are automatically billable and disappear here after invoicing.</p></div><?php endif; ?>
</section>
<section class="card"><div class="card-header"><div><h2>Approved assessment services</h2><p>Approved assessments with an assessment service are automatically billable.</p></div></div><?php if($assessments): ?><div class="table-wrap"><table class="data-table"><thead><tr><th></th><th>Date</th><th>Assessment</th><th>Therapist</th><th>Price</th><th>PDF</th></tr></thead><tbody><?php foreach($assessments as $a): ?><tr><td><input type="checkbox" name="assessment_ids[]" value="<?= (int)$a['id'] ?>"></td><td><?= $e($a['assessment_date']) ?></td><td><?= $e($a['service_name']) ?><br><small><?= $e($a['assessment_reference']) ?></small></td><td><?= $e($a['therapist_name']) ?></td><td>KSh <?= number_format((float)$a['service_price'],2) ?></td><td><a class="btn btn-secondary btn-small" target="_blank" href="/crm/public/assessments/<?= (int)$a['id'] ?>/pdf">PDF</a></td></tr><?php endforeach; ?></tbody></table></div><?php else: ?><div class="empty-state"><strong>No unbilled approved assessments.</strong><p>Approve an assessment and give it an assessment service to make it billable.</p></div><?php endif; ?></section>
<?php if($sessions || $assessments): ?><section class="card"><div class="grid grid-2"><label class="form-field"><span>Invoice date</span><input type="date" name="invoice_date" value="<?= $e($form['invoice_date']??date('Y-m-d')) ?>" required></label><label class="form-field"><span>Due date</span><input type="date" name="due_date" value="<?= $e($form['due_date']??'') ?>"></label><label class="form-field"><span>Discount (KSh)</span><input name="discount" value="<?= $e($form['discount']??'0') ?>" inputmode="decimal"></label><label class="form-field"><span>Tax (KSh)</span><input name="tax" value="<?= $e($form['tax']??'0') ?>" inputmode="decimal"></label></div><label class="form-field"><span>Notes</span><textarea name="notes" rows="4"><?= $e($form['notes']??'') ?></textarea></label><div class="form-actions"><a class="btn btn-secondary" href="/crm/public/invoices">Cancel</a><button class="btn btn-primary" type="submit">Create Invoice From Selected Work</button></div></section><?php endif; ?>
</form><?php endif; ?>