<?php
$filters=$filters??['search'=>'','status'=>'']; $pagination=$pagination??['page'=>1,'pages'=>1,'total'=>0];
$query=static function(int $page) use($filters):string { $p=array_filter(['search'=>$filters['search'],'status'=>$filters['status']],fn($v)=>$v!==''); $p['page']=$page; return '?'.http_build_query($p); };
$e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');
?>
<section class="page-header"><div><p class="eyebrow">Clinical</p><h1>Therapists</h1><p>Manage therapist profiles and professional details.</p></div><a class="btn btn-primary" href="/crm/public/therapists/create">+ Add Therapist</a></section>
<?php if(!empty($message)): ?><div class="alert alert-success"><?=$e($message)?></div><?php endif; ?>
<?php if(!empty($error)): ?><div class="alert alert-danger"><?=$e($error)?></div><?php endif; ?>
<section class="card section-gap"><form method="get" class="filter-grid"><label class="form-field"><span>Search</span><input type="search" name="search" value="<?=$e($filters['search'])?>" placeholder="Name, email, title or registration"></label><label class="form-field"><span>Status</span><select name="status"><option value="">All statuses</option><option value="active" <?=$filters['status']==='active'?'selected':''?>>Active</option><option value="inactive" <?=$filters['status']==='inactive'?'selected':''?>>Inactive</option></select></label><div class="filter-actions"><button class="btn btn-primary" type="submit">Filter</button><a class="btn btn-secondary" href="/crm/public/therapists">Reset</a></div></form></section>
<section class="card table-card"><div class="card-header"><div><h2>Therapist directory</h2><p><?=$pagination['total']?> therapist(s)</p></div></div><?php if(empty($therapists)): ?><div class="empty-state"><h3>No therapists found</h3><p>Create a user with the Therapist role first, then add their therapist profile.</p></div><?php else: ?><div class="table-wrap"><table class="data-table"><thead><tr><th>Therapist</th><th>Professional title</th><th>Registration</th><th>Contact</th><th>Status</th><th>Actions</th></tr></thead><tbody><?php foreach($therapists as $t): ?><tr><td><strong><?=$e($t['full_name'])?></strong><br><small><?=$e($t['email'])?></small></td><td><?=$e($t['professional_title']??'—')?></td><td><?=$e($t['registration_number']??'—')?></td><td><?=$e($t['phone']??'—')?></td><td><span class="badge <?=$t['status']==='active'?'badge-success':'badge-muted'?>"><?=$e(ucfirst($t['status']))?></span></td><td><div class="action-row"><a class="btn btn-secondary btn-small" href="/crm/public/therapists/<?= (int)$t['id'] ?>/edit">Edit</a><form method="post" action="/crm/public/therapists/<?= (int)$t['id'] ?>/delete" onsubmit="return confirm('Archive this therapist profile? The record will be removed from the active directory but retained for audit/history.');"><input type="hidden" name="_csrf" value="<?=htmlspecialchars(\App\Core\Csrf::token(),ENT_QUOTES,'UTF-8')?>"><button class="btn btn-danger btn-small" type="submit">Delete</button></form></div></td></tr><?php endforeach;?></tbody></table></div><?php endif;?></section>
<?php if(($pagination['pages']??1)>1): ?><nav class="pagination" aria-label="Therapist pagination"><?php for($p=1;$p<=$pagination['pages'];$p++): ?><a class="<?=$p===$pagination['page']?'active':''?>" href="<?=$e($query($p))?>"><?=$p?></a><?php endfor;?></nav><?php endif; ?>
