<?php
session_start();
if(isset($_SESSION['teacher']))
{ 
  $teacher_id=$_SESSION['teacher']['id'];
  if(isset($_GET['class_id']) && isset($_GET['session_id']))
  {
     
    $class_id=htmlentities($_GET['class_id']);
    include '../manage/function.php';
    $session_id=htmlentities($_GET['session_id']);

    include '../classes/students_class.php';
    include '../classes/classroom_class.php';
    include '../classes/class_subjects_class.php';
    include '../classes/subjects_class.php';
    include '../classes/assessment_class.php';
    include '../classes/session_class.php';
    include '../classes/result_class.php';

    $student=new Student("../manage/connection.php");
    $classroom=new ClassRoom("../manage/connection.php");
    $classSubject=new ClassSubject("../manage/connection.php");
    $subject=new Subject("../manage/connection.php");
    $assessment=new Assessment("../manage/connection.php");
    $class_session=new Session("../manage/connection.php");
    $result=new Result("../manage/connection.php");
    
    if($classroom->getClassRoom($class_id)['teacher_id']==$teacher_id)
    {
?>
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Malika School</title>

    <!-- Template CSS -->
    <link rel="stylesheet" href="assets/css/style-starter.css">

    <link rel="stylesheet" href="assets/vendors/simple-datatables/style.css">
    <link rel="stylesheet" href="assets/vendors/perfect-scrollbar/perfect-scrollbar.css">
    <!-- google fonts -->
    <link href="//fonts.googleapis.com/css?family=Nunito:300,400,600,700,800,900&display=swap" rel="stylesheet">
</head>

<body class="sidebar-menu-collapsed">
    <div class="se-pre-con"></div>
    <section>
        <!-- sidebar menu start -->
        <?php
  include 'navigation.php';
  ?>
        <!-- //sidebar menu end -->
        <!-- header-starts -->
        <?php
  include 'header.php';
  ?>
        <!-- //header-ends -->
        <!-- main content start -->
        <div class="main-content">

            <!-- content -->
            <div class="container-fluid content-top-gap">

                <!-- breadcrumbs -->
                <nav aria-label="breadcrumb" class="mb-4">
                    <ol class="breadcrumb my-breadcrumb">
                        <li class="breadcrumb-item"><a href="index.html">Home</a></li>
                        <!-- <li class="breadcrumb-item"><a href="#">Elements</a></li> -->
                        <li class="breadcrumb-item active" aria-current="page">Students
                            (<?php echo count($student->getAllStudents());?>)</span></li>
                    </ol>
                </nav>
                <!-- //breadcrumbs -->
                <?php
          if(isset($_GET['message']))
          {
            echo '<div align="center" id="status"><h4>'.$_GET['message'].'</h4></div>';
          }
        ?>
                <div id="status" align="center"></div>
                <!-- people -->
                <section class="people">
                    <section class="w3l-team-block">
                        <!-- //people cards style 1 -->
                        <div class="card card_border mb-5">
                            <div class="cards__heading">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-sm-10">
                                            <h3><b
                                                    style="color: black;"><?php echo strtoupper($classroom->getClassRoom($class_id)['section']).' '.$classroom->getClassRoom($class_id)['class_no'];?></b>
                                                - <span>
                                                    <?php
                            echo $class_session->getSession($session_id)['year'].' ';
                            
                            if($class_session->getSession($session_id)['term']==1)
                            {
                              
                              echo $class_session->getSession($session_id)['term'].'st term ';
                            }
                            elseif($class_session->getSession($session_id)['term']==2)
                            {
                              
                              echo $class_session->getSession($session_id)['term'].'nd term ';
                            }
                            else 
                            {
                              
                              echo $class_session->getSession($session_id)['term'].'rd term ';
                            }
                            ?>
                                                    - Student's <span> Promotion</span> </h3>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <form method="post" id="prom">
                                <div class="card-body">
                                    <table class='table table-striped' id="table1">
                                        <thead>
                                            <tr>
                                                <th><input type="checkbox" id="select_all" name="" /></th>
                                                <th>S/N</th>
                                                <th>Student's Name</th>
                                                <th>Reg No.</th>
                                                <th>CA 1</th>
                                                <th>CA 2</th>
                                                <th>Exam</th>
                                                <th>Total</th>
                                                <th>Position</th>
                                                <!-- <th>Action</th> -->
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                            $n=1;
                            foreach($result->getAllResult($class_id, $session_id) as $resultInfo)
                            {
                                echo '<tr>
                                <td>
                                '.(($student->getStudent($resultInfo['student_id'])['classroom_id']==$class_id)?'<input type="checkbox" id="student_check" name="promote_'.$resultInfo['student_id'].'" value=""/>': "").'
                                </td>
                                <td>'.$n.'</td>
                                <td>'.$student->getStudent($resultInfo['student_id'])['firstname'].' '.$student->getStudent($resultInfo['student_id'])['lastname'].'</td> 
                                <td>'.$student->getStudent($resultInfo['student_id'])['reg_no'].'</td> 
                                <td>'.$resultInfo['ca_1'].'</td>
                                <td>'.$resultInfo['ca_2'].'</td>
                                <td>'.$resultInfo['exam'].'</td>
                                <td>'.$resultInfo['total'].'</td>
                                <td>'.posInfo($resultInfo['position']).'</td>
                                <!--<td><a href="view_result.php?result_id='.$resultInfo['id'].'" class="btn btn-primary">View</a></td>-->
                            </tr>';
                            $n++;
                            }

                            ?>

                                        </tbody>
                                    </table>


                                    <div align="center" id="checkStatus"></div>
                                    <div class="row">
                                        <?php
                                    if(count($result->getAllResult($class_id, $session_id))!=0)
                                    {
                                      ?>
                                      <div class="col-sm-4"></div>
                                        <div class="col-sm-4">
                                          <span>Choose Class To Promote</span>
                                          <input type="radio" name="class_id" value="<?php echo $class_id;?>" style="display: none;" checked/>
                                          <select type="" class="form-control" name="promote_class">
                                            <?php
                                              foreach($classroom->getAllClasses() as $allClass)
                                              {
                                                echo '
                                                <option value="'.$allClass['id'].'">'.ucwords($allClass['section']).' '.$allClass['class_no'].'</option>';
                                              }
                                            ?>
                                            <option value="00">Graduated</option>
                                          </select>
                                        </div>
                                        <div class="col-sm-4"></div>
                                        <div class="col-sm-4"></div>
                                        <div align="center" class="col-sm-4">
                                          <br>
                                            <button name="submit" class="btn btn-success">Promote
                                                Selected</button>
                                        </div>
                                        <?php 
                                    }?>
                                    </div>
                            </form>
                        </div>
            </div>
    </section>
    <!-- //teams1 -->

    <!-- //people cards style 2 -->

    </section>
    <!-- //people -->

    </div>
    <!-- //content -->
    </div>
    <!-- main content end-->
    </section>
    <!--footer section start-->
    <?php
include 'footer.php';
?>
    <!--footer section end-->
    <!-- move top -->
    <button onclick="topFunction()" id="movetop" class="bg-primary" title="Go to top">
        <span class="fa fa-angle-up"></span>
    </button>
    <script>
    // When the user scrolls down 20px from the top of the document, show the button
    window.onscroll = function() {
        scrollFunction()
    };

    function scrollFunction() {
        if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
            document.getElementById("movetop").style.display = "block";
        } else {
            document.getElementById("movetop").style.display = "none";
        }
    }

    // When the user clicks on the button, scroll to the top of the document
    function topFunction() {
        document.body.scrollTop = 0;
        document.documentElement.scrollTop = 0;
    }
    </script>
    <!-- /move top -->


    <script src="assets/js/jquery-3.3.1.min.js"></script>
    <script src="assets/js/jquery-1.10.2.min.js"></script>

    <!-- chart js -->
    <script src="assets/js/Chart.min.js"></script>
    <script src="assets/js/utils.js"></script>
    <!-- //chart js -->

    <!-- Different scripts of charts.  Ex.Barchart, Linechart -->
    <script src="assets/js/bar.js"></script>
    <script src="assets/js/linechart.js"></script>
    <!-- //Different scripts of charts.  Ex.Barchart, Linechart -->


    <script src="assets/js/jquery.nicescroll.js"></script>
    <script src="assets/js/scripts.js"></script>
    <script src="assets/vendors/perfect-scrollbar/perfect-scrollbar.min.js"></script>
    <script src="assets/vendors/simple-datatables/simple-datatables.js"></script>
    <script src="assets/js/vendors.js"></script>

    <!-- close script -->
    <script>
    var closebtns = document.getElementsByClassName("close-grid");
    var i;

    for (i = 0; i < closebtns.length; i++) {
        closebtns[i].addEventListener("click", function() {
            this.parentElement.style.display = 'none';
        });
    }
    </script>
    <!-- //close script -->

    <!-- disable body scroll when navbar is in active -->
    <script>
    $(function() {
        $('.sidebar-menu-collapsed').click(function() {
            $('body').toggleClass('noscroll');
        })
    });
    </script>
    <!-- disable body scroll when navbar is in active -->

    <!-- loading-gif Js -->
    <script src="assets/js/modernizr.js"></script>
    <script>
    $(window).load(function() {
        // Animate loader off screen
        $(".se-pre-con").fadeOut("slow");;
    });
    </script>
    <!--// loading-gif Js -->
    <script>
    $(document).on('click', '#select_all', function() {

        if ($('#select_all').is(':checked')) {
            // alert('Checked')
            $('[id*="student_check"]').each(function() {
                $(this).prop('checked', true);
            });

        } else {
            // alert('Not Checked')
            $('[id*="student_check"]').each(function() {
                $(this).prop('checked', false);
            });
        }
        // Checked Count
        checkCount();
    });

    // Get Checked

    $(document).on('click', 'input[type=checkbox]', function() {
        checkCount();
    });
    // Get promote clicked
    $(document).on('submit', '#prom', function(e) {
        e.preventDefault();
        var formData = new FormData(this);
        
        $('#status').html('<h4 class="card-title">Loading...</h4>');
        $.ajax({
            url: "../control/promote_student.php", // Url to which the request is send
            type: "POST", // Type of request to be send, called as method
            data: formData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
            contentType: false, // The content type used when sending data to the server.
            cache: false, // To unable request pages to be cached
            processData: false, // To send DOMDocument or non processed data file it is set to false
            success: function(data) // A function to be called if request succeeds
            {
                if(data=="success")
                {
                  $('#status').html('<h4 class="card-title">Promotion SUccessfull</h4>');
                  $('#checkStatus').html("<h4>Promotion SUccessfull</h4>");
                  setTimeout(function(){
                    window.location="?class_id=<?php echo $class_id;?>&&session_id=<?php echo $session_id;?>";
                  },1000);
                }
                else
                {
                  $('#status').html('<h4 class="card-title">' + data + '</h4>');
                  $('#checkStatus').html("<h4>" + data + "</h4>");
                }
                  //  alert(data);
            }
        });
    });


    function checkCount() {
        check_count = 0;
        $('[id*="student_check"]').each(function() {
            if ($(this).is(':checked')) {
                check_count++;
            }
        });
        $('#checkStatus').html("<h4>" + check_count + " Selected</h4>");
        // var check_count=0;
    }
    </script>
    <!-- Bootstrap Core JavaScript -->
    <script src="assets/js/bootstrap.min.js"></script>


</body>

</html>
<?php
    }
    else
    {
      header('location:assessments.php');
    }
  }
  else
  {
    header("location:classes.php");
  }
}
else
{
    header("location:../index.php");
}
?>