<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); $form=$form??[]; $report=$report??null; $version=$version??null; $value=static fn($k,$d='')=>$form[$k]??$d; $status=$report['status']??'draft'; ?>
<section class="page-header"><div><p class="eyebrow">Clinical · Report</p><h1><?= $report?'Report '.$e($report['report_reference']):'New Clinical Report' ?></h1><p><?= $e($pageDescription??'') ?></p></div><a class="btn btn-secondary" href="/crm/public/reports">Back to Reports</a></section>
<?php if(!empty($error)): ?><div class="alert alert-danger"><?= $e($error) ?></div><?php endif; ?><?php if(!empty($message)): ?><div class="alert alert-success"><?= $e($message) ?></div><?php endif; ?>
<section class="card"><div class="card-header"><div><h2>Report details</h2><p>Therapists may report only on currently assigned patients.</p></div><span class="badge <?= $status==='approved'?'badge-success':'badge-muted' ?>"><?= $e(ucwords(str_replace('_',' ',$status))) ?><?= $report?' · v'.(int)$report['current_version']:'' ?></span></div>
<form method="post" action="<?= $report?'/crm/public/reports/'.(int)$report['id'].'/edit':'/crm/public/reports/create' ?>" class="form-stack"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><div class="grid grid-2">
<label class="form-field"><span>Patient</span><select name="patient_id" required <?= $report?'disabled':'' ?>><option value="">Select patient</option><?php foreach($patients as $p): ?><option value="<?= (int)$p['id'] ?>" <?= (string)$value('patient_id')===(string)$p['id']?'selected':'' ?>><?= $e($p['first_name'].' '.$p['last_name'].' — '.$p['patient_reference']) ?></option><?php endforeach; ?></select><?php if($report): ?><input type="hidden" name="patient_id" value="<?= (int)$report['patient_id'] ?>"><?php endif; ?></label>
<label class="form-field"><span>Therapist</span><select name="therapist_id" required <?= !empty($therapistLocked)?'disabled':'' ?>><option value="">Select therapist</option><?php foreach($therapists as $t): ?><option value="<?= (int)$t['id'] ?>" <?= ((string)($report['therapist_id']??$value('therapist_id')) === (string)$t['id']) ? 'selected' : '' ?>><?= $e($t['full_name']) ?><?= !empty($t['professional_title'])?' — '.$e($t['professional_title']):'' ?></option><?php endforeach; ?></select><?php if(!empty($therapistLocked)&&!empty($therapists[0])): ?><input type="hidden" name="therapist_id" value="<?= (int)$therapists[0]['id'] ?>"><?php endif; ?></label>
</div></section>
<section class="card"><div class="card-header"><div><h2>Clinical report content</h2><p>Approved versions are locked and must never be silently overwritten.</p></div></div><div class="grid grid-2">
<label class="form-field"><span>Clinical summary</span><textarea name="summary" rows="6"><?= $e($value('summary')) ?></textarea></label>
<label class="form-field"><span>Clinical findings</span><textarea name="clinical_findings" rows="6"><?= $e($value('clinical_findings')) ?></textarea></label>
<label class="form-field"><span>Progress</span><textarea name="progress" rows="6"><?= $e($value('progress')) ?></textarea></label>
<label class="form-field"><span>Recommendations</span><textarea name="recommendations" rows="6"><?= $e($value('recommendations')) ?></textarea></label>
<label class="form-field" style="grid-column:1/-1"><span>Additional notes</span><textarea name="additional_notes" rows="6"><?= $e($value('additional_notes')) ?></textarea></label>
</div><div class="form-actions"><?php if($report && ($status ?? '') === 'approved' && \App\Core\Auth::hasPermission('reports.generate')): ?><a class="btn btn-secondary" href="/crm/public/reports/<?= (int)$report['id'] ?>/pdf" target="_blank">View / Print PDF</a><?php endif; ?><a class="btn btn-secondary" href="/crm/public/reports">Cancel</a><?php if(!$report || in_array($status,['draft','returned'],true)): ?><button class="btn btn-primary" type="submit">Save Draft</button><?php endif; ?></div></section></form>
<?php if($report): ?><section class="card"><div class="card-header"><div><h2>Approval workflow</h2><p>Official reports are locked after approval.</p></div></div><div class="form-actions">
<?php if(in_array($status,['draft','returned'],true) && \App\Core\Auth::hasPermission('reports.submit')): ?><form method="post" action="/crm/public/reports/<?= (int)$report['id'] ?>/submit"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-primary" type="submit">Submit for Review</button></form><?php endif; ?>
<?php if($status==='submitted' && \App\Core\Auth::hasPermission('reports.review')): ?><form method="post" action="/crm/public/reports/<?= (int)$report['id'] ?>/review"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-primary" type="submit">Start Review</button></form><?php endif; ?>
<?php if(in_array($status,['submitted','under_review'],true) && \App\Core\Auth::hasPermission('reports.review')): ?><form method="post" action="/crm/public/reports/<?= (int)$report['id'] ?>/return"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-secondary" type="submit">Return for Changes</button></form><?php endif; ?>
<?php if($status==='under_review' && \App\Core\Auth::hasPermission('reports.approve')): ?><form method="post" action="/crm/public/reports/<?= (int)$report['id'] ?>/approve"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-primary" type="submit">Approve & Lock</button></form><?php endif; ?>
<?php if($status==='approved' && \App\Core\Auth::hasPermission('reports.edit')): ?><form method="post" action="/crm/public/reports/<?= (int)$report['id'] ?>/new-version"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-secondary" type="submit">Create New Version</button></form><?php endif; ?></div></section>
<section class="card"><div class="card-header"><div><h2>Version history</h2><p>Every approved clinical version is retained.</p></div></div><div class="table-wrap"><table class="data-table"><thead><tr><th>Version</th><th>Status</th><th>Created By</th><th>Created</th><th>Approved</th></tr></thead><tbody><?php foreach($versions as $v): ?><tr><td><strong>v<?= (int)$v['version_number'] ?></strong></td><td><?= $e(ucwords(str_replace('_',' ',$v['status']))) ?></td><td><?= $e($v['created_by_name']) ?></td><td><?= $e($v['created_at']) ?></td><td><?= $e($v['approved_at']??'—') ?></td></tr><?php endforeach; ?></tbody></table></div></section><?php endif; ?>
