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

<!-- This page unique styles -->
<style>
/*Core setup styles*/
.sideNav{
    height: 100%;
    /*width: 220px;*/
    position: fixed;
    z-index: 9;
}
.navbar{
    top: 0;
    position: fixed;
    z-index: 9999;
}
.the-links{
    padding-top: 10px;
}
/*.nav-links{
    height: 100%;
    overflow-y: auto;
}*/
.insider{
    margin-top: 10px;
    position: relative;
}
.card{
    height: auto;
}
.cat-pag{
    position: relative;
}
.cat-page-header{
    position: sticky;
    width: 100%;
    height: 50px;
    background:#F7F9FB; 
    top: 60px;
    right: 60px;
    z-index: 1;
}
@media(max-width: 992px){
.insider{
    margin-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
    }        
}
.card-header h5{
    font-size: 14px;
}

@media(max-width: 992px){
    .negative h5{
        font-size: 15px;
    }
    .negative a{
        font-size: 14px;
    }
    .negative a i{
        font-size: 14px;
    }
}
.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

$emailAdd = $username;
?>

<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>Search for Student</h5>
          <div>
              
            <button name="search-btn" class="btn btn-sm info text-white">
              <i class="fa fa-search"></i> Search
            </button>
          </div>
        </div>

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

              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Student Name</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                    <!-- <label>Enter to search</label> -->
                    <input type="text" 
                    autofocus="" 
                    required="" 
                    id="searchInput"
                    autocomplete="off" 
                    name="studId" 
                    placeholder="Student ID or Names"
                    onkeyup="searchStudent(this.value)">
                    </div>
                    <div id="suggestionsBox" class="suggestions"></div>
                  </div>
                </div>
              </div>

              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Term</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <select name="term" required="">
                        <option value="" hidden="">Select Term</option>
                        <option value="First">First</option>
                        <option value="Second">Second</option>
                        <option value="Third">Third</option>
                        <option value="<?=$current_term?>">Current Term</option>
                      </select>                          
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Session</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <select name="session" required="">
                        <option value="" hidden="">Select Session</option>
                        <?php
                        $sessStmt = "SELECT session_year FROM sessions order by sessionId DESC ";
                        $sessPdo = $pdo->prepare($sessStmt);
                        $sessPdo->execute();
                        while ($sessRow = $sessPdo->fetch(PDO::FETCH_ASSOC)) {
                          $sessData = $sessRow['session_year'];?>
                          <option value="<?=$sessData?>"><?=$sessData?></option>
                        <?php } ?>
                        <option value="<?=$current_session?>">Current Session</option>
                      </select>                         
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-lg-6 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="" hidden="">Select class</option>
                        <?php
                        $claStmt = "SELECT DISTINCT(class) AS class FROM teacher_assignments 
                              WHERE staffId = ?";
                        $clasPdo = $pdo->prepare($claStd);
                        $clasPdo->execute([$staffId]);
                        while ($clasRow = $clasPdo->fetch(PDO::FETCH_ASSOC)) {
                          $disClass = $clasRow['class'];?>
                          <option value="<?=$disClass?>"><?=$disClass?></option>
                        <?php }?>
                      </select>                           
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Subject</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <select name="subject" required="">
                        <option value="" hidden="">Select Subject</option>
                        <?php
                        $subStmt = "SELECT DISTINCT(subject) FROM teacher_assignments
                                WHERE staffId = ?";
                        $subPdo = $pdo->prepare($subStmt);
                        $subPdo->execute([$staffId]);
                        while ($subRow = $subPdo->fetch(PDO::FETCH_ASSOC)) {
                          $dataSub = $subRow['subject'];?>
                          <option value="<?=$dataSub?>"><?=$dataSub?></option>
                        <?php } ?>
                      </select>                           
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Record Type</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <select name="type" required="">
                        <option value="" hidden="">Select Type</option>
                        <option value="test">First CA Test</option>
                        <option value="test2">Second CA Test</option>
                        <option value="exam">Internal Exam</option>
                      </select>                          
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>

    </div>

  </div>
</div>

<?php
if (isset($_POST['search-btn'])) {
  $adminNo        = $_POST['studId'];
  $selectedClass  = $_POST['class'];
  $selectedTerm   = $_POST['term'];
  $selectedSession = $_POST['session'];
  $selectedSub    = $_POST['subject'];
  $selectedType   = $_POST['type'];

  $_SESSION['adminNo'] = $adminNo;
  $_SESSION['selectedClass'] = $selectedClass;
  $_SESSION['selectedTerm'] = $selectedTerm;
  $_SESSION['selectedSession'] = $selectedSession;
  $_SESSION['selectedSub'] = $selectedSub;
  if ($selectedType == 'test') {
    header('Location:mid-test.php');
  }elseif ($selectedType == 'test2') {
    header('Location:mid-test2.php');
  }
  else{
    header('Location:internal.php');
  }

}
?>

<style>
.suggestions {
  /*position: absolute;*/
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #ddd;
  border-radius: 5px;
  z-index: 9999;
  max-height: 200px;
  overflow-y: auto;
  display: none;
}
.suggestion-item {
  padding: 10px;
  cursor: pointer;
}
.suggestion-item:hover {
  background: #f5f5f5;
}
</style>
<?php include 'includes/footer.php'; ?>
<script>
function searchStudent(query) {
  let box = document.getElementById("suggestionsBox");

  if (query.length < 2) {
    box.style.display = "none";
    return;
  }

  let xhr = new XMLHttpRequest();
  xhr.open("POST", "searchStudent.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onload = function () {
    if (this.status == 200) {
      box.innerHTML = this.responseText;
      box.style.display = "block";
    }
  };
  xhr.send("query=" + encodeURIComponent(query));
}

function selectStudent(value) {
  document.getElementById("searchInput").value = value;
  document.getElementById("suggestionsBox").style.display = "none";
}
</script>

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

