<?php
session_start();
ob_start();
if(isset($_SESSION['username'])){
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">


  <?php include 'includes/styles.php'; ?>
    <title>View Student |  <?php echo $school_name; ?> </title>
    <?php
    $module_name = 'Student';
    include "check-admin-privilege.php";
    ?>

    <style>
        .fa-eye{
            background: #f06001;
            padding: 5px;
            border-radius: 2px;
            color: white;
            font-size: 11px;
        }
    </style>

</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

  <!-- Navbar -->
    <?php include 'includes/notifications.php'; ?>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
<?php include "includes/sidebar.php";?>

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

    if(isset($_GET['student'])){
      $student_username = $_GET['student'];
      //select the teachers username from the database

      $sql = "SELECT * FROM student WHERE username =:username";
      $conn->query($sql);
      $conn->bind(":username", $student_username);
      $result = $conn->fetchSingle();
      $db_username = $result->username;
      if($student_username == $db_username) {
          $student_name = $result->name;

          $classid = $result->classesID;
          $sectionid = $result->sectionid;

          $sql = "SELECT name FROM classes WHERE id =:id";
          $conn->query($sql);
          $conn->bind(":id", $classid);
          $className = $conn->fetchColumn();

          //fetch student section 

          $sql = "SELECT name FROM sections WHERE class =:id AND id =:sectionid";
          $conn->query($sql);
          $conn->bind(":id", $classid);
          $conn->bind(":sectionid", $sectionid);
          $sectionName = $conn->fetchColumn();


          ?>
          <div class="content-wrapper">
          <!-- Content Header (Page header) -->
          <div class="content-header">
              <div class="container-fluid">
                  <div class="row mb-2">
                      <div class="col-sm-6">
                          <h1 class="m-0">Viewing <?php echo $student_name; ?></h1>
                      </div><!-- /.col -->
                      <div class="col-sm-6">
                          <ol class="breadcrumb float-sm-right">
                              <li class="breadcrumb-item"><a href="<?php echo $conn->base_url();?>">Home</a></li>
                              <li class="breadcrumb-item"><a href="<?php echo $conn->base_url();?>student">Back to students page </a></li>
                              <li class="breadcrumb-item active"><?php echo $student_name; ?></li>
                          </ol>
                      </div><!-- /.col -->
                  </div><!-- /.row -->
              </div><!-- /.container-fluid -->
          </div>
          <!-- /.content-header -->

          <!-- Main content -->
          <section class="content">
          <div class="container-fluid">

          <div class="row">
          <div class="col-md-12">

          <!-- /.card-header -->
          <div class="card card-info card-tabs">
          <div class="card-header p-0 pt-1">
              <ul class="nav nav-tabs" id="custom-tabs-one-tab" role="tablist">
                  <li class="nav-item">
                      <a class="nav-link active" id="custom-tabs-one-home-tab" data-toggle="pill" href="#details"
                         role="tab" aria-controls="custom-tabs-one-home" aria-selected="false">Students Details</a>
                  </li>
                  <li class="nav-item">
                      <a class="nav-link" id="custom-tabs-one-profile-tab" data-toggle="pill" href="#profile" role="tab"
                         aria-controls="custom-tabs-one-profile" aria-selected="true">Profile picture</a>
                  </li>
                  <li class="nav-item">
                      <a class="nav-link" id="custom-tabs-one-messages-tab" data-toggle="pill" href="#result" role="tab"
                         aria-controls="custom-tabs-one-messages" aria-selected="false">Terminal Reports</a>
                  </li>
              </ul>
          </div>


          <div class="card-body">
          <div class="tab-content" id="custom-tabs-one-tabContent">
          <div class="tab-pane fade active show" id="details" role="tabpanel"
               aria-labelledby="custom-tabs-one-home-tab">
              <!-- form start -->
              <form class="form-horizontal" id="student-add" method="POST">
                  <div class="card-body">

                      <div class="form-group row">
                          <label for="name" class="col-sm-2 col-form-label"> Name <span
                                      style="color: #f00;">*</span></label>
                          <div class="col-sm-4">
                              <input type="text" class="form-control" id="name" required="true" name="name"
                                     value="<?php echo $student_name; ?>">
                          </div>

                          <label for="dob" class="col-sm-2 col-form-label">date of birth</label>
                          <div class="col-sm-4">
                              <input type="date" class="form-control" id="dob" name="dob"
                                     value="<?php echo $result->dob; ?>">
                          </div>

                      </div>

                      <div class="form-group row">
                          <label for="db_group" class="col-sm-2 col-form-label">Sex <span style="color: #f00;">*</span></label>
                          <div class="col-sm-4">
                              <select name="sex" id="b_group" class="form-control" required="true">
                                  <option value="<?php echo $result->sex; ?>"
                                          selected><?php echo $result->sex; ?></option>
                              </select>
                          </div>
                          <label for="religion" class="col-sm-2 col-form-label">Religion</label>
                          <div class="col-sm-4">
                              <input type="text" class="form-control" id="religion" name="religion"
                                     value="<?php echo $result->religion; ?>">
                          </div>
                      </div>


                      <div class="form-group row">
                          <label for="email" class="col-sm-2 col-form-label">Email</label>
                          <div class="col-sm-4">
                              <input type="email" class="form-control" id="email" name="email" data-parsley-type="email"
                                     data-parsley-trigger="keyup" value="<?php echo $result->email; ?>">
                          </div>

                          <label for="phone" class="col-sm-2 col-form-label">Phone</label>
                          <div class="col-sm-4">
                              <input type="tel" class="form-control" id="phone" name="phone" data-parsley-type='number'
                                     maxlength="11" data-parsley-length="[11,11]" data-parsley-trigger="keyup"
                                     pattern="\d{11}" value="<?php echo $result->phone; ?>">
                          </div>
                      </div>


                      <div class="form-group row">
                          <label for="address" class="col-sm-2 col-form-label">Address</label>
                          <div class="col-sm-4">
                              <input type="text" class="form-control" id="address" name="address"
                                     value="<?php echo $result->address; ?>">
                          </div>

                          <label for="state" class="col-sm-2 col-form-label">State</label>
                          <div class="col-sm-4">
                              <input type="text" class="form-control" id="state" name="state"
                                     value="<?php echo $result->state; ?>">
                          </div>
                      </div>


                      <div class="form-group row">
                          <label for="db_group" class="col-sm-2 col-form-label">Class <span
                                      style="color: #f00;">*</span></label>
                          <div class="col-sm-4">
                              <select name="class" id="classID" class="form-control" required="true">
                                  <option value="<?php echo $classid; ?>" selected><?php echo $className; ?></option>
                              </select>
                          </div>

                          <label for="db_group" class="col-sm-2 col-form-label">Section <span
                                      style="color: #f00;">*</span></label>
                          <div class="col-sm-4">
                              <select name="section" id="section" class="form-control" required="true">
                                  <option value="<?php echo $sectionid; ?>"
                                          selected><?php echo $sectionName; ?> </option>
                              </select>
                          </div>
                      </div>


                      <div class="form-group row">
                          <label for="state" class="col-sm-2 col-form-label">Country</label>
                          <div class="col-sm-4">
                              <input type="text" class="form-control" id="country" name="country"
                                     value="<?php echo $result->country; ?>">
                          </div>

                          <label for="reg_no" class="col-sm-2 col-form-label">Registration No<span style="color: #f00;">*</span></label>
                          <div class="col-sm-4">
                              <input type="text" class="form-control" id="reg_no" required name="reg_no"
                                     value="<?php echo $result->registerNO; ?>">
                          </div>
                      </div>

                      <div class="form-group row">
                          <label for="username" class="col-sm-2 col-form-label">Username <span
                                      style="color: #f00;">*</span></label>
                          <div class="col-sm-4">
                              <input type="text" class="form-control" id="username" required name="username"
                                     value="<?php echo $result->username; ?>">
                          </div>

                      </div>


                  </div>
                  <div id="success-msg"></div>
                  <!-- /.card-body -->

                  <!-- /.card-footer -->
              </form>

              <!-- form end -->
          </div>
          <!--   student photo start -->
          <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="custom-tabs-one-profile-tab">
              <div>
                  <?php
                  if (empty($result->photo)) {
                      ?>   <p style="text-align: left;"><img src="<?php echo $base_url; ?>images/default.png"
                                                             alt="No image set"></p><?php

                  } else {
                      ?>  <p style="text-align: left;"><img
                              src="<?php echo $base_url; ?>upload/<?php echo $result->photo; ?>"
                              alt="<?php $result->photo; ?>" width='300' height='300' style='border-radius: 50%;'>
                      </p><?php
                  }
                  ?>
              </div>

          </div>

          <!--   student photo end -->


          <!--   student result start -->

              <div class="tab-pane fade" id="result" role="tabpanel" aria-labelledby="custom-tabs-one-messages-tab">
                  <?php
                  $username = $result->username;

                  //fetch specific terms
                  $sql = "SELECT DISTINCT(current_school_session) FROM results WHERE name_of_student =:name AND results.current_school_session !=''";
                  $conn->query($sql);
                  $conn->bind(":name", $student_name);
                  $available_sessions = $conn->fetchMultiple();
                  $rowcount = $conn->rowCount();
                  $i = 1;

                  echo "<p style='margin-left:20px; margin-bottom: -13px;'>you have $rowcount available session(s) for
            <span style='color: #f06001; text-transform: capitalize;'>
            $username</span></p>";
                  ?>
                  <div class="card-body">
                      <table id="teacher" class="table table-bordered table-striped">
                          <thead>
                          <tr>
                              <th>#</th>
                              <th>Term</th>
                              <th>Academic Session</th>
                              <th>Action</th>
                          </tr>
                          </thead>
                          <tbody>
                          <?php
                          if($rowcount){
                              foreach ($available_sessions as $session) {
                                  $db_session = $session->current_school_session;

                                  // fetch all terms for the current session
                                  $sql = "SELECT DISTINCT (exam) FROM results WHERE current_school_session =:current_school_session AND name_of_student =:name";
                                  $conn->query($sql);
                                  $conn->bind(":current_school_session", $db_session);
                                  $conn->bind(":name", $student_name);
                                  $available_exams = $conn->fetchMultiple();

                                  foreach ($available_exams as $exam) {
                                      $exam_name = $exam->exam;
                                      //fetch distinct class
                                      $sql = "SELECT DISTINCT(class) FROM results WHERE current_school_session =:current_school_session AND exam =:exam AND name_of_student =:name";
                                      $conn->query($sql);
                                      $conn->bind(":current_school_session", $db_session);
                                      $conn->bind(":exam", $exam_name);
                                      $conn->bind(":name", $student_name);
                                      $available_classes = $conn->fetchColumn();

                                      //fetch distinct section
                                      $sql = "SELECT DISTINCT(section) FROM results WHERE current_school_session =:current_school_session AND exam =:exam AND name_of_student =:name";
                                      $conn->query($sql);
                                      $conn->bind(":current_school_session", $db_session);
                                      $conn->bind(":exam", $exam_name);
                                      $conn->bind(":name", $student_name);
                                      $available_sections = $conn->fetchColumn();

                                      // display the table
                                      $slugify_name = $conn->slugify($student_name);
                                      $slugify_session = $conn->slugify($db_session);
                                      $slugify_class = $conn->slugify($available_classes);
                                      $slugify_section = $conn->slugify($available_sections);
                                      $slugify_exam = $conn->slugify($exam_name);
                                      $result_link = $base_url."generate_report/$slugify_name/$slugify_exam/$slugify_section/$slugify_class/$slugify_session";
                                      ?>
                                      <tr>
                                          <td><?php echo $i++; ?></td>
                                          <td><?php echo $exam_name;?></td>
                                          <td><?php echo $db_session;?></td>
                                          <td>
                                              <a href="<?php echo $result_link; ?>"  target="_blank" data-toggle="tooltip" title="View Terminal Report">
                                                  <i class="fa fa-eye"></i>
                                              </a>&nbsp;
                                          </td>
                                      </tr>
                                      <?php
                                  }
                              }
                          } else {
                              ?> <tr>
                                  <td colspan="4" class="text-center">No Data in table</td>
                              </tr><?php
                          }
                          ?>
                          </tbody>
                          <tfoot>
                          <tr>
                              <th>#</th>
                              <th>Term</th>
                              <th>Academic Session</th>
                              <th>Action</th>
                          </tr>
                          </tfoot>
                      </table>
                  </div>
              </div>
              <!-- student result end -->

                </div>
              </div>
              <!-- /.card -->
            </div>
             
     
    <!-- /.card -->

          </div>
        
 
        </div>


        <!-- /.row (main row) -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
<?php }else{
     //redirect back to the teachers page
    header("Location: ../student");
}

}else{
    //redirect back to the teachers page
    header("Location: student");

  }
 ?>


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

    <?php
    if(isset($_POST['change_password'])) {
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];
        $username = $_POST['username'];
        //check for password length
        if (!$conn->Checkpassword($password)) {
            echo "<script>
              toastr['error'](' Password must include one uppercase letter, one lowercase letter, one number,  one special character such as $ or % and length should be between 6 and 16.');
        </script>";
            return false;
        }
        //check if both passwords matches

        if ($password != $confirm_password) {
            echo "<script>
              toastr['error']('Your passwords do not match');
        </script>";
            return false;
        }else {
                //all checks passed, update password
                $sql = "UPDATE student SET password =:password WHERE username =:username";
                $conn->query($sql);
                $conn->bind(":password", $conn->Password_Encryption($password));
                $conn->bind(":username", $username);

                if ($conn->execute()) {
                    echo "<script>
              toastr['success']('$username password was updated successfully. Redirecting to all students page.');
        </script>";

                } else {
                    echo "<script>
              toastr['error']('An error occurred while updating password.');
        </script>";

                }


            }
        }
    ?>

    <script>
        $(function () {
            $("#teacher").DataTable({
                "responsive": true, "lengthChange": false, "autoWidth": false,
                "buttons": ["copy", "csv", "pdf", "print"]
            }).buttons().container().appendTo('#teacher_wrapper .col-md-6:eq(0)');
            $('#example2').DataTable({
                "paging": true,
                "lengthChange": false,
                "searching": true,
                "ordering": true,
                "info": true,
                "autoWidth": false,
                "responsive": true
            });
        });
    </script>

    <script>
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();
        });
    </script>

    <?php
  }else{
      ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">
        <title>Login to continue | <?php echo $school_name; ?> </title>

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

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

  ?>
