<?php include ("kfunctions.php"); ?>
<html>
<head>
<?php kheader(); ?>
<?php 
require_once("connect.php");

?>
<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script src="js/sorttable.js"></script>
</head>
<body>


    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <?php ktopbar(); ?>
            <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
            <?php kleftbar(); ?>
            <!-- /.navbar-collapse -->
        </nav>

        <div id="page-wrapper">

            <div class="container-fluid">

 <!-- Page Heading -->
                


                <div class="row">
                    <form method='post'>
<div class='col-lg-2'>
<select class='form-control' name='classlevel' onchange='this.form.submit()'>
<option value=''>Level</option>
<option value='<50000'>Primary</option>
<option value='>50000'>Secondary</option>
</select>
    </div>
</form>
<div class='col-lg-12'><br></div>


<?php
include ("connect.php");
if(isset($_POST["submit_grades"])){
$classlevel=$_POST["classlevel"];


$result_grades = $dbh->query("select * from grades ");
$row_grades = $result_grades->fetchObject();
$count_grades = $result_grades->rowCount();

if($count_grades>0){
do{
$from=$_POST["from".$row_grades->autoid.""];
$to=$_POST["to".$row_grades->autoid.""];
$grades=$_POST["grades".$row_grades->autoid.""];

if($classlevel=="<50000"){$from_out="pfrom"; $to_out="pto"; }
elseif($classlevel==">50000"){$from_out="sfrom"; $to_out="sto"; }

if($from!="" and $to!=""){
$update_grades=$dbh->query("update grades set $from_out='$from', $to_out='$to' where grade='$grades'");

}
}while($row_grades = $result_grades->fetchObject());

if($classlevel=="<50000"){$dalevel="Primary";}elseif($classlevel==">50000"){$dalevel="Secondary";}
echo "<div class='col-lg-12'><div class='alert alert-success'>Success: Grades for ".$dalevel." have been updated. </div></div>";

 }else{
    echo "<div class='col-lg-12'><div class='alert alert-danger'>Sorry : Grades have not been submitted.  </div></div>";
 }   
}

if(isset($_POST["classlevel"])){
$classlevel=$_POST["classlevel"];
if($classlevel=="<50000"){$daclass="Primary";}elseif($classlevel==">50000"){$daclass="Secondary";}
$title="for ".$daclass;
}else{
$title="";
}


echo "
<div class='col-lg-12'>     
<div class='panel panel-info'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Edit grades ".$title."</h3></div>
<div class='panel-body'>";

if(isset($_POST["classlevel"])){
$classlevel=$_POST["classlevel"];

echo "
<form method='post'>
<input type='submit' name='submit_grades' style='float:right;' class='btn btn-sm btn-warning' value='Submit grades'> 
<input type='hidden' name='classlevel' value='".$classlevel."'>
<table width='30%' class='table table-bordered table-hover table-striped sortable'>
 <thead>
<tr>
<th width='5%'>No</th>
<th width='20%'>From</th>
<th width='20%'>To</th>
<th width='20%'>Grades</th>
<th width='20%'>Aggregates</th>
</tr>
</thead>
<tbody>";
$result_grades = $dbh->query("select * from grades ");
$row_grades = $result_grades->fetchObject();
$count_grades = $result_grades->rowCount();

if($count_grades>0){
    $x=1;
do{
if($classlevel=="<50000"){$from_out=$row_grades->pfrom; $to_out=$row_grades->pto;}
elseif($classlevel==">50000"){$from_out=$row_grades->sfrom; $to_out=$row_grades->sto;}

echo "
<tr>
<td>".$x++."</td>
<td><input type='text' class='form-control' name='from".$row_grades->autoid."' value='".$from_out."'</td>
<td><input type='text' class='form-control' name='to".$row_grades->autoid."' value='".$to_out."'></td>
<td><input type='text' class='form-control' name='grades".$row_grades->autoid."' value='".$row_grades->grade."' readonly></td>
<td><input type='text' class='form-control'  value='".$row_grades->aggr."' readonly></td>
</tr>";
}while($row_grades = $result_grades->fetchObject());
}else{
    echo "
<tr>
<td colspan='6' align='center'>There is NO data in the table so far. </td>
</tr>";
}

echo "
</tbody>
</table>
</form>";
}else{
    echo "Select from the options above and select filter";
}
?>
</div></div>
</div>
                </div>
                <!-- /.row -->

               

            </div>
            <!-- /.container-fluid -->
<?php useractivity("on page", "56"); ?>
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js" charset="UTF-8"></script>
</body>

</html>
