<?php include ("kfunctions.php");
adminall("doss","dosp","overadmin")
?>
<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">

                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-body">


<form method='post'>
    <div class='col-lg-2'>
<select class='form-control' name='schoolclass' required>
<option value="">Class</option>
<?php
include("connect.php");
$role=$_SESSION["role"];
if($role=='doss'){$class_out="type='secclas'";}
elseif($role=='dosp'){$class_out="type='priclas'";}
elseif($role=='overadmin'){$class_out="type='priclas' or type='secclas'";}

$result_scrap = $dbh->query("select * from scrap where $class_out ");
$row_scrap = $result_scrap->fetchObject();
$count_scrap = $result_scrap->rowCount();
if($count_scrap>0){
do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap = $result_scrap->fetchObject());
}else{
echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
</div>
<div class='col-lg-2'>
<select class='form-control' name='schoolyear' required>
<option value=''>Year</option>
  <?php
$result_scrap = $dbh->query("select * from scrap where type='ya' ");
$row_scrap = $result_scrap->fetchObject();
$count_scrap = $result_scrap->rowCount();
if($count_scrap>0){
do{
echo "<option value='".$row_scrap->item."'>".$row_scrap->item2."</option>";
}while($row_scrap = $result_scrap->fetchObject());
}else{
echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
    </div>
<div class='col-lg-2'>
<select class='form-control' name='schoolterm' required>
<option value=''>Term</option>
<?php
  $result_scrap = $dbh->query("select * from scrap where type='ts' ");
$row_scrap = $result_scrap->fetchObject();
$count_scrap = $result_scrap->rowCount();
if($count_scrap>0){
do{
echo "<option value='t".$row_scrap->item."'>Term ".$row_scrap->item2."</option>";
}while($row_scrap = $result_scrap->fetchObject());
}else{
echo "<option value=''>Please contact your IT department. </option>";
}
?>
</select>
    </div>
<div class='col-lg-1'>
<input type='submit' name="std_filter" class='btn btn-sm btn-primary' value='Filter'>
</div>
</form>

<div class='col-lg-12'><br></div>
<div class='col-lg-12'>
<div class='panel panel-green'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Filtered students</h3></div>
<div class='panel-body'>

<?php

if(isset($_POST["submit"])){
$schoolyear =$_POST["schoolyear"];
$schoolterm =$_POST["schoolterm"];
$schoolclass =$_POST["schoolclass"];

$rt=substr($schoolterm,1);

include ("connect.php");
$result_t = $dbh->query("select * from $schoolterm where schoolyear='$schoolyear' and schoolclass='$schoolclass'");
$row_t = $result_t->fetchObject();
$count_t = $result_t->rowCount();

if($count_t>0){
   do{
    $action=$_POST["action".$row_t->autoid.""];
    $cid=$_POST["cid".$row_t->autoid.""];

$update_t=$dbh->query("update $schoolterm set active='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass'");

$update_children=$dbh->query("update children set status='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass'");

if($schoolclass<=10){
$update_citadelp=$dbh->query("update citadel_p set active='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and term='$rt'");
$update_kingsp=$dbh->query("update kingslanding_p set active='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and term='$rt'");    

}
elseif($schoolclass>=21 && $schoolclass<25){
$update_citadelo=$dbh->query("update citadel_o set active='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and term='$rt'");
$update_kingso=$dbh->query("update kingslanding_o set active='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and term='$rt'");

}
elseif($schoolclass>24){
$update_citadela=$dbh->query("update citadel_a set active='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and term='$rt'");
$update_rivendell=$dbh->query("update rivendell set active='$action' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and term='$rt'");    
}


   }while($row_t = $result_t->fetchObject());
}else{
echo "<div class='alert alert-danger'>Sorry: Submission was unsuccessful. </div>";    
}

echo "<div class='alert alert-success'>Success: Student status has been updated. </div>";
}

?>
<form method='post'>
<table class='table table-bordered table-hover table-striped sortable'>
 <thead>
<tr>
<th>No</th>
<th>Names</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_POST["std_filter"])){
echo "<input type='submit' style='float:right' name='submit' class='btn btn-sm btn-primary' value='Submit'>";

include ("connect.php");
$schoolyear =$_POST["schoolyear"];
$schoolterm =$_POST["schoolterm"];
$schoolclass =$_POST["schoolclass"];

echo "
<input type='hidden' name='schoolyear' value='".$schoolyear."'>
<input type='hidden' name='schoolclass' value='".$schoolclass."'>
<input type='hidden' name='schoolterm' value='".$schoolterm."'>";

$result_t = $dbh->query("select * from $schoolterm where schoolyear='$schoolyear' and schoolclass='$schoolclass'");
$row_t = $result_t->fetchObject();
$count_t = $result_t->rowCount();

if($count_t>0){
    $x=1;
do{
$result_child = $dbh->query("select * from children where cid='$row_t->cid'");
$row_child = $result_child->fetchObject();

echo "

<tr>
<td>".$x++."</td>
<td> ".$row_t->cid." - ".$row_child->fname." ".$row_child->sname."</td>
<td>
<input type='hidden' name='cid".$row_t->autoid."' value='".$row_t->cid."'>
<select class='form-control' name='action".$row_t->autoid."'>";
if($row_t->active==1){echo "<option value='1'>Active</option>";}
elseif($row_t->active==0){echo "<option value='0'>Inactive</option>";}
echo "
<option value='1'>Active</option>
<option value='0'>Inactive</option>
</select>
</td>
</tr>";
}while($row_t = $result_t->fetchObject());
}else{
    echo "So far, there is NO match data.";
}

}else{
    echo "
    <tr>
    <td align='center' colspan='3'><b><i>Select the above filters.</i></b></td>
</tr>
    ";
}
?>
</tbody>
</table>
</form>
</div></div>

</div>
                            </div> </div>
                    </div>
      

               

         

    <!-- 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>
