<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); $form=$form??[]; $assessment=$assessment??null; $value=static fn($k,$d='')=>$form[$k]??($assessment[$k]??$d); ?>
<section class="page-header"><div><p class="eyebrow">Clinical · Assessment</p><h1><?= $assessment?'Assessment '.$e($assessment['assessment_reference']):'New Assessment' ?></h1><p><?= $e($pageDescription??'') ?></p></div><a class="btn btn-secondary" href="/crm/public/assessments">Back to Assessments</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>Assessment details</h2><p>Only patients currently assigned to the selected therapist can be assessed.</p></div><span class="badge badge-muted"><?= $e(ucfirst($assessment['status']??'draft')) ?></span></div>
<form method="post" action="<?= $assessment?'/crm/public/assessments/'.(int)$assessment['id'].'/edit':'/crm/public/assessments/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 <?= $assessment?'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($assessment): ?><input type="hidden" name="patient_id" value="<?= (int)$assessment['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)$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>
<label class="form-field"><span>Assessment service</span><select name="service_id" required <?= ($assessment && ($assessment['status']??'')==='approved')?'disabled':'' ?>><option value="">Select assessment service</option><?php foreach(($services??[]) as $service): ?><option value="<?= (int)$service['id'] ?>" <?= (string)$value('service_id')===(string)$service['id']?'selected':'' ?>><?= $e($service['name']) ?></option><?php endforeach; ?></select><?php if($assessment && ($assessment['status']??'')==='approved'): ?><input type="hidden" name="service_id" value="<?= (int)$assessment['service_id'] ?>"><?php endif; ?></label>
<label class="form-field"><span>Assessment date</span><input type="date" name="assessment_date" value="<?= $e($value('assessment_date',date('Y-m-d'))) ?>" required></label>
</div></section>
<section class="card"><div class="card-header"><div><h2>Clinical documentation</h2><p>Record objective, patient-specific clinical information.</p></div></div><div class="grid grid-2">
<label class="form-field"><span>Reason for referral</span><textarea name="reason_for_referral" rows="5"><?= $e($value('reason_for_referral')) ?></textarea></label>
<label class="form-field"><span>Background</span><textarea name="background" rows="5"><?= $e($value('background')) ?></textarea></label>
<label class="form-field"><span>Observations</span><textarea name="observations" rows="5"><?= $e($value('observations')) ?></textarea></label>
<label class="form-field"><span>Clinical findings</span><textarea name="clinical_findings" rows="5"><?= $e($value('clinical_findings')) ?></textarea></label>
<label class="form-field"><span>Goals</span><textarea name="goals" rows="5"><?= $e($value('goals')) ?></textarea></label>
<label class="form-field"><span>Recommendations</span><textarea name="recommendations" rows="5"><?= $e($value('recommendations')) ?></textarea></label>
<label class="form-field" style="grid-column:1/-1"><span>Additional notes</span><textarea name="additional_notes" rows="5"><?= $e($value('additional_notes')) ?></textarea></label>
</div><div class="form-actions"><a class="btn btn-secondary" href="/crm/public/assessments">Cancel</a><?php if(!$assessment || $assessment['status']!=='approved'): ?><?php if(!$assessment || $assessment['status']!=='submitted'): ?><button class="btn btn-primary" type="submit"><?= $assessment?'Save Changes':'Create Assessment' ?></button><?php endif; ?><?php endif; ?></div></section></form>
<?php if($assessment && $assessment['status']=='approved'): ?><div class="form-actions" style="margin:12px 0"><a class="btn btn-secondary" target="_blank" href="/crm/public/assessments/<?= (int)$assessment['id'] ?>/pdf">View / Print Assessment PDF</a></div><?php endif; ?><?php if($assessment): ?><section class="card"><div class="card-header"><div><h2>Workflow</h2><p>Assessment status controls the clinical review process.</p></div></div><div class="form-actions"><form method="post" action="/crm/public/assessments/<?= (int)$assessment['id'] ?>/submit"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><?php if($assessment['status']==='draft' && \App\Core\Auth::hasPermission('assessments.submit')): ?><button class="btn btn-primary" type="submit">Submit for Review</button><?php endif; ?></form><?php if($assessment['status']==='submitted' && \App\Core\Auth::hasPermission('assessments.review')): ?><form method="post" action="/crm/public/assessments/<?= (int)$assessment['id'] ?>/review"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-primary" type="submit">Mark Reviewed</button></form><?php endif; ?><?php if($assessment['status']==='reviewed' && \App\Core\Auth::hasPermission('assessments.approve')): ?><form method="post" action="/crm/public/assessments/<?= (int)$assessment['id'] ?>/approve"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><button class="btn btn-primary" type="submit">Approve Assessment</button></form><?php endif; ?></div></section><?php endif; ?>
