<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); $filters=$filters??[]; $data=$reports??['items'=>[],'total'=>0,'page'=>1,'pages'=>1]; $items=$data['items']??[]; $statuses=['draft','submitted','under_review','returned','approved']; ?>
<section class="page-header"><div><p class="eyebrow">Clinical · Reports</p><h1>Clinical Reports</h1><p><?= $e($pageDescription??'Clinical reports and approval workflow.') ?></p></div><?php if(\App\Core\Auth::hasPermission('reports.create')): ?><a class="btn btn-primary" href="/crm/public/reports/create">New Report</a><?php endif; ?></section>
<form class="card filter-card" method="get" action="/crm/public/reports"><div class="filter-grid"><label class="form-field"><span>Search</span><input name="search" value="<?= $e($filters['search']??'') ?>" placeholder="Reference, patient or therapist"></label><label class="form-field"><span>Status</span><select name="status"><option value="">All statuses</option><?php foreach($statuses as $s): ?><option value="<?= $e($s) ?>" <?= ($filters['status']??'')===$s?'selected':'' ?>><?= $e(ucwords(str_replace('_',' ',$s))) ?></option><?php endforeach; ?></select></label><div class="filter-actions"><button class="btn btn-primary" type="submit">Filter</button><a class="btn btn-secondary" href="/crm/public/reports">Reset</a></div></div></form>
<section class="card table-card"><div class="card-header"><div><h2>Report records</h2><p><?= (int)$data['total'] ?> record<?= (int)$data['total']===1?'':'s' ?> found.</p></div></div><?php if(!$items): ?><div class="empty-state"><h3>No reports found</h3><p>Create a clinical report for a patient with a current therapist assignment.</p></div><?php else: ?><div class="table-wrap"><table class="data-table"><thead><tr><th>Reference</th><th>Patient</th><th>Therapist</th><th>Version</th><th>Status</th><th>Updated</th><th>Action</th></tr></thead><tbody><?php foreach($items as $r): ?><tr><td><strong><?= $e($r['report_reference']) ?></strong></td><td><strong><?= $e($r['first_name'].' '.$r['last_name']) ?></strong><br><small><?= $e($r['patient_reference']) ?></small></td><td><?= $e($r['therapist_name']) ?></td><td>v<?= (int)$r['current_version'] ?></td><td><span class="badge <?= $r['status']==='approved'?'badge-success':'badge-muted' ?>"><?= $e(ucwords(str_replace('_',' ',$r['status']))) ?></span></td><td><?= $e($r['updated_at']) ?></td><td><a class="btn btn-secondary btn-small" href="/crm/public/reports/<?= (int)$r['id'] ?>/edit">View / Edit</a></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?><?php if(($data['pages']??1)>1): ?><div class="pagination"><?php for($p=1;$p<=$data['pages'];$p++): ?><a class="page-button <?= $p===$data['page']?'active':'' ?>" href="?<?= http_build_query(array_merge($filters,['page'=>$p])) ?>"><?= $p ?></a><?php endfor; ?></div><?php endif; ?></section>
