<?php include ("kfunctions.php");
//role("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='panel panel-primary'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Transfer students</h3></div>
<div class='panel-body'>
<?php
include("connect.php");
//isset classes
if(isset($_POST["submit_classes"])){
$schoolyear1=$_POST["schoolyear1"];
$schoolterm1="t".$_POST["schoolterm1"];
$schoolyear2=$_POST["schoolyear2"];
$schoolterm2="t".$_POST["schoolterm2"];
$rterm=$_POST["schoolterm2"];
$fterm2=$_POST["schoolterm2"];

//scrap
$result_scrap=$dbh->prepare("select * from scrap where type='secclas' or type='priclas'");
$result_scrap->execute();
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
do{
//checkbox
if(isset($_POST["fclass".$row_scrap->autoid.""])){
$fclass=$_POST["fclass".$row_scrap->autoid.""];

//selected t
$result_t=$dbh->prepare("select * from $schoolterm1 where schoolyear='$schoolyear1' and schoolclass='$fclass' and active='1'");
$result_t->execute();
$row_t=$result_t->fetchObject();
$count_t=$result_t->rowCount();

$schoolclass=$fclass;
include("class.php");

if($count_t>0){
do{
//checking 
$result_ct=$dbh->prepare("select * from $schoolterm2 where schoolyear='$schoolyear1' and schoolclass='$fclass' and cid='$row_t->cid' and active='1'");
$result_ct->execute();
$row_ct=$result_ct->fetchObject();
$count_ct=$result_ct->rowCount();

$result_child=$dbh->prepare("select * from children where cid='$row_t->cid'");
$result_child->execute();
$row_child=$result_child->fetchObject();

//class : false
if($count_ct==0){
$insert_t=$dbh->prepare("insert into $schoolterm2 (schoolclass,schoolyear,cid,ownfee,lastupdated,feesbalance,reqsbalance,uniform,uniformbal,contribution,contributionbal,registration,registrationbal,holiday,holidaybal,admission,admissionbal,transport,transportbal,mockfa,mockfabal,other1,other1bal,other2,other2bal,other3,other3bal,orgfee,comment,status,active) values('$row_t->schoolclass','$row_t->schoolyear','$row_t->cid','$row_t->ownfee','$row_t->lastupdated','$row_t->feesbalance','$row_t->reqsbalance','$row_t->uniform','$row_t->uniformbal','$row_t->contribution','$row_t->contributionbal','$row_t->registration','$row_t->registrationbal','$row_t->holiday','$row_t->holidaybal','$row_t->admission','$row_t->admissionbal','$row_t->transport','$row_t->transportbal','$row_t->mockfa','$row_t->mockfabal','$row_t->other1','$row_t->other1bal','$row_t->other2','$row_t->other2bal','$row_t->other3','$row_t->other3bal','$row_t->orgfee','$row_t->comment','$row_t->status','$row_t->active')");
$insert_t->execute();

$update_citadelo=$dbh->prepare("update citadel_o set term='$rterm' where schoolclass='$row_t->schoolclass' and active='1'");
$update_citadelo->execute();

$update_citadela=$dbh->prepare("update citadel_a set term='$rterm' where schoolclass='$row_t->schoolclass' and active='1'");
$update_citadela->execute();
echo "<div class='alert alert-success'><b>Success</b>: ".$row_child->fname." ".$row_child->sname." <b>Class : </b>".$daclass." has been transfered successfully. </div>";
}else{
echo "<div class='alert alert-warning'><b>Error</b>: ".$row_child->fname." ".$row_child->sname." <b>Class : </b>".$daclass." already exists in Term ".$fterm2.". </div>";
}
//class : false
//checking
}while($row_t=$result_t->fetchObject());
}else{
echo "<div class='alert alert-danger'>Error: Transfer unsuccessful for ".$daclass." to Term ".$fterm2.". </div>";
}
//selected t
}
//checkbox
}while($row_scrap=$result_scrap->fetchObject());
}
//scrap
}
//isset classes
?>
<input type='checkbox' name='selectedall' id='selectedall' onclick='checkall()' value=''> Selected All <br>
<div style='color:red;'>
<b><i>Caution </i></b><br>
1. Make sure you check the class you want to transfer<br>
2. The term on the left hand side is <b>NOT EQUAL</b> to the right hand side<br>
3. The term on the left hand side is <b>NOT LESS</b> to the right hand side<br>
</div>
<form method='post'>
<table class='table  table-hover table-striped '>
 <thead>
<tr>
<td align='center'><b>Transfer (checked)</b></td>
<th></th>
<td align='center'><b>Transfered</b></td>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class='col-lg-6'>
<select class='form-control' name='schoolyear1' id='schoolyear1' onclick='fieldchecker()' required>
<option value='<?php echo date("y"); ?>'><?php echo date("Y"); ?></option>
<?php
$result_scrap=$dbh->prepare("select * from scrap where type='ya'");
$result_scrap->execute();
$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-6'>
<select class='form-control' name='schoolterm1' id='schoolterm1' onclick='fieldchecker()' required>
<option value=''>Term</option>
<?php
$result_scrap=$dbh->prepare("select * from scrap where type='ts'");
$result_scrap->execute();
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
do{
echo "<option value='".$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>
</td>
<td align='center'><input type='submit' style='float:right;' class='btn btn-sm btn-warning' name='submit_classes' value='Transfer'></td>
<td align='right'>
<div class='col-lg-6'>
<select class='form-control' name='schoolyear2' id='schoolyear2' onclick='fieldchecker()' required>
<option value='<?php echo date("y"); ?>'><?php echo date("Y"); ?></option>
<?php
$result_scrap=$dbh->prepare("select * from scrap where type='ya'");
$result_scrap->execute();
$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-6'>
<select class='form-control' name='schoolterm2' id='schoolterm2' onclick='fieldchecker()' required>
<option value=''>Term</option>
<?php
$result_scrap=$dbh->prepare("select * from scrap where type='ts'");
$result_scrap->execute();
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
do{
echo "<option value='".$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>
</td>
</tr>
<?php
$result_scrap=$dbh->prepare("select * from scrap where type='secclas' or type='priclas'");
$result_scrap->execute();
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
do{
echo "<tr>
    <td  colspan='2'><input type='checkbox' id='fclass".$row_scrap->autoid."' name='fclass".$row_scrap->autoid."' value='".$row_scrap->item."'> ".$row_scrap->item2."</td>
    <td></td>
    
</tr>";
}while($row_scrap=$result_scrap->fetchObject());
}else{
    echo "<tr>
<td colspan='2'>Please contact your IT department. </td>
    </tr>";
}
?>
</tbody>
</table>
</form>
</div></div>

</div>
                            
                <!-- /.row -->

               

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

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

    </div>
    <!-- /#wrapper -->
    <script>
function fieldchecker(){
var schoolyear1 = document.getElementById("schoolyear1").value;
var schoolterm1 = document.getElementById("schoolterm1").value;
var schoolyear2 = document.getElementById("schoolyear2").value;
var schoolterm2 = document.getElementById("schoolterm2").value;

if(schoolyear1 < <?php echo date("y"); ?> || schoolyear2 < <?php echo date("y"); ?>){
alert("WARNING : YEAR  HAS TO BE GREATER OR EQUAL TO THER CURRENT YEAR. ");
return false
}else if(schoolyear1 > schoolyear2){
alert("WARNING : YEAR ON THE LEFT-HAND SIDE HAS TO BE GREATER THAN THE YEAR ON THE RIGHT-HAND SIDE. ");
return false
}

if((schoolterm1 != "" && schoolterm2 !="") && schoolterm1 == schoolterm2 && schoolyear1 == schoolyear2){
alert("WARNING : YOU ARE TRYING TO COPY TO THE SAME TERM IN THE SAME YEAR. ");
return false
}

}
function checkall(){
<?php
$result_scrap=$dbh->prepare("select * from scrap where type='priclas' or type='secclas'");
$result_scrap->execute();
$row_scrap=$result_scrap->fetchObject();
$count_scrap=$result_scrap->rowCount();

if($count_scrap>0){
    do{
?>
if(document.getElementById('selectedall').checked){
document.getElementById('fclass<?php echo $row_scrap->autoid; ?>').checked=true;
}else{
document.getElementById('fclass<?php echo $row_scrap->autoid; ?>').checked=false; 
}

<?php
}while($row_scrap=$result_scrap->fetchObject());
}

?>
}
</script>
    <!-- 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>
