<?php $e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');$form=$form??[];$appointment=$appointment??null;$value=static fn($k,$d='')=>$form[$k]??($appointment[$k]??$d); ?>
<section class="page-header"><div><p class="eyebrow">Schedule</p><h1><?= $appointment?'Edit Appointment':'New Appointment' ?></h1><p><?= $e($pageDescription??'') ?></p></div><a class="btn btn-secondary" href="/crm/public/appointments">Back</a></section>
<?php if(!empty($error)): ?><div class="alert alert-danger"><?= $e($error) ?></div><?php endif; ?>
<form method="post" action="<?= $appointment?'/crm/public/appointments/'.(int)$appointment['id'].'/edit':'/crm/public/appointments/create' ?>" class="form-stack"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>">
<section class="card"><div class="card-header"><div><h2>Appointment</h2><p>Only active therapist-patient assignments can be scheduled.</p></div></div><div class="grid grid-2">
<label class="form-field"><span>Patient</span><select name="patient_id" required><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></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>Date</span><input type="date" name="appointment_date" value="<?= $e($value('appointment_date',date('Y-m-d'))) ?>" required></label>
<label class="form-field"><span>Service</span><select name="service_id"><option value="">Select service</option><?php foreach($services as $s): ?><option value="<?= (int)$s['id'] ?>" <?= (string)$value('service_id')===(string)$s['id']?'selected':'' ?>><?= $e($s['name']) ?></option><?php endforeach; ?></select></label>
<label class="form-field"><span>Start time</span><input type="time" name="start_time" value="<?= $e($value('start_time')) ?>" required></label><label class="form-field"><span>End time</span><input type="time" name="end_time" value="<?= $e($value('end_time')) ?>" required></label>
<label class="form-field"><span>Status</span><select name="status"><?php foreach(['scheduled','confirmed','completed','cancelled','no_show'] as $s): ?><option value="<?= $s ?>" <?= $value('status','scheduled')===$s?'selected':'' ?>><?= $e(ucwords(str_replace('_',' ',$s))) ?></option><?php endforeach; ?></select></label>
<label class="form-field"><span>Notes</span><textarea name="notes" rows="3" placeholder="Optional appointment notes"><?= $e($value('notes')) ?></textarea></label>
</div><div class="form-actions"><a class="btn btn-secondary" href="/crm/public/appointments">Cancel</a><button class="btn btn-primary" type="submit"><?= $appointment?'Save Changes':'Create Appointment' ?></button></div></section></form>
