<?php
session_start();
error_reporting('0');
include '../admin/includes/database.php';
include '../admin/includes/functions.php';
$conn= new Functions();
$output = '';
if(!empty($_POST['class_id']) && !empty($_POST['term_id']) && !empty($_POST['section_id'])
    && !empty($_POST['subject_id']) && !empty($_POST['academic_session'])){
    $class = $_POST['class_id'];
    $term = $_POST['term_id'];
    $section = $_POST['section_id'];
    $subject = $_POST['subject_id'];
    $academic_session = $_POST['academic_session'];

    //fetch details from cbt_exams table 
    $sql = "SELECT * FROM cbt_exams WHERE class_id = :class_id 
    AND term_id =:term_id AND section_id =:section_id AND subject_id =:subject_id
    AND academic_session =:academic_session";
    $conn->query($sql);
    $conn->bind(":class_id", $class);
    $conn->bind(":term_id", $term);
    $conn->bind(":section_id", $section);
    $conn->bind(":subject_id", $subject);
    $conn->bind(":academic_session", $academic_session);

    $result_details = $conn->fetchSingle();
    if(!$result_details){
        $output = "<script>toastr['error']('No students found..')</script>";
        echo $output;
        exit();
    }

    $exam_id = $result_details->id;
    $exam_title = $result_details->title;
    $exam_academic_session = $result_details->academic_session;
    $subject_id = $result_details->subject_id;
    //fetch subject name 
    $conn->query("SELECT * FROM subjects WHERE id = :subject_id");
    $conn->bind(":subject_id", $subject_id);
    $subject_details = $conn->fetchSingle();
    $subject_name = $subject_details->subject_name;
    $exam_date = date("F, d, Y", strtotime($result_details->exam_date));
    
    $count = 1;
     
    

    //fetch students names based on the selected class
    $sql = "SELECT * FROM student WHERE classesID = :class AND sectionid =:section ORDER BY name";
    $conn->query($sql);
    $conn->bind(":class", $class);
    $conn->bind(":section", $section);
    $rowCount = $conn->rowCount();
    if($rowCount > 0) {

        $result = $conn->fetchMultiple();
        $available_students_id = [];

        foreach($result as $row): 
            $student_id = $row->studentID;
            $student_name = $row->name;
            array_push($available_students_id, $student_id);
        endforeach;
        
        //select from cbt_results where studentID in (available_students_id)
        $sql = "SELECT * FROM cbt_results 
        JOIN student ON student.studentID = cbt_results.student_id 
        WHERE student_id IN (" . implode(',', $available_students_id) . ")
        AND cbt_results.exam_id = $exam_id";

        $conn->query($sql);
        $row_count = $conn->rowCount(); 

        if($row_count > 0){
            $result = $conn->fetchMultiple(); 
            $output = "<div class='card-body'>
                     <div class='callout callout-danger' style='margin-top: -20px; position: relative; left: -20px; border-radius: unset;'>
                            <p>Note: Result fetched successfully.</p>
                        </div>
                 ";
        $output.= "<div class='col-md-12 jumbotron'>
                   <h3 class='text-center'>Result Details</h3>
                   <div class='text-center'>
                   <span class='text-center' style='display: inline-block;'><strong>Exam Title: </strong><span> $exam_title</span></span><br>
                   <span class='text-center' style='display: inline-block;'><strong>Academic Session:</strong> <span> $exam_academic_session</span></span><br>
                  </div>

            </div>";

        $output.= "<div class='table-responsive'>";
        $output .= "<table class='table table-bordered table-hover table-stripped' id='teacher'>
                     <thead>
                        <th>#</th>
                        <th>Photo</th>
                        <th>Name</th>  
                        <th>Subject</th>
                        <th>Date</th>
                        <th>Correct Answers</th>
                        <th>Action</th>
                    </thead>";
        $output .= "<tbody>";

        foreach ($result as $student){
        

            $student_image = $student->photo;
            if(empty($student_image)){
                $student_image = "<img src='images/default.png' width='32' height='32'>";
            }else{
                $student_image = "<img src='upload/$student->photo' width='32' height='32'>";
            }
            $reg_no = $student->registerNO;
            $output.= " <tr>
                <td>$count</td>
                <td>$student_image </td>
                <td>$student->name</td>
                <td>$subject_name</td>
                <td>$exam_date</td>
                <td>$student->correct_answers / $student->total_questions</td>
                <td><a href='student_exam_result.php?exam_id=$exam_id&student_id=$student->student_id' class='btn btn-info w-100' target='_blank'>View Full Result</a></td>
                
            </tr>";
            $count++;
        }


        $output .= "</tbody>";
        $output .= "<tfoot>
                    <th>#</th>
                    <th>Photo</th>
                    <th>Name</th>  
                    <th>Subject</th>
                    <th>Date</th>
                    <th>Correct Answers</th>
                    <th>Action</th>
                    </tfoot>";

        $output.= "</table>";
        $output.= "</div>";
        $output .= "</div>";
        echo $output;


        }else{
            echo "No result found";
        }

    
    }else{
        echo "<p style='padding: 30px; text-align: center; '> No Students found for the selected criteria.</p>";
    }

}else{
    echo "<script>toastr['error']('Fields marked asterisk(*) are required.')</script>";
}

?>

<style>
    /* media query */
    @media only screen and (max-width: 680px){
        /* display table data as block level element */
        table tr td, th {
            display: block !important;
            width: 100%;
        }
    }

</style>
