<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); ?>
<section class="page-header"><div><p class="eyebrow">Clinical · Care planning</p><h1>Treatment Plans</h1><p><?= $e($pageDescription??'') ?></p></div><?php if(\App\Core\Auth::hasPermission('treatment_plans.create')): ?><a class="btn btn-primary" href="/crm/public/treatment-plans/create">+ New Treatment Plan</a><?php endif; ?></section>
<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="Patient or therapist"></label><label class="form-field"><span>Status</span><select name="status"><option value="">All statuses</option><?php foreach(['draft','active','completed','closed'] as $s): ?><option value="<?= $s ?>" <?= ($filters['status']??'')===$s?'selected':'' ?>><?= $e(ucfirst($s)) ?></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/treatment-plans">Reset</a></div></form></section>
<section class="card"><div class="card-header"><div><h2>Care plans</h2><p><?= (int)$plans['total'] ?> plan(s)</p></div></div><?php if(empty($plans['items'])): ?><div class="empty-state"><h3>No treatment plans found</h3><p>Create a plan for a currently assigned patient.</p></div><?php else: ?><div class="table-wrap"><table class="data-table"><thead><tr><th>Patient</th><th>Therapist</th><th>Status</th><th>Review date</th><th>Updated</th><th>Actions</th></tr></thead><tbody><?php foreach($plans['items'] as $p): ?><tr><td><strong><?= $e($p['first_name'].' '.$p['last_name']) ?></strong><div class="muted"><?= $e($p['patient_reference']) ?></div></td><td><?= $e($p['therapist_name']) ?></td><td><span class="badge badge-muted"><?= $e(ucfirst($p['status'])) ?></span></td><td><?= $e($p['review_date']??'—') ?></td><td><?= $e($p['updated_at']??'') ?></td><td><a class="btn btn-small btn-secondary" href="/crm/public/treatment-plans/<?= (int)$p['id'] ?>/edit">View / Edit</a></td></tr><?php endforeach; ?></tbody></table></div><?php if(($plans['pages']??1)>1): ?><div class="pagination"><?php for($i=1;$i<=$plans['pages'];$i++): ?><a class="<?= $i===$plans['page']?'active':'' ?>" href="?page=<?= $i ?>&search=<?= urlencode((string)($filters['search']??'')) ?>&status=<?= urlencode((string)($filters['status']??'')) ?>"><?= $i ?></a><?php endfor; ?></div><?php endif; ?><?php endif; ?></section>
