<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); $filters=$filters??[]; $pagination=$pagination??['page'=>1,'pages'=>1,'total'=>0]; ?>
<section class="page-header"><div><p class="eyebrow">Clinical · Sessions</p><h1>Therapy Sessions</h1><p><?= $e($pageDescription??'Manage therapy sessions.') ?></p></div><a class="btn btn-primary" href="/crm/public/sessions/create">Record Session</a></section>
<form class="card filter-card" method="get" action="/crm/public/sessions"><div class="filter-grid"><label class="form-field"><span>Search</span><input 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(['scheduled','completed','cancelled','no_show'] as $s): ?><option value="<?= $s ?>" <?= ($filters['status']??'')===$s?'selected':'' ?>><?= $e(ucwords(str_replace('_',' ',$s))) ?></option><?php endforeach; ?></select></label><label class="form-field"><span>Date</span><input type="date" name="date" value="<?= $e($filters['date']??'') ?>"></label><div class="filter-actions"><button class="btn btn-primary" type="submit">Filter</button><a class="btn btn-secondary" href="/crm/public/sessions">Reset</a></div></div></form>
<section class="card table-card"><div class="card-header"><div><h2>Session history</h2><p><?= (int)$pagination['total'] ?> session<?= (int)$pagination['total']===1?'':'s' ?> found.</p></div></div><?php if(empty($sessions)): ?><div class="empty-state"><h3>No sessions found</h3><p>Record a session for a currently assigned patient to begin the clinical history.</p></div><?php else: ?><div class="table-wrap"><table class="data-table"><thead><tr><th>Date</th><th>Patient</th><th>Therapist</th><th>Service / Type</th><th>Duration</th><th>Status</th><th>Action</th></tr></thead><tbody><?php foreach($sessions as $s): ?><tr><td><?= $e($s['session_date']) ?><br><small><?= $e(substr((string)($s['start_time']??''),0,5)) ?></small></td><td><strong><?= $e($s['first_name'].' '.$s['last_name']) ?></strong><br><small><?= $e($s['patient_reference']) ?></small></td><td><?= $e($s['therapist_name']) ?></td><td><?= $e($s['service_name']??$s['session_type']??'—') ?></td><td><?= $s['duration_minutes']!==null?(int)$s['duration_minutes'].' min':'—' ?></td><td><span class="badge <?= $s['status']==='completed'?'badge-success':($s['status']==='cancelled'?'badge-danger':'badge-muted') ?>"><?= $e(ucwords(str_replace('_',' ',$s['status']))) ?></span></td><td><a class="btn btn-secondary btn-small" href="/crm/public/sessions/<?= (int)$s['id'] ?>/edit">View / Edit</a></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?><?php if(($pagination['pages']??1)>1): ?><div class="pagination"><?php for($p=1;$p<=$pagination['pages'];$p++): ?><a class="page-button <?= $p===$pagination['page']?'active':'' ?>" href="?<?= http_build_query(array_merge($filters,['page'=>$p])) ?>"><?= $p ?></a><?php endfor; ?></div><?php endif; ?></section>
