<?php
//  add this *inside* your foreach loop
$modalId   = "editStudentModal{$student['id']}";
$formId    = "studentEditForm{$student['id']}";
$actionUrl = site_url('parent/edit_student/' . $student['id']);
?>
<!--–––– MODAL ––––-->
<div class="modal fade" id="<?= $modalId ?>" tabindex="-1" aria-labelledby="<?= $modalId ?>Label" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <!-- one form only -->
      <form method="post"
            action="<?= $actionUrl ?>"
            id="<?= $formId ?>"
            class="student-edit-form needs-live-validation">
        <?= csrf_field() ?>

        <div class="modal-header">
          <h5 class="modal-title" id="<?= $modalId ?>Label">
            Edit Student: <?= esc($student['firstname'] . ' ' . $student['lastname']) ?>
          </h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>

        <div class="modal-body">
          <!-- First / Last -->
          <div class="row mb-2">
            <div class="col-md-6">
              <label class="form-label">First Name</label>
              <input type="text" name="firstname" class="form-control" value="<?= esc($student['firstname']) ?>" maxlength="30" required>
            </div>
            <div class="col-md-6">
              <label class="form-label">Last Name</label>
              <input type="text" name="lastname" class="form-control" value="<?= esc($student['lastname']) ?>" maxlength="30" required>
            </div>
          </div>

          <!-- DOB / Gender -->
          <div class="row mb-2">
            <div class="col-md-6">
              <label class="form-label">Date of Birth</label>
              <input type="date" name="dob" class="form-control" value="<?= esc($student['dob']) ?>" required>
            </div>
            <div class="col-md-6">
              <label class="form-label">Gender</label>
              <select name="gender" class="form-select" required>
                <option value="">-- Select --</option>
                <option value="Male"   <?= strtolower($student['gender']) === 'male'   ? 'selected' : '' ?>>Male</option>
                <option value="Female" <?= strtolower($student['gender']) === 'female' ? 'selected' : '' ?>>Female</option>
              </select>
            </div>
          </div>

          <!-- Allergies / Conditions -->
          <div class="row mb-2">
            <div class="col-md-6">
              <label class="form-label">Allergies (comma-separated)</label>
              <input type="text" name="allergies" class="form-control"
                     value="<?= esc(is_array($student['allergies']) ? implode(', ', $student['allergies']) : $student['allergies']) ?>">
            </div>
            <div class="col-md-6">
              <label class="form-label">Medical Conditions (comma-separated)</label>
              <input type="text" name="medical_conditions" class="form-control"
                     value="<?= esc(is_array($student['medical_conditions']) ? implode(', ', $student['medical_conditions']) : $student['medical_conditions']) ?>">
            </div>
          </div>

          <!-- Consent / Grade -->
          <div class="row mb-2">
            <div class="col-md-6">
              <label class="form-label">Photo Consent</label>
              <select name="photo_consent" class="form-select" required>
                <option value="">-- Select --</option>
                <option value="1" <?= $student['photo_consent'] ? 'selected' : '' ?>>Yes</option>
                <option value="0" <?= !$student['photo_consent'] ? 'selected' : '' ?>>No</option>
              </select>
            </div>
            <div class="col-md-6">
              <label class="form-label">Registration Grade</label>
              <input type="text" name="registration_grade" class="form-control" value="<?= esc($student['registration_grade']) ?>" required>
            </div>
          </div>
        </div>

        <div class="modal-footer">
          <button type="submit" class="btn btn-success">
            <i class="fas fa-save"></i> Save Changes
          </button>
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
        </div>
      </form>
    </div>
  </div>
</div>
