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



    <?php include '../admin/includes/styles.php';  global $school_name; ?>
    <title> View exam results |    <?php echo $school_name; ?> </title>

</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";
    
    $sql = "SELECT * FROM teachers WHERE username =:username";
    $conn->query($sql);
    $conn->bind(":username", $username);

    $result = $conn->fetchSingle();
    $teacher_name = $result->name;

      //fetch teacher section details 
      $sql = 'SELECT * FROM sections WHERE teacher_name =:teacher_name'; 
      $conn->query($sql);
      $conn->bind(":teacher_name", $teacher_name);
      $result_details = $conn->fetchSingle();
  
      $class_id = $result_details->class;
      $section_name = $result_details->name;
      $section_id = $result_details->id;
    
    ?>

    

    <!-- Content Wrapper. Contains page content -->
    <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"> View exam results </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 active">View exam results</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">
                        <div class="callout callout-danger" style="margin-top: 30px;">
                            <p>Note: Select the exam, class to view the exam results</p>
                        </div>
                        <div class="card card-info">
                            <div class="card-header">
                                <h3 class="card-title"><i class="fa fa-flask"></i> View students exam results</h3>
                            </div>
                            <!-- /.card-header -->
                            <!-- form start -->
                            <form class="form-horizontal" method="post" id="fetch_student_cbt_results">
                                <div class="card-body">
                                    <div class="form-row">
                                        <div class="col-md-3">
                                            <label for="class"> Class <span class="text-danger">*</span></label>
                                            <?php
                                                //fetch all classes
                                                $sql = "SELECT class FROM sections WHERE teacher_name =:teacher_name";
                                                $conn->query($sql);
                                                $conn->bind(":teacher_name", $teacher_name);
                                                $class = $conn->fetchColumn();

                                                ?>
                                            <select name="class_id" id="classID" class="form-control select2 myclass" required data-parsley-trigger="keyup" style="width: 100%;">
                                                <option value="">Select Class</option>
                                                <?php
                                                if ($conn->rowCount() > 0) {
                                                    $conn->query("SELECT name FROM classes WHERE id = '$class'"); 
                                                    $class_name = $conn->fetchColumn();
                                                    echo "<option value='$class'>$class_name</option>"; 
                                                   
                                                } else {
                                                    ?>
                                                    <option value="">No classes found</option><?php
                                                }

                                                ?>

                                            </select>
                                        </div>

                                        <div class="col-md-3">
                                            <label for="class"> Term <span class="text-danger">*</span></label>
                                            <select name="term_id" id="term" class="form-control select2" required data-parsley-trigger="keyup" style="width: 100%;">
                                                <option value="">Select Term</option>
                                                <?php
                                                //fetch the active term from general settings
                                                $sql = "SELECT * FROM terms";
                                                $conn->query($sql);
                                                $rowcount = $conn->rowCount();

                                                if($rowcount > 0){
                                                    $terms = $conn->fetchMultiple();
                                                   foreach ($terms as $term) {
                                                        ?> <option value="<?php echo $term->id; ?>"><?php echo $term->name; ?></option><?php
                                                   }
                                                }else{
                                                    ?>  <option value="">No active term found</option><?php
                                                }

                                                ?>


                                            </select>
                                        </div>

                                        <div class="col-md-3">
                                            <label for="section"> Section <span class="text-danger">*</span></label>
                                            <select name="section_id" id="section" class="form-control select2" required data-parsley-trigger="keyup" style="width: 100%;">
                                                <option value="">Select Section</option>
                                            </select>
                                        </div>

                                        <div class="col-md-3">
                                            <label for="subject"> Subject <span class="text-danger">*</span></label>
                                            <select name="subject_id" id="subject" class="form-control select2" required data-parsley-trigger="keyup" style="width: 100%;">
                                                <option value="">Select Subject</option>


                                            </select>
                                        </div>

                                        <div class="col-md-3">
                                            <label for="class"> Academic Session <span class="text-danger">*</span></label>
                                            <select name="academic_session" id="academic_session" class="form-control select2" required data-parsley-trigger="keyup" style="width: 100%;">
                                                <option value="">Select Session</option>
                                                <?php
                                                //fetch the active term from general settings
                                                $sql = "SELECT DISTINCT(academic_session) FROM cbt_exams";
                                                $conn->query($sql);
                                                $rowcount = $conn->rowCount();

                                                if($rowcount > 0){
                                                    $academic_sessions = $conn->fetchMultiple();
                                                   foreach ($academic_sessions as $session) {
                                                        ?> <option value="<?php echo $session->academic_session; ?>"><?php echo $session->academic_session; ?></option><?php
                                                   }
                                                }else{
                                                    ?>  <option value="">No sessions term found</option><?php
                                                }

                                                ?>


                                            </select>
                                        </div>
                                        

                                        <div class="col-md-12" style="margin-top: 20px;">
                                            <input type="submit" value="Fetch Available Students" class="btn btn-success" id="submit">
                                        </div>

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

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

                    <div class="col-md-12">
                        <!--                        display mark details-->
                        <div class="card card-info" id="display-details">

                        </div>


                    </div>


                    <!-- /.row (main row) -->
                </div><!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "../admin/includes/footer.php";?>

    <script>
        $(document).ready(function () {
            $('#classID').on('change', function () {
                var classID = $(this).val();
                if(classID){
                    $.ajax({
                        type: 'POST',
                        url: 'fetch_section.php',
                        data: 'classesID=' +classID,//append the classID
                        success: function (html) {
                            $('#section').html(html);
                        }

                    });
                }else{
                    $('#section').html('<option value="">select class  first</option>');
                }
            })
        })
    </script>

    <script>
        $(document).ready(function () {
            $('#classID').on('change', function () {
                var classID = $(this).val();
                if(classID){
                    $.ajax({
                        type: 'POST',
                        url: 'fetch_subject.php',
                        data: 'classesID=' +classID,//append the classID
                        success: function (html) {
                            $('#subject').html(html);
                        }

                    });
                }else{
                    $('#subject').html('<option value="">select class  first</option>');
                }
            })
        })
    </script>

    <script>
        $('#fetch_student_cbt_results').parsley();
        $('#fetch_student_cbt_results').on('submit', function (event) {
            event.preventDefault();
            if($('#fetch_student_cbt_results').parsley().isValid()){
                $.ajax({
                    url: 'fetch-students-cbt-result-details.php',
                    method: 'POST',
                    data: new FormData(this),
                    contentType: false,
                    cache: false,
                    processData: false,
                    beforeSend: function () {
                        $('#submit').attr('disabled', 'disabled');
                        $('#submit').val('fetching result details, pls wait.....');
                    },
                    success: function (data) {
                        $('#fetch_student_cbt_results').parsley().reset();
                        $('#submit').attr('disabled', false);
                        $('#submit').val('Fetch Available Students');
                        $('#display-details').html(data);
                    }
                });
            }
        });
    </script>

    <script>
        $(document).ready(function(){
            $('.select2').select2();
        })
    </script>
    <script>
        $(document).ready(function(){
            $('#class').select2();

        })

    </script>
    <script>
        $(function () {
            $("#teacher").DataTable({
                "responsive": true, "lengthChange": false, "autoWidth": false,
                "buttons": ["copy", "csv", "pdf", "print", "colvis"]
            }).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>

    <?php
     
      //submit form

    ?>

    <script>
        if ( window.history.replaceState ) {
            window.history.replaceState( null, null, window.location.href );
        }
    </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 '../admin/includes/styles.php'; ?>
        <?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 $teacher_login_link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>
    

    <?php
    }

    ?>
