<?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>Patients</h1><p>Manage patient records and demographic information.</p></div><a class="btn btn-primary" href="/crm/public/patients/create">+ Add Patient</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="Reference, name, phone or email"></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><option value="discharged" <?=$filters['status']==='discharged'?'selected':''?>>Discharged</option></select></label><div class="filter-actions"><button class="btn btn-primary" type="submit">Filter</button><a class="btn btn-secondary" href="/crm/public/patients">Reset</a></div></form></section>
<section class="card table-card"><div class="card-header"><div><h2>Patient directory</h2><p><?=$pagination['total']?> patient(s)</p></div></div><?php if(empty($patients)): ?><div class="empty-state"><h3>No patients found</h3><p>Create the first patient record to begin building the clinical register.</p></div><?php else: ?><div class="table-wrap"><table class="data-table"><thead><tr><th>Reference</th><th>Patient</th><th>Date of birth</th><th>Contact</th><th>Status</th><th>Actions</th></tr></thead><tbody><?php foreach($patients as $p): ?><tr><td><strong><?=$e($p['patient_reference'])?></strong></td><td><strong><?=$e($p['first_name'].' '.$p['last_name'])?></strong><?php if(!empty($p['gender'])):?><br><small><?=$e($p['gender'])?></small><?php endif;?></td><td><?=$e($p['date_of_birth']??'—')?></td><td><?=$e($p['phone']??'—')?><br><?php if(!empty($p['email'])):?><small><?=$e($p['email'])?></small><?php endif;?></td><td><span class="badge <?= $p['status']==='active'?'badge-success':($p['status']==='discharged'?'badge-danger':'badge-muted') ?>"><?=$e(ucfirst($p['status']))?></span></td><td><div class="action-row"><?php if(\App\Core\Auth::hasRole('THERAPIST')): ?><a class="btn btn-secondary btn-small" href="/crm/public/patients/<?= (int)$p['id'] ?>">View</a><?php else: ?><a class="btn btn-secondary btn-small" href="/crm/public/patients/<?= (int)$p['id'] ?>/edit">Edit</a><?php endif; ?><form method="post" action="/crm/public/patients/<?= (int)$p['id'] ?>/delete" onsubmit="return confirm('Archive this patient record? The record will be removed from the active directory but retained for audit/history.');"><input type="hidden" name="_csrf" value="<?=$e(\App\Core\Csrf::token())?>"><button class="btn btn-danger btn-small" type="submit">Delete</button></form><?php if(\App\Core\Auth::hasPermission('patients.assign')): ?><a class="btn btn-secondary btn-small" href="/crm/public/patients/<?= (int)$p['id'] ?>/assignments">Assign</a><?php endif; ?></div></td></tr><?php endforeach;?></tbody></table></div><?php endif;?></section>
<?php if(($pagination['pages']??1)>1): ?><nav class="pagination" aria-label="Patient 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; ?>
