<?php $isEdit=is_array($therapist); $form=$form??[]; $value=static function(string $key,string $default='') use($form,$therapist):string { if(array_key_exists($key,$form)) return (string)$form[$key]; return is_array($therapist)?(string)($therapist[$key]??$default):$default; }; ?>
<section class="page-header"><div><p class="eyebrow">Clinical / Therapists</p><h1><?=$isEdit?'Edit Therapist':'Add Therapist'?></h1><p><?=$isEdit?'Update professional profile details.':'Create a therapist profile for an existing Therapist user.'?></p></div><a class="btn btn-secondary" href="/crm/public/therapists">Back to Therapists</a></section>
<?php if(!empty($error)): ?><div class="alert alert-danger"><?=htmlspecialchars($error,ENT_QUOTES,'UTF-8')?></div><?php endif; ?>
<?php if(!empty($message)): ?><div class="alert alert-success"><?=htmlspecialchars($message,ENT_QUOTES,'UTF-8')?></div><?php endif; ?>
<section class="card form-card section-gap"><form method="post" action="<?=$isEdit?'/crm/public/therapists/'.(int)$therapist['id'].'/edit':'/crm/public/therapists/create'?>" class="form-stack"><input type="hidden" name="_csrf" value="<?=htmlspecialchars($csrf,ENT_QUOTES,'UTF-8')?>"><?php if(!$isEdit): ?><label class="form-field"><span>Therapist user *</span><select name="user_id" required><option value="">Select a Therapist user</option><?php foreach($users as $user): ?><option value="<?= (int)$user['id'] ?>" <?=((int)($form['user_id']??0)===(int)$user['id'])?'selected':''?>><?=htmlspecialchars($user['full_name'].' — '.$user['email'],ENT_QUOTES,'UTF-8')?></option><?php endforeach; ?></select><small>Only active users assigned the Therapist role and without an existing profile are shown.</small></label><?php else: ?><div class="detail-list"><div><dt>Therapist</dt><dd><?=htmlspecialchars($therapist['full_name'],ENT_QUOTES,'UTF-8')?></dd></div><div><dt>Email</dt><dd><?=htmlspecialchars($therapist['email'],ENT_QUOTES,'UTF-8')?></dd></div></div><?php endif; ?><div class="grid grid-2"><label class="form-field"><span>Professional title</span><input maxlength="120" name="professional_title" value="<?=htmlspecialchars($value('professional_title'),ENT_QUOTES,'UTF-8')?>" placeholder="e.g. Occupational Therapist"></label><label class="form-field"><span>Registration number</span><input maxlength="120" name="registration_number" value="<?=htmlspecialchars($value('registration_number'),ENT_QUOTES,'UTF-8')?>"></label></div><label class="form-field"><span>Status</span><select name="status"><option value="active" <?=$value('status','active')==='active'?'selected':''?>>Active</option><option value="inactive" <?=$value('status')==='inactive'?'selected':''?>>Inactive</option></select></label><label class="form-field"><span>Notes</span><textarea name="notes" rows="6" maxlength="5000" placeholder="Professional notes..."> <?=htmlspecialchars($value('notes'),ENT_QUOTES,'UTF-8')?></textarea></label><div class="form-actions"><a class="btn btn-secondary" href="/crm/public/therapists">Cancel</a><button class="btn btn-primary" type="submit"><?=$isEdit?'Save Changes':'Create Therapist'?></button></div></form></section>
