<?php
require_once __DIR__ . '/../../admin/_nav.php';
$pageTitle  = 'Edit Student';
$sidebarNav = adminNav();
$u = function($p) { return \Core\View::url($p); };
$parent = $student['parents'][0] ?? null;
?>
<div class="page-header">
  <div><h1><i class="bi bi-pencil-square"></i> Edit: <?= htmlspecialchars($student['name']) ?></h1></div>
  <div style="display:flex;gap:8px">
    <a href="<?= $u('admin/students/'.$student['id']) ?>" class="btn btn-secondary"><i class="bi bi-eye"></i> View Profile</a>
    <a href="<?= $u('admin/students') ?>" class="btn btn-secondary"><i class="bi bi-arrow-left"></i> Back</a>
  </div>
</div>

<form method="POST" action="<?= $u('admin/students/'.$student['id'].'/update') ?>">
  <?= \Core\View::csrf() ?>

  <div class="grid g2" style="align-items:start">

    <!-- ── Student Details ── -->
    <div class="card">
      <div class="card-header"><h5><i class="bi bi-person"></i> Student Details</h5></div>
      <div class="card-body">
        <div class="grid g2">
          <div class="form-group">
            <label>Full Name *</label>
            <input type="text" name="name" value="<?= htmlspecialchars($student['name']) ?>" required>
          </div>
          <div class="form-group">
            <label>Assessment Number *</label>
            <input type="text" name="assessment_no" value="<?= htmlspecialchars($student['assessment_no']) ?>" required>
          </div>
          <div class="form-group">
            <label>Stream *</label>
            <select name="stream_id" required>
              <?php foreach($streams as $s): ?>
              <option value="<?= $s['id'] ?>" <?= $student['stream_id']==$s['id']?'selected':'' ?>><?= htmlspecialchars($s['full_name']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label>Gender *</label>
            <select name="gender" required>
              <option value="male"   <?= $student['gender']==='male'  ?'selected':'' ?>>Male</option>
              <option value="female" <?= $student['gender']==='female'?'selected':'' ?>>Female</option>
            </select>
          </div>
          <div class="form-group">
            <label>Date of Birth</label>
            <input type="date" name="dob" value="<?= htmlspecialchars($student['dob'] ?? '') ?>">
          </div>
          <div class="form-group">
            <label>Admission Date</label>
            <input type="date" name="admission_date" value="<?= htmlspecialchars($student['admission_date'] ?? '') ?>">
          </div>
          <div class="form-group">
            <label>Status *</label>
            <select name="status" required>
              <?php foreach(['active','transferred','graduated','suspended'] as $st): ?>
              <option value="<?= $st ?>" <?= $student['status']===$st?'selected':'' ?>><?= ucfirst($st) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>
      </div>
    </div>

    <!-- ── Parent / Guardian ── -->
    <div class="card">
      <div class="card-header"><h5><i class="bi bi-person-heart"></i> Parent / Guardian</h5></div>
      <div class="card-body">
        <?php if($parent): ?>
        <input type="hidden" name="parent_id" value="<?= $parent['id'] ?>">
        <div class="grid g2">
          <div class="form-group">
            <label>Parent Full Name</label>
            <input type="text" name="parent_name" value="<?= htmlspecialchars($parent['parent_name']) ?>">
          </div>
          <div class="form-group">
            <label>Parent Phone</label>
            <input type="text" name="parent_phone" value="<?= htmlspecialchars($parent['parent_phone'] ?? '') ?>" placeholder="0712345678">
            <small style="color:var(--gray)">Used for SMS notifications</small>
          </div>
          <div class="form-group">
            <label>Parent Email</label>
            <input type="email" name="parent_email" value="<?= htmlspecialchars($parent['parent_email'] ?? '') ?>">
          </div>
          <div class="form-group">
            <label>Relationship</label>
            <select name="parent_relationship">
              <?php foreach(['parent','guardian','mother','father','sibling','other'] as $rel): ?>
              <option value="<?= $rel ?>" <?= ($parent['relationship']??'parent')===$rel?'selected':'' ?>><?= ucfirst($rel) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>
        <?php if(count($student['parents']) > 1): ?>
        <div style="margin-top:12px;padding:10px;background:var(--light);border-radius:8px;font-size:12px;color:var(--gray)">
          <i class="bi bi-info-circle"></i> This student has <?= count($student['parents']) ?> linked parents. Editing the primary one above.
        </div>
        <?php endif; ?>
        <?php else: ?>
        <div style="padding:20px;text-align:center;color:var(--gray)">
          <i class="bi bi-person-x" style="font-size:32px;display:block;margin-bottom:8px"></i>
          No parent linked yet.
        </div>
        <div class="grid g2" style="margin-top:12px">
          <div class="form-group">
            <label>Parent Full Name</label>
            <input type="text" name="parent_name" placeholder="e.g. John Kamau">
          </div>
          <div class="form-group">
            <label>Parent Phone</label>
            <input type="text" name="parent_phone" placeholder="0712345678">
            <small style="color:var(--gray)">Used for SMS notifications</small>
          </div>
          <div class="form-group">
            <label>Parent Email</label>
            <input type="email" name="parent_email" placeholder="parent@email.com">
          </div>
          <div class="form-group">
            <label>Relationship</label>
            <select name="parent_relationship">
              <?php foreach(['parent','guardian','mother','father','sibling','other'] as $rel): ?>
              <option value="<?= $rel ?>"><?= ucfirst($rel) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>
        <?php endif; ?>
      </div>
    </div>

  </div>

  <div style="margin-top:16px">
    <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Save Changes</button>
    <a href="<?= $u('admin/students/'.$student['id']) ?>" class="btn btn-secondary" style="margin-left:8px">Cancel</a>
  </div>
</form>
