<?php
$e=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');
$form=$form??[];
?>
<section class="page-header">
  <div><p class="eyebrow">Clinical · Patient Assignment</p><h1><?= $e($patient['first_name'].' '.$patient['last_name']) ?></h1><p><?= $e($patient['patient_reference']) ?> · Manage the current care team and preserve assignment history.</p></div>
  <a class="btn btn-secondary" href="/crm/public/patients">Back to Patients</a>
</section>
<?php if(!empty($message)): ?><div class="alert alert-success"><?= $e($message) ?></div><?php endif; ?>
<?php if(!empty($error)): ?><div class="alert alert-danger"><?= $e($error) ?></div><?php endif; ?>
<section class="card section-gap">
  <div class="card-header"><div><h2>Assign therapist</h2><p>Only authorized administrators can create or end assignments.</p></div></div>
  <form method="post" action="/crm/public/patients/<?= (int)$patient['id'] ?>/assignments" class="form-grid">
    <input type="hidden" name="_csrf" value="<?= $e($csrf) ?>">
    <label class="form-field"><span>Therapist</span><select name="therapist_id" required><option value="">Select therapist</option><?php foreach($therapists as $t): ?><option value="<?= (int)$t['id'] ?>" <?= ((string)($form['therapist_id']??'')===(string)$t['id'])?'selected':'' ?>><?= $e($t['full_name']) ?><?= $t['professional_title']?' — '.$e($t['professional_title']):'' ?></option><?php endforeach; ?></select></label>
    <label class="form-field"><span>Role</span><select name="role"><option value="primary" <?= (($form['role']??'')==='primary')?'selected':'' ?>>Primary Therapist</option><option value="supporting" <?= (($form['role']??'supporting')==='supporting')?'selected':'' ?>>Supporting Therapist</option></select></label>
    <label class="form-field"><span>Start date</span><input type="date" name="assigned_at" value="<?= $e($form['assigned_at']??date('Y-m-d')) ?>" required></label>
    <label class="form-field form-field-wide"><span>Notes</span><textarea name="notes" rows="3" placeholder="Optional assignment notes"><?= $e($form['notes']??'') ?></textarea></label>
    <div class="form-actions"><button class="btn btn-primary" type="submit">Assign Therapist</button></div>
  </form>
</section>
<section class="card table-card">
  <div class="card-header"><div><h2>Assignment history</h2><p>Historical assignments are never deleted.</p></div></div>
  <?php if(empty($assignments)): ?><div class="empty-state"><h3>No assignments yet</h3><p>Assign a therapist to begin the patient's care-team history.</p></div><?php else: ?>
  <div class="table-wrap"><table class="data-table"><thead><tr><th>Therapist</th><th>Role</th><th>Start</th><th>End</th><th>Status</th><th>Assigned by</th><th>Actions</th></tr></thead><tbody>
  <?php foreach($assignments as $a): ?><tr><td><strong><?= $e($a['therapist_name']) ?></strong><?php if(!empty($a['therapist_email'])):?><br><small><?= $e($a['therapist_email']) ?></small><?php endif;?></td><td><?= $e($a['role']==='primary'?'Primary Therapist':'Supporting Therapist') ?></td><td><?= $e($a['assigned_at']) ?></td><td><?= $e($a['ended_at']??'—') ?></td><td><span class="badge <?= $a['status']==='active'?'badge-success':'badge-muted' ?>"><?= $e(ucfirst($a['status'])) ?></span></td><td><?= $e($a['assigned_by_name']??'—') ?></td><td><?php if($a['status']==='active' && empty($a['ended_at'])): ?><form method="post" action="/crm/public/assignments/<?= (int)$a['id'] ?>/end" onsubmit="return confirm('End this patient assignment? The history will be retained.');"><input type="hidden" name="_csrf" value="<?= $e($csrf) ?>"><input type="date" name="ended_at" value="<?= date('Y-m-d') ?>" required><button class="btn btn-danger btn-small" type="submit">End</button></form><?php else: ?>—<?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div><?php endif; ?>
</section>
