<?php include ("kfunctions.php");
//include ("seredirect.php");
?>
<html>
<head>
<?php kheader(); ?>
<?php 
require_once("connect.php");
$result=$dbh->query("select * from scrap");

?>
<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">

<script src="js/sorttable.js"></script>
<script src="js/printreport.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">
                        <h1 class="page-header">
                           Done/ Undone subjects
                        </h1>
                        
                    </div>
                </div>
                <!-- /.row -->

                <div class="row">
<form method='post'>
<div class='col-lg-2'>
<select class='form-control' name='level' onchange='this.form.submit()'>
<option value=''>Level</option>
<option value='alevel'>A level subjects</option>
<option value='olevel'>O level subjects</option>
</select>
</div>
</form>
<div class='col-lg-12'><br></div>
<div id='myDiv'>
<div class='col-lg-12'>                         
<div class="panel panel-primary"><!--Initiative-->
<div class="panel-heading"><h3 class="panel-title">Subjects</h3></div>
<div class="panel-body">
<?php
if(isset($_POST["submit_subs"])){
$level=$_POST["level"];
if($level=="alevel"){$level_out="type='secsuba'";}
elseif($level=="olevel"){$level_out="type='secsub' or type='prisubps'";}
include("connect.php");

$result_scrap=$dbh->prepare("select * from scrap where $level_out ");
$result_scrap->execute();
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
do{
$sub=$_POST["sub".$row_scrap->autoid.""];
if(isset($_POST["check".$row_scrap->autoid.""])){$check_out=1;}else{$check_out=0;}

$update_scrap=$dbh->prepare("update scrap set item4='$check_out' where item='$sub'");
$update_scrap->execute();

}while($row_scrap=$result_scrap->fetchObject());
  echo "<div class='alert alert-success'>Success: Subjects have been updated. </div>";
}else{
    echo "<div class='alert alert-danger'>Error: Subjects have not been updated. </div>";
}

}    
if(isset($_POST["level"])){
$level=$_POST["level"];
if($level=="alevel"){$level_out="type='secsuba'";}
elseif($level=="olevel"){$level_out="type='secsub' or type='prisubps'";}

echo "
<form method='post'>
<input type='submit' style='float:right;' name='submit_subs' class='btn btn-sm btn-primary' value='Submit'>
<input type='hidden' name='level' value='".$level."'>
<table class='table table-bordered table-hover table-striped sortable'>
<tr>
<th>No</th>
<th>Subject</th>
<th>Check/ Uncheck subject</th>
</tr>";

$result_scrap=$dbh->prepare("select * from scrap where $level_out ");
$result_scrap->execute();
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
$x=1;
do{
if($row_scrap->item4==1){$check_stat="checked";}
else{$check_stat="";}

echo "<tr>
<input type='hidden' name='sub".$row_scrap->autoid."' value='".$row_scrap->item."'>
<td>".$x++."</td>
<td>".$row_scrap->item2." ".substr($row_scrap->item,6)."</td>
<td><input type='checkbox' name='check".$row_scrap->autoid."' value='1' ".$check_stat."> Check to activate </td>";

echo "<tr>";
}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "<tr>
    <td colspan='10' align='center'>Please contact your IT department.</td>
    </tr>";}
echo "</table>
</form>";
}

 ?>


</div></div>
</div>
</div>
</div>
          <!-- /.container-fluid -->

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

</html>
