<?php session_start();?>
<?php include'includes/header.php'; ?>
<?php include'includes/sidebar.php';?>
<?php include'includes/functions.php';?>
<?php include'includes/navbar.php'; ?>

<!-- Handle Add Slider -->
<style>
@media(max-width: 992px){
  .insider{
      margin-top: -50px;
  }
  .card{
    height: 500px;
  }
  .negative{
    height: 500px;
  } 
  .card-header h5{
    font-size: 12px;
  }
}

.card{
    height: 500px;
}
.card-header h5{
  font-size: 13px;
  font-weight: bold;
}
.custom-fieldset-box{
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 5px;
  position: relative;
  margin-bottom: 20px;
  background: #F7F9FB;
}
.custom-field-title{
  position: absolute;
  top: -12px;
  left: 15px;
  background: #F7F9FB;
  padding: 0 10px;
  font-weight: bold;
  color: #1E5F67;
  font-size: 15px;
}

input{
  width: 100%;
  height: 50px;
  border:2px solid #ccc;
  outline: none;
  border-radius: 5px;
  padding-left: 10px;
  padding-right: 10px;
}
select{
  width: 100%;
  height: 50px;
  border:2px solid #ccc;
  outline: none;
  border-radius: 5px;
  padding-left: 10px;
  padding-right: 10px;
}
.avater{
    width: 60px;
    height: 60px;
    border:2px solid #ccc;
    border-radius: 50%;
}
</style>
<?php

if (isset($_GET['stdId'])) {
    $adminNo = $_GET['stdId'];

    // get information using the stdId
    $sql = "SELECT * FROM student WHERE adminNo = ?";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$adminNo]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    $surname = $row['surname'];
    $othernames = $row['otherNames'];
    $gender = $row['gender'];
    $password = $row['password'];
    $class = $row['class'];

     // Get image and birth info
     $imgSql = "SELECT * FROM student_image WHERE adminNo = ?";
     $imgstmt = $pdo->prepare($imgSql);
     $imgstmt->execute([$adminNo]);

     $imrow = $imgstmt->fetch(PDO::FETCH_ASSOC);
     $image = $imrow['photo_path'];
     $dob   = $imrow['dob']; 
}


?>

<!-- Main page starts here -->
<div id="notification-containers" class="notification-containers"></div>
<div class="insider">
  <div class="container">
    <div class="cat-pag">
      <form action="" method="POST" enctype="multipart/form-data">
        <div class="cat-page-header d-flex justify-content-between align-items-center">
          <h5></h5>
          <div>
                          
            <button name="save-btn" 
              class="btn btn-sm 
              info text-white">
              Update Student 
              <i class="fa fa-user-plus"></i>
            </button>

          </div>
        </div>

        <div class="card mt-3">
          <div class="card-header" style="display: flex; align-items: center; justify-content: space-between;">
            <h5>ADD NEW STUDENT</h5>
          </div>
          <div class="card-body" style="background: #F7F9FB;">
            <div class="row" style="margin-top: -10px;">

              <!-- Surname -->
              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Surname</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="text"
                      value="<?=$surname?>" 
                      name="surname" 
                      placeholder="Student Surname" 
                      required="" 
                      class="form-control">
                    </div>
                  </div>
                </div>
              </div>

              <!-- Othernames -->
              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Other Names</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="text" 
                      value="<?=$othernames?>" 
                      name="othername" 
                      placeholder="Second & Last Names"
                      required="" 
                      class="form-control">               
                    </div>
                  </div>
                </div>
              </div>

              <!-- Date of birth -->
              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Date of Birth</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="date"
                      value="<?=$dob?>" 
                      name="dob"
                      required="" 
                      class="form-control">                  
                    </div>
                  </div>
                </div>
              </div>

              <!-- Gender -->
              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Gender</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <select name="gender">
                        <option value="<?=$gender?>"><?=$gender?></option>
                        <option value="M">Male</option>
                        <option value="F">Female</option>
                      </select>                          
                    </div>
                  </div>
                </div>
              </div>

              <!-- Image -->
              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Image</span>
                  <div class="inner-cont">
                    <div class="avatar">
                        <input type="hidden" name="feature_image">
                        <div class="uploads" style="margin-top: -5px;">
                          <img alt="" src="../../student/images/avatar/<?=$image?>" id = "image" >
                          <div class="rightRound" id = "upload">
                            <input type="file" name="the_avatar" id = "fileImg" accept=".jpg, .jpeg, .png">
                            <i class = "fa fa-camera"></i>
                          </div>
                          <div class="leftRound" id = "cancel" style = "display: none;">
                            <i class = "fa fa-times"></i>
                          </div>
                        </div>
                    </div>
                  </div>
                </div>
              </div>

              <!-- Class -->
              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Class</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                     <select name="class" required="">
                      <option value="<?=$class?>"><?=$class?></option>
                      <?php
                      $clss = "SELECT * FROM classes";
                      $stmtClass = $pdo->prepare($clss);
                      $stmtClass->execute([]);
                      while ($clList = $stmtClass->fetch(PDO::FETCH_ASSOC)) {
                        $className = $clList['className'];?>
                        <option value="<?=$className?>"><?=$className?></option>
                      <?php };
                      ?>
                     </select>                       
                    </div>
                  </div>
                </div>
              </div>

              <!-- New Password -->
              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">New Password</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="password"
                      value="<?=$password?>" 
                      name="new_password"
                      placeholder="Set New Password" 
                      required="" 
                      class="form-control">                          
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Confirm Password</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="password"
                      value="<?=$password?>" 
                      required=""
                      placeholder="Enter password again" 
                      name="confirm" 
                      class="form-control">                          
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>

      

    </div>
   
  </div>
</div>
<?php
if (isset($_POST['save-btn'])) {

  $surname        = $_POST['surname'];
  $othername      = $_POST['othername'];
  $date_of_birth  = $_POST['dob'];
  $gender         = $_POST['gender'];
  $class          = $_POST['class'];
  $new_password   = $_POST['new_password'];
  $confirm        = $_POST['confirm'];

  $the_avatar_image          = $_FILES['the_avatar']['name'];
  $the_avatar_img_temp       = $_FILES['the_avatar']['tmp_name'];
  move_uploaded_file($the_avatar_img_temp, "../../student/images/avatar/$the_avatar_image");

  if ($new_password != $confirm) {
    $_SESSION['message'] = 'Password does not match';
    $_SESSION['type'] = 'error';
  }
  else{
    if (empty($the_avatar_image)) {
      // Get studentImg
      $stdImgStmt  = "SELECT photo_path FROM student_image WHERE adminNo = ?";
      $stdImgPdo = $pdo->prepare($stdImgStmt);
      $stdImgPdo->execute([$adminNo]);
      $stdImgRow = $stdImgPdo->fetch(PDO::FETCH_ASSOC);
      $the_avatar_image = $stdImgRow['photo_path']; 
    }
    $valPass = password_hash($new_password, PASSWORD_DEFAULT);
    $updateSql = "UPDATE student SET surname = ?, otherNames = ?, gender =?,
                  password = ?, class = ? WHERE adminNo = ?";
    $updStmt = $pdo->prepare($updateSql);
    $updStmt->execute([$surname, $othername, $gender, $password, $class, $adminNo]);
    

    // Then update student Image
    $updImg = "UPDATE student_image SET photo_path = ?, dob = ? WHERE adminNo = ?";
    $upIStmt = $pdo->prepare($updImg);
    $upIStmt->execute([$the_avatar_image, $date_of_birth, $adminNo]);
    
    $_SESSION['message'] = 'Student Updated Successfully';
    $_SESSION['type'] = 'success';?>

    <script type="text/javascript">
      setTimeout("location.href='students.php';", 2000);
    </script>
    <?php

      
  }

}

?>


<?php include 'includes/footer.php'; ?>


<script>
    $(document).ready(function () {
        $('#example').DataTable();
        $('#SelectAllBoxes').click(function () {
            $('.checkBoxes').prop('checked', this.checked);
        });
    });

    function openModal(id) {
        document.getElementById(id).style.display = "block";
    }

    function closeModal(id) {
        document.getElementById(id).style.display = "none";
    }

    function handleDelete() {
        const checked = document.querySelectorAll("input[name='checkBoxArray[]']:checked");
        if (checked.length === 0) {
            alert("No user selected!");
            return;
        }
        openModal('modal-delete');
    }
   
</script>

<script type="text/javascript">
    document.getElementById("fileImg").onchange = function(){
      document.getElementById("image").src = URL.createObjectURL(fileImg.files[0]); // Preview new image
      document.getElementById("cancel").style.display = "block";
      document.getElementById("uploads").style.display = "none";
    }
    var userImage = document.getElementById('image').src;
    document.getElementById("cancel").onclick = function(){
      document.getElementById("image").src = userImage; // Back to previous image
      document.getElementById("cancel").style.display = "none";
      document.getElementById("uploads").style.display = "block";
    }      
</script>


<?php include'includes/notification-alert-js.php';?>

