<?php
session_start();
error_reporting('0');
include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();
$output = '';
if(!empty($_POST['class']) && !empty($_POST['exam']) && !empty($_POST['section'])
    && !empty($_POST['year'])){
    $class = $_POST['class'];
    $exam = $_POST['exam'];
    $section = $_POST['section'];
    $exam_year = $_POST['year'];


    //fetch subject and class

    $sql = "SELECT name FROM sections WHERE id = :id";
    $conn->query($sql);
    $conn->bind(":id", $section);
    $db_section = $conn->fetchColumn();

    //fetch class names
    $sql = "SELECT name FROM classes WHERE id = :id";
    $conn->query($sql);
    $conn->bind(":id", $class);
    $db_class = $conn->fetchColumn();



    $count = 1;
    //fetch students names based on the selected class
    $sql = "SELECT * FROM student WHERE classesID = :class AND sectionid =:section ORDER BY name ASC";
    $conn->query($sql);
    $conn->bind(":class", $class);
    $conn->bind(":section", $section);
    $rowCount = $conn->rowCount();
    if($rowCount > 0) {
        $result = $conn->fetchMultiple();
        $output = "<div class='card-body'>";
        $output.= "<div class='col-md-12 jumbotron'>
                   <h3 class='text-center'> Based on your Selections</h3>
                   <div class='text-center'>
                   <span class='text-center' style='display: inline-block; font-size: 20px;'>Class: <span style='color: #296658;'>$db_class</span></span><br>
                   <span class='text-center' style='display: inline-block;font-size: 20px;'>Selected Term: <span style='color: #296658;'>$exam</span></span><br>
                   <span class='text-center' style='display: inline-block;font-size: 20px;'>Section: <span style='color: #296658;'>$db_section</span></span><br>
                   <span class='text-center' style='display: inline-block;font-size: 20px;'>Year: <span style='color: #296658;'>$exam_year</span></span>
                  </div>
                  
                  <p style='color: #379239; font-weight: bold; text-align: center; margin-top: 20px;'>Cognitive Area Keys: 
                   <span style='color: black; font-size: 16px; '> 1 (Poor), 2 (Fair), 3 (Good), 4 (Very Good), 5 (Excellent) </span>
                  </p>
                  
                  <p style='color: #9b44ba; font-weight: 500; text-align: center; margin-top: 20px;'>N.B: After adding new cognitive area, to view the added details, scroll to the top of the page and 
                     click on <span style='font-size: 16px;' class='text-info'>View Student Details</span> button again to update record list. </p>

            </div>";

        $output.= "<div class='table-responsive'>";
        $output .= "<table class='table table-bordered table-hover table-stripped' id='teacher'>
                     <thead>
                    <th style='width: 2%'>#</th>
                    <th style='width: 5%'>Photo</th>
                    <th style='width: 20%'>Name</th>
                    <th>Cognitive Area</th>
                    </thead>";
        $output .= "<tbody>";
        foreach ($result as $student) {
            //fetch exam and test scores from db if it exists
            $sql = "SELECT * FROM student_results WHERE student_name = :name AND class=:class AND section =:section AND school_year =:school_year AND term =:exam";
            $conn->query($sql);
            $conn->bind(":name", $student->name);
            $conn->bind(":section", $db_section);
            $conn->bind(":class", $db_class);
            $conn->bind(":exam", $exam);
            $conn->bind(":school_year", $exam_year);
            $details = $conn->fetchSingle();

            $rowCount = $conn->rowCount();
            if ($rowCount > 0) {

                $student_image = $student->photo;
                if (empty($student_image)) {
                    $image_link = $conn->base_url() . 'images/default.png';
                    $student_image = "<img src='$image_link' width='32' height='32'>";
                } else {
                    $image_link = $conn->base_url() . "upload/$student->photo";
                    $student_image = "<img src='$image_link' width='32' height='32'>";
                }

                $output .= " <tr>";
                $output .= " <td> $count </td>";
                $output .= " <td>  $student_image  </td>";
                $output .= " <td> <input type='hidden' name='student_name[]' value='$student->name'>$student->name </td>";



                //fetch school student's cognitive area details from table
                $sql = "SELECT * FROM students_cognitive_area WHERE student_name=:name 
                        AND exam =:exam AND exam_year =:year AND class =:class AND section =:section";
                $conn->query($sql);
                $conn->bind(":name", $student->name);
                $conn->bind(":exam", $exam);
                $conn->bind(":year", $exam_year);
                $conn->bind(":class", $db_class);
                $conn->bind(":section", $db_section);
                $result_details = $conn->fetchMultiple();

                $output .= "<td>";
                if($result_details){
                    $output .= "<div class='card card-default collapsed-card'>";
                    $output .= "<div class='card-header'> 
                           <h3 class='card-title'> Update Cognitive Area details for <span style='color: #50c478; font-weight: bold;'> $student->name </span> </h3>

                                <div class='card-tools'>
                                    <button type='button' class='btn btn-tool' data-card-widget='collapse'>
                                        <i class='fas fa-minus'></i>
                                    </button>
                                </div>
                         </div>";

                    $output .= "<div class='card-body'>";//start of card body div

                    $output .= "<div class='row'>";//start of row div
                    $output .= "<div class='col-12 col-sm-12'>";//start of col-sm-12 div

                    foreach ($result_details as $found_psychomotor) {
                        $psychomotor_name = $found_psychomotor->cognitive_name;
                        $psychomotor_value = $found_psychomotor->cognitive_value;
                        $psychomotor_id = $found_psychomotor->id;
                        //loop through all the data retrieved
                        $output .= "<form action='' method='post' id='$psychomotor_id'>
                             <div class='form-group row'>
                                                <div class='col-sm-7'>
                                                    <input type='text' class='form-control' id='psychomotor_name' name='psychomotor_name' value='$psychomotor_name' required data-parsley-trigge='keyup' readonly>
                                                </div>
                                                <div class='col-sm-3'>
                                                    <input type='number' class='form-control' id='psychomotor_value' name='psychomotor_value' min='1' max='5' value='$psychomotor_value' required data-parsley-trigge='keyup'>
                                                </div>
                                                <input type='hidden' name='id' value='$psychomotor_id'>
                                               
                                                <div class='col-sm-2'>
                                                    <input type='submit' name='update_psychomotor' value='GO'
                                                           class='btn btn-info' id='submit$psychomotor_id'>
                                                </div>
                                            </div>
                                            <!-- /.form-group -->
                                            <div id='updated-status'></div>
                                          
                          </form>";

                        ?>
                        <script>

                            $('#<?php echo $psychomotor_id?>').parsley();
                            $('#<?php echo $psychomotor_id?>').on('submit', function (event) {
                                event.preventDefault();
                                if($('#<?php echo $psychomotor_id?>').parsley().isValid()){
                                    $.ajax({
                                        url: 'update-cognitive.php',
                                        method: 'POST',
                                        data: new FormData(this),
                                        contentType: false,
                                        cache: false,
                                        processData: false,
                                        beforeSend: function () {
                                            $('#submit<?php echo $psychomotor_id;?>').attr('disabled', 'disabled');
                                            $('#submit<?php echo $psychomotor_id;?>').val('Updating...');
                                        },
                                        success: function (data) {
                                            $('#<?php echo $psychomotor_id?>').parsley().reset();
                                            $('#submit<?php echo $psychomotor_id;?>').attr('disabled', false);
                                            $('#submit<?php echo $psychomotor_id;?>').val('GO');
                                            $('#updated-status').html(data);
                                        }
                                    });
                                }
                            });
                        </script>
                        <?php


                    }
                    $output .= "</div>";//end of col-sm-12 div
                    $output .= "</div>";//end of row div
                    $output .= "</div>";//end of card body div
                    $output .= "</div>";//end of card-default div

                }//end of Result Details if statement

                //query to add new psychomotor skills
                $sql = "SELECT * FROM cognitive_area";
                $conn->query($sql);
                $fetched_result = $conn->fetchMultiple();
                if($fetched_result){
                    $output .= "<div class='card card-secondary collapsed-card'>";
                    $output .= "<div class='card-header'> 
                           <h3 class='card-title'> Add New Cognitive Area details for <span style='color: #ffb14c; font-weight: bold;'> $student->name </span> </h3>

                                <div class='card-tools'>
                                    <button type='button' class='btn btn-tool' data-card-widget='collapse'>
                                        <i class='fas fa-minus'></i>
                                    </button>
                                </div>
                         </div>";

                    $output .= "<div class='card-body'>";//start of card body div

                    $output .= "<div class='row'>";//start of row div
                    $output .= "<div class='col-12 col-sm-12'>";//start of col-sm-12 div
                    $counter = 1;

                    foreach ($fetched_result as $new_psychomotor) {
                        $new_psychomotor_name = $new_psychomotor->name;
                        $new_psychomotor_id = $new_psychomotor->id;
                        $student_name = $conn->slugify($student->name);
                        //loop through all the data retrieved
                        //check if the same psychomotor details exist in records


                        $output .= "<form action='' method='post' id='p_$student_name$counter'>
                             <div class='form-group row'>
                                            <div class='col-sm-7'>
                                                    <input type='text' class='form-control' id='psychomotor_name' name='new_psychomotor_name' value='$new_psychomotor_name' required data-parsley-trigge='keyup' readonly>
                                                </div>
                                                <div class='col-sm-3'>
                                                    <input type='number' class='form-control' id='psychomotor_value' name='new_psychomotor_value' min='1' max='5'  required data-parsley-trigge='keyup' placeholder='Enter Value'>
                                                </div>
                                                  
                                                <div class='col-sm-2'>
                                                    <input type='submit' name='update_psychomotor' value='ADD'
                                                           class='btn btn-success' id='send$student_name$counter'>
                                                </div>
                                            </div>
                                            <input type='hidden' name='student_section' value='$db_section'>
                                            <input type='hidden' name='student_class' value='$db_class'>
                                            <input type='hidden' name='exam' value='$exam'>
                                            <input type='hidden' name='exam_year' value='$exam_year'>
                                            <input type='hidden' name='student_name' value='$student->name'>
                                            <!-- /.form-group -->
                                            <div id='new-updated-status'></div>
                                          
                                </form>";



                        ?>


                        <script>

                            $('#p_<?php echo $student_name.$counter; ?>').parsley();
                            $('#p_<?php echo $student_name.$counter; ?>').on('submit', function (event) {
                                event.preventDefault();
                                if($('#p_<?php echo $student_name.$counter; ?>').parsley().isValid()){
                                    $.ajax({
                                        url: 'insert-cognitive.php',
                                        method: 'POST',
                                        data: new FormData(this),
                                        contentType: false,
                                        cache: false,
                                        processData: false,
                                        beforeSend: function () {
                                            $('#send<?php echo $student_name.$counter; ?>').attr('disabled', 'disabled');
                                            $('#send<?php echo $student_name.$counter; ?>').val('Adding...');
                                        },
                                        success: function (data) {
                                            $('#p_<?php echo $student_name.$counter; ?>').parsley().reset();
                                            $('#send<?php echo $student_name.$counter; ?>').attr('disabled', false);
                                            $('#send<?php echo $student_name.$counter; ?>').val('ADD');
                                            $('#new-updated-status').html(data);
                                        }
                                    });

                                }
                            });
                        </script>
                        <?php
                        $counter++;


                    }
                    $output .= "</div>";//end of col-sm-12 div
                    $output .= "</div>";//end of row div
                    $output .= "</div>";//end of card body div
                    $output .= "</div>";//end of card-default div

                }

                $output .= "</td>";//end of table data tag

            }
                $count++;

            }

            $output.= "</tr>";
            $output .= "</tbody>";

        $output .= "<tfoot>
                    <th>#</th>
                    <th>Photo</th>
                    <th>Name</th>
                    <th>Cognitive Area</th>
                    </tfoot>";

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


}else{
    echo "<p class='alert alert-danger alert-dismissible fade show text-center' role='alert'>
              <i class='fas fa-ban'></i>
               Fields marked (*) are required.
                  <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
                             <span aria-hidden='true'>×</span>
                  </button>
           </p>";
}

?>
    <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)');
        // });
    </script>



<?php




