<?php

include('includes/session.php');

error_reporting(0);

include('includes/dbconnection.php');
include('includes/flash.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName = $stResult['term_name'];

if (isset($_POST['submit'])) {

  $eid = $_SESSION['student_id'];

  $contactno = $_POST['phone'];

  $email = $_POST['email'];

  $gender = $_POST['gender'];

  $nextphone = $_POST['nextphone'];

  $country = $_POST['country'];

  $age = $_POST['age'];

  $district = $_POST['district'];

  $state = $_POST['state'];

  $village = $_POST['village'];

  $parentName = $_POST['parentName'];

  $relation = $_POST['relation'];

  $occupation = $_POST['occupation'];

  $language = $_POST['language'];

  $religion = $_POST['religion'];

  $nationality = $_POST['nationality'];

  $work_address = $_POST['work_address'];

  $hospital_name = $_POST['hospital_name'];

  $hospital_phone = $_POST['hospital_phone'];

  $doctor_address = $_POST['doctor_address'];

  $doctor_phone = $_POST['doctor_phone'];

  $medical_history = $_POST['medical_history'];

  $physical_peculiarity = $_POST['physical_peculiarity'];









  $sql = "UPDATE students SET

  contactno=:contactno,

  email=:email,

  gender=:gender,

  nextphone=:nextphone,

  country=:country,

  age=:age,

  district=:district,

  state=:state,

  village=:village,

  parentName=:parentName,

  relation=:relation,

  occupation=:occupation,

  language=:language,

  religion=:religion,

  nationality=:nationality,

  work_address=:work_address,

  hospital_name=:hospital_name,

  hospital_phone=:hospital_phone,

  doctor_address=:doctor_address,

  doctor_phone=:doctor_phone,

  medical_history=:medical_history,

  physical_peculiarity=:physical_peculiarity

WHERE id=:eid";



  $query = $dbh->prepare($sql);

  $query->bindParam(':email', $email, PDO::PARAM_STR);

  $query->bindParam(':contactno', $contactno, PDO::PARAM_STR);

  $query->bindParam(':eid', $eid, PDO::PARAM_STR);

  $query->bindParam(':gender', $gender, PDO::PARAM_STR);

  $query->bindParam(':nextphone', $nextphone, PDO::PARAM_STR);

  $query->bindParam(':country', $country, PDO::PARAM_STR);

  $query->bindParam(':age', $age, PDO::PARAM_STR);

  $query->bindParam(':district', $district, PDO::PARAM_STR);

  $query->bindParam(':state', $state, PDO::PARAM_STR);

  $query->bindParam(':village', $village, PDO::PARAM_STR);

  $query->bindParam(':parentName', $parentName, PDO::PARAM_STR);

  $query->bindParam(':relation', $relation, PDO::PARAM_STR);

  $query->bindParam(':occupation', $occupation, PDO::PARAM_STR);

  $query->bindParam(':language', $language, PDO::PARAM_STR);

  $query->bindParam(':religion', $religion, PDO::PARAM_STR);

  $query->bindParam(':nationality', $nationality, PDO::PARAM_STR);

  $query->bindParam(':work_address', $work_address, PDO::PARAM_STR);

  $query->bindParam(':hospital_name', $hospital_name, PDO::PARAM_STR);

  $query->bindParam(':hospital_phone', $hospital_phone, PDO::PARAM_STR);

  $query->bindParam(':doctor_address', $doctor_address, PDO::PARAM_STR);

  $query->bindParam(':doctor_phone', $doctor_phone, PDO::PARAM_STR);

  $query->bindParam(':medical_history', $medical_history, PDO::PARAM_STR);

  $query->bindParam(':physical_peculiarity', $physical_peculiarity, PDO::PARAM_STR);



  $query->execute();

  flash_alert('updated successfully');

  header("Location: stdprofileoldboard.php?tab_id=$tabSessionID");
  exit;

}

?>



<?php @include("includes/head.php"); ?>



<body class="hold-transition sidebar-mini layout-fixed">

  <div class="wrapper">

    <!-- Navbar -->

    <?php @include("includes/headerstd.php"); ?>

    <!-- /.navbar -->

    <!-- Side bar and Menu -->

    <?php @include("includes/sidebaroldstdboard.php"); ?>

    <!-- /.sidebar and menu -->



    <!-- Content Wrapper. Contains page content -->

    <div class="content-wrapper">

      <section class="content-header premium-header">

        <div class="container-fluid">

          <div class="header-glass-card">

            <div class="row align-items-center">

              <div class="col-md-8">

                <div class="title-section">

                  <div class="icon-box bg-primary-gradient shadow-sm">

                    <i class="fas fa-user-graduate"></i>

                  </div>

                  <div>

                    <h1 class="premium-title text-primary">

                      Student Information Management

                      <span class="premium-subtitle text-secondary">Manage Profile</span>

                    </h1>
                    <div class="mt-1">
                      <?php if ($currentSessionName): ?>
                        <span class="badge badge-primary mr-1">
                          <i class="fas fa-calendar-alt"></i>
                          <?= htmlspecialchars($currentSessionName) ?>
                        </span>
                      <?php endif; ?>
                      <?php if ($currentTermName): ?>
                        <span class="badge badge-success">
                          <i class="fas fa-bookmark"></i>
                          <?= htmlspecialchars($currentTermName) ?>
                        </span>
                      <?php else: ?>
                        <span class="badge badge-warning">
                          <i class="fas fa-exclamation-triangle"></i> No active term
                        </span>
                      <?php endif; ?>
                    </div>

                  </div>

                </div>

              </div>

              <div class="col-md-4">

                <nav aria-label="breadcrumb">

                  <ol class="breadcrumb premium-breadcrumb">

                    <li class="breadcrumb-item"><a href="oldstdboard.php" class="text-primary">Home</a></li>

                    <li class="breadcrumb-item active" aria-current="page">Update Profile</li>

                  </ol>

                </nav>

              </div>

            </div>

          </div>

        </div>

      </section>

      <div class="col-lg-12">

        <div class="card card-primary mb-4">

          <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">

            <h5 class="m-0 font-weight-bold"> <i class="fas fa-user-edit"></i> Update Student Profile</h5>

          </div>

          <div class="card-body">

            <form method="post">

              <?php

              $eid = $_SESSION['student_id'];

              $sql = "SELECT * from students   where id=:eid ";

              $query = $dbh->prepare($sql);

              $query->bindParam(':eid', $eid, PDO::PARAM_STR);

              $query->execute();

              $results = $query->fetchAll(PDO::FETCH_OBJ);



              $cnt = 1;

              if ($query->rowCount() > 0) {

                foreach ($results as $row) {

                  ?>

                  <div class="container rounded bg-white mt-5">

                    <div class="row">

                      <div class="col-md-4 border-right">

                        <div class="d-flex flex-column align-items-center text-center p-3 py-5">

                          <?php

                          if ($row->studentImage == "avatar15.jpg") { ?>

                            <img class="rounded-circle mt-5" src="studentimages/avatar15.jpg" width="90">

                            <?php

                          } else { ?>

                            <img class="rounded-circle mt-5" src="studentimages/<?php echo $row->studentImage; ?>" width="90">

                            <?php

                          } ?><span class="font-weight-bold"><?php echo $row->studentName; ?> </span><span
                            class="text-black-50"><?php echo $row->studentno; ?></span><span><?php echo $row->mobilenumber; ?></span>

                          <div class="mt-3">

                            <a href="update_stdimageoldboard.php?id=<?php echo $id; ?>">Edit Image</a>

                          </div>

                        </div>

                      </div>

                      <div class="col-md-8">

                        <div class="p-3 py-5">

                          <div class="d-flex justify-content-between align-items-center mb-3">

                            <div class="d-flex flex-row align-items-center back"><i
                                class="fa fa-long-arrow-left mr-1 mb-1"></i>

                            </div>

                            <h6 class="text-right">Edit Profile</h6>

                          </div>

                          <hr>

                          <h4>Student Details</h4>

                          <hr>

                          <div class="row mt-2">

                            <div class="col-md-6"><label for="name">Student Name</label><input type="text"
                                class="form-control" name="fullname" value="<?php echo $row->studentName; ?>"
                                required='true' readonly="true"></div>

                            <div class="col-md-6"><label for="name">Admission No.</label><input type="text"
                                class="form-control" name="adno" value="<?php echo $row->studentno; ?>" required
                                readonly="true"></div>



                          </div>



                          <div class="row mt-3">

                            <div class="col-md-6">



                              <label for="name">Class</label><input type="text" class="form-control" name="class"
                                value="<?php echo $row->class; ?>" required readonly="true">

                            </div>

                            <div class="col-md-3">



                              <label for="name">Arm </label><input type="text" class="form-control" name="section"
                                value="<?php echo $row->section; ?>" readonly="true">

                            </div>

                            <div class="col-md-3">



                              <label for="name">School Type</label><input type="text" class="form-control" name="stream"
                                value="<?php echo $row->stream; ?>" readonly="true">

                            </div>

                          </div>





                          <div class="row mt-3">

                            <div class="col-md-6"><label for="name">Email</label><input type="email" class="form-control"
                                value="<?php echo $row->email; ?>" name="email" placeholder="Email only." required></div>



                            <div class="col-md-6"><label for="name">Phone No.</label><input type="text" class="form-control"
                                value="<?php echo $row->contactno; ?>" name="phone" placeholder="Phone No." required></div>

                          </div>



                          <div class="row mt-2">

                            <!-- Gender -->

                            <div class="col-md-6">

                              <label for="gender">Gender</label>

                              <select id="gender" name="gender" class="form-control select2" required>

                                <option value="">Select Gender</option>

                                <option value="Male" <?php if ($row->gender == 'Male')
                                  echo 'selected'; ?>>Male</option>

                                <option value="Female" <?php if ($row->gender == 'Female')
                                  echo 'selected'; ?>>Female</option>

                              </select>

                            </div>

                            <div class="col-md-6"><label for="name">Date of Birth</label><input type="date"
                                class="form-control" value="<?php echo $row->age; ?>" name="age" placeholder="YYYY-MM-DD"
                                required></div>

                          </div>

                          <hr>

                          <h4>Guardian's Details</h4>

                          <hr>

                          <div class="row mt-2">

                            <div class="col-md-6"><label for="name">Guardian Name</label><input type="text"
                                class="form-control" name="parentName" value="<?php echo $row->parentName; ?>" required>
                            </div>

                            <div class="col-md-6"><label for="name">Relationship to student</label><input type="text"
                                class="form-control" value="<?php echo $row->relation; ?>" name="relationship to student"
                                placeholder="Relation" required></div>

                          </div>

                          <div class="row mt-2">

                            <div class="col-md-6"><label for="name">Occupation</label><input type="text"
                                class="form-control" name="occupation" value="<?php echo $row->occupation; ?>"
                                placeholder="Parent Occupation" required></div>

                            <div class="col-md-6"><label for="name">Phone No.</label><input type="text" class="form-control"
                                value="<?php echo $row->nextphone; ?>" name="nextphone" placeholder="Parent Phone No."
                                required></div>

                          </div>



                          <hr>

                          <h4>Address</h4>

                          <hr>



                          <div class="row mt-2">

                            <div class="col-md-6">

                              <label for="home_address">Home Address</label>

                              <input type="text" class="form-control" name="country"
                                value="<?php echo htmlspecialchars($row->country); ?>" required placeholder="Home Address">

                            </div>



                            <div class="col-md-6">

                              <label for="state">State of Origin</label>

                              <select id="state" class="form-control" name="state" required>

                                <option value="">-- Select State --</option>

                              </select>

                            </div>

                          </div>



                          <div class="row mt-2">

                            <div class="col-md-6">

                              <label for="city">City</label>

                              <input type="text" class="form-control" name="district"
                                value="<?php echo htmlspecialchars($row->district); ?>" placeholder="City">

                            </div>



                            <div class="col-md-6">

                              <label for="village">Local Government (LGA) of Origin</label>

                              <select id="village" class="form-control" name="village" required>

                                <option value="">-- Select LGA --</option>

                              </select>

                            </div>

                          </div>



                          <hr>

                          <h4>Medical & Other Details</h4>

                          <hr>

                          <div class="row mt-2">

                            <div class="col-md-6">

                              <label for="language">Languages</label>

                              <input type="text" class="form-control" name="language" value="<?php echo $row->language; ?>"
                                placeholder="E.g., English, Yoruba">

                            </div>



                            <div class="col-md-6">

                              <label for="religion">Religion</label>

                              <input type="text" class="form-control" name="religion" value="<?php echo $row->religion; ?>">

                            </div>

                          </div>



                          <div class="row mt-2">

                            <div class="col-md-6">

                              <label for="nationality">Nationality</label>

                              <input type="text" class="form-control" name="nationality"
                                value="<?php echo $row->nationality; ?>">

                            </div>



                            <div class="col-md-6">

                              <label for="work_address">Parent's Work Address</label>

                              <input type="text" class="form-control" name="work_address"
                                value="<?php echo $row->work_address; ?>">

                            </div>

                          </div>



                          <div class="row mt-2">

                            <div class="col-md-6">

                              <label for="hospital_name">Hospital Name</label>

                              <input type="text" class="form-control" name="hospital_name"
                                value="<?php echo $row->hospital_name; ?>">

                            </div>



                            <div class="col-md-6">

                              <label for="hospital_phone">Hospital Phone</label>

                              <input type="text" class="form-control" name="hospital_phone"
                                value="<?php echo $row->hospital_phone; ?>">

                            </div>

                          </div>



                          <div class="row mt-2">

                            <div class="col-md-6">

                              <label for="doctor_address">Doctor's Address</label>

                              <input type="text" class="form-control" name="doctor_address"
                                value="<?php echo $row->doctor_address; ?>">

                            </div>



                            <div class="col-md-6">

                              <label for="doctor_phone">Doctor's Phone</label>

                              <input type="text" class="form-control" name="doctor_phone"
                                value="<?php echo $row->doctor_phone; ?>">

                            </div>

                          </div>



                          <div class="row mt-2">

                            <div class="col-md-6">

                              <label for="medical_history">Medical History</label>

                              <textarea name="medical_history"
                                class="form-control"><?php echo $row->medical_history; ?></textarea>

                            </div>



                            <div class="col-md-6">

                              <label for="physical_peculiarity">Physical Peculiarity</label>

                              <textarea name="physical_peculiarity"
                                class="form-control"><?php echo $row->physical_peculiarity; ?></textarea>

                            </div>

                          </div>









                          <div class="mt-5 text-center"><button class="btn btn-primary profile-button" type="submit"
                              name="submit"> <i class="fas fa-sync"></i> Update Profile</button></div>

                        </div>

                      </div>

                    </div>

                  </div>

                  <?php

                }

              } ?>

            </form>

          </div>

        </div>

      </div>



      <!-- /.content-header -->

    </div>

    <!-- /.content-wrapper -->

    <?php @include("includes/foot.php"); ?>

    <?php @include("includes/footer.php"); ?>

    <script>

      $(document).ready(function () {

        const preselectedState = "<?php echo addslashes($row->state ?? ''); ?>";

        const preselectedLga = "<?php echo addslashes($row->village ?? ''); ?>";



        // Initialize Select2

        $('#state, #village, #gender').select2();



        // Load states

        $.getJSON('get_states.php', function (states) {

          let stateOptions = `<option value="">-- Select State --</option>`;

          $.each(states, function (i, state) {

            const selected = (state.name === preselectedState) ? 'selected' : '';

            stateOptions += `<option value="${state.name}" ${selected}>${state.name}</option>`;

          });

          $('#state').html(stateOptions).trigger('change'); // Trigger change to load LGAs

        });



        // When state changes, load LGAs

        $('#state').on('change', function () {

          const selectedState = $(this).val();

          if (!selectedState) {

            $('#village').html('<option value="">-- Loading........ --</option>').trigger('change');

            return;

          }



          $.getJSON(`get_local_governments.php?state_id=${encodeURIComponent(selectedState)}`, function (lgas) {

            let lgaOptions = `<option value="">-- Select LGA --</option>`;

            $.each(lgas, function (i, lga) {

              const selected = (lga.name === preselectedLga) ? 'selected' : '';

              lgaOptions += `<option value="${lga.name}" ${selected}>${lga.name}</option>`;

            });

            $('#village').html(lgaOptions).trigger('change'); // Must trigger change

          });

        });

      });

    </script>



</body>



</html>