<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); $form=$form??[]; $plan=$plan??null; $value=static fn($k,$d='')=>$form[$k]??($plan[$k]??$d); ?>
<section class="page-header"><div><p class="eyebrow">Clinical · Treatment plan</p><h1><?= $plan?'Treatment Plan #'.(int)$plan['id']:'New Treatment Plan' ?></h1><p><?= $e($pageDescription??'') ?></p></div><a class="btn btn-secondary" href="/crm/public/treatment-plans">Back to Treatment Plans</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>Plan details</h2><p>Only a patient with a current therapist assignment can have an active treatment plan.</p></div><span class="badge badge-muted"><?= $e(ucfirst($plan['status']??'draft')) ?></span></div>
<form method="post" action="<?= $plan?'/crm/public/treatment-plans/'.(int)$plan['id'].'/edit':'/crm/public/treatment-plans/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 <?= $plan?'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($plan): ?><input type="hidden" name="patient_id" value="<?= (int)$plan['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>Review date</span><input type="date" name="review_date" value="<?= $e($value('review_date')) ?>"></label><label class="form-field"><span>Frequency</span><input type="text" name="frequency" value="<?= $e($value('frequency')) ?>" placeholder="e.g. Weekly"></label></div></section>
<section class="card"><div class="card-header"><div><h2>Goals & interventions</h2><p>Document measurable goals and the planned therapeutic approach.</p></div></div><div class="grid grid-2">
<label class="form-field"><span>Overall goals</span><textarea name="goals" rows="5"><?= $e($value('goals')) ?></textarea></label><label class="form-field"><span>Short-term goals</span><textarea name="short_term_goals" rows="5"><?= $e($value('short_term_goals')) ?></textarea></label><label class="form-field"><span>Long-term goals</span><textarea name="long_term_goals" rows="5"><?= $e($value('long_term_goals')) ?></textarea></label><label class="form-field"><span>Interventions</span><textarea name="interventions" rows="5"><?= $e($value('interventions')) ?></textarea></label><label class="form-field" style="grid-column:1/-1"><span>Recommendations</span><textarea name="recommendations" rows="5"><?= $e($value('recommendations')) ?></textarea></label></div><div class="form-actions"><a class="btn btn-secondary" href="/crm/public/treatment-plans">Cancel</a><?php if(!$plan || $plan['status']!=='closed'): ?><button class="btn btn-primary" type="submit"><?= $plan?'Save Changes':'Create Treatment Plan' ?></button><?php endif; ?></div></form></section>
<?php if($plan): ?><section class="card"><div class="card-header"><div><h2>Plan status</h2><p>Close is an administrative action; therapists cannot close plans.</p></div></div><div class="form-actions"><?php foreach(['draft'=>'Draft','active'=>'Activate','completed'=>'Mark Completed','closed'=>'Close'] as $status=>$label): if($status===$plan['status']) continue; if($status==='closed'&&!\App\Core\Auth::hasPermission('treatment_plans.manage')) continue; if($status!=='closed'&&!\App\Core\Auth::hasPermission('treatment_plans.manage')&&!\App\Core\Auth::hasRole('THERAPIST')) continue; ?><form method="post" action="/crm/public/treatment-plans/<?= (int)$plan['id'] ?>/status"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><input type="hidden" name="status" value="<?= $status ?>"><button class="btn btn-secondary" type="submit"><?= $e($label) ?></button></form><?php endforeach; ?></div></section><?php endif; ?>
