<?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>

<!-- 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">
        <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">
              Add 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" 
                      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" 
                      name="othername" 
                      placeholder="Second & Last Names"
                      required="" 
                      class="form-control">               
                    </div>
                  </div>
                </div>
              </div>

              <!-- Date of birth -->
              <div class="col-lg-6 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" 
                      name="dob"
                      required="" 
                      class="form-control">                  
                    </div>
                  </div>
                </div>
              </div>

              <!-- Gender -->
              <div class="col-lg-6 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="M">Male</option>
                        <option value="F">Female</option>
                      </select>                          
                    </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="<?=$className?>"><?=$className?></option>
                     </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" 
                      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"
                      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'];

  if ($new_password != $confirm) {
    $_SESSION['message'] = 'Password does not match';
    $_SESSION['type'] = 'error';
  }else{
    $valPass = password_hash($new_password, PASSWORD_DEFAULT);
    $save_student = "INSERT INTO student 
    (surname, otherNames,
    gender, password, class, term, session)
    VALUES(?,?,?,?,?,?,?)";
    $stmt = $pdo->prepare($save_student);
    $stmt->execute([
      $surname, $othername, 
      $gender, $valPass, 
      $class, $current_term, 
      $current_session]);
    $lastId = $pdo->lastInsertId();

    // Then insert into student Image
    $inserts = "INSERT INTO student_image(studentId, dob)VALUES(?,?)";
    $insertPdo = $pdo->prepare($inserts);
    $insertPdo->execute([$lastId, $date_of_birth]);

    $_SESSION['message'] = 'Student Added Successfully.';
    $_SESSION['type'] = 'success';?>

    <script type="text/javascript">
      setTimeout("location.href='my-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>


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

