<?php 
include ("kfunctions.php");
role("overadmin");
?>

<html>
<head>
<?php kheader(); ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!--<script src="js/thousandseparator.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" style='margin-top:10px;'>

      
            <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">
                           Learners
                        
                        </h1>
                    </div>
                </div>
<form method='post'>
                <!-- /.row -->
                <div class="col-lg-3">
<select name="schoolyear" class="form-control">
<option value='<?php echo date('y'); ?>'>20<?php echo date('y'); ?></option>

<?php
$result2 = $dbh->query("select * from scrap where type='ya'");
$row2 = $result2->fetchObject();

do{
echo "<option value='".$row2->item."'>".$row2->item2."</option>";
}while($row2 = $result2->fetchObject());
?>
</select>
</div>

                <div class='col-lg-3'>
<select class='form-control' name='schoolclass'>
<option value="">Class</option>
<?php
include("connect.php");
$result_scrap = $dbh->query("select * from scrap where type='priclas'");
$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-3">
<select class="form-control" name="term">
<option value="">Term</option>
<option value="1">Term 1 </option>
<option value="2">Term 2 </option>
<option value="3">Term 3 </option>
</select>
</div>

<div class="col-lg-1">
<input type='submit' name='filter' class='btn btn-md btn-primary' value='Filter'>
</div>
</form>

                                            <div class="col-lg-12"><br>
  <div class="panel panel-green">
                <div class="panel-heading">
                                <h3 class="panel-title">Student List</h3>
                            </div>
                            <div class="panel-body">
<?php
if(isset($_POST['filter'])){
  if(!empty($_POST["schoolyear"])){
   $schoolyear=$_POST["schoolyear"];
   $fschoolyear="and schoolyear='$schoolyear' "; 
   echo "<b>Year :</b> 20".$schoolyear.", ";
 } else { $fschoolyear="";}
 
 if(!empty($_POST["schoolclass"])){
   $schoolclass=$_POST["schoolclass"];
   $fschoolclass="and schoolclass='$schoolclass'";
   $result5 = $dbh->query("select * from scrap where item='$schoolclass'");
   $row5 = $result5->fetchObject();
 
   echo " <b>Class : </b> ".$row5->item2.", ";
 } else { $fschoolclass='';}
 
 if(!empty($_POST["term"])){
   $term=$_POST["term"];
   $fterm="and term='$term'";
   echo " <b>Term</b> : ".$term;
 } else { $fterm=''; }
 
 $fquery=$fschoolyear.$fschoolclass.$fterm;
 $limiter='';
 
 } else {$fquery='';$limiter='limit 0,20';}

if(isset($_POST["submit_students"])){
  if(!empty($_POST["schoolyear"])){
    $schoolyear=$_POST["schoolyear"];
    $fschoolyear="and schoolyear='$schoolyear' "; 
    
  } else { $fschoolyear="";}
  
  if(!empty($_POST["schoolclass"])){
    $schoolclass=$_POST["schoolclass"];
    $fschoolclass="and schoolclass='$schoolclass'";
    
  } else { $fschoolclass='';}
  
  if(!empty($_POST["term"])){
    $term=$_POST["term"];
    $fterm="and term='$term'";
    
  } else { $fterm=''; }
  
  $fquery=$fschoolyear.$fschoolclass.$fterm;
  $limiter='';

$result_children = $dbh->query("select * from children where status=2 $fquery order by autoid desc $limiter");
$row_children = $result_children->fetchObject();
$count_children = $result_children->rowCount();


if($count_children>0){ do{ $i=$row_children->cid;
  if(!empty($_POST["discount".$i.""]) || !empty($_POST["increment".$i.""]) || !empty($_POST["balance".$i.""])){
    $discount=$_POST["discount".$i.""];
    $increment=$_POST["increment".$i.""];
    $balance=$_POST["balance".$i.""];
    $cid=$_POST["cid".$i.""];
    $schoolyear=$_POST["schoolyear".$i.""];
    $schoolclass=$_POST["schoolclass".$i.""];
    $term=$_POST["term".$i.""];

    $result_homeland = $dbh->query("select * from homeland where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and schoolterm='$term'");
    $count_homeland = $result_homeland->rowCount();
  
    echo $cid."<br>";

    if($count_homeland == 0){
    $insert_homeland=$dbh->query("insert into homeland (cid,schoolyear,schoolclass,schoolterm,ouruser,discount,increment,amount) values($cid,'$schoolyear','$schoolclass','$term','$rolenumber','$discount','$increment','$balance')");
    $insert_bravos=$dbh->query("insert into bravos (cid,schoolyear,schoolclass,schoolterm,lastuser,discount,increment,balance) values($cid,'$schoolyear','$schoolclass','$term','$rolenumber','$discount','$increment','$balance')");
    } else {
      $update=$dbh->query("update homeland set discount='$discount', increment='$increment', ouruser='$rolenumber', amount='$balance' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and schoolterm='$term'");
      $update=$dbh->query("update bravos set discount='$discount', increment='$increment', lastuser='$rolenumber', balance='$balance' where cid='$cid' and schoolyear='$schoolyear' and schoolclass='$schoolclass' and schoolterm='$term'");
    }
    
  
  }

}while($row_children = $result_children->fetchObject());
}
//echo "<script>window.location='list_students.php'</script>";
}

?>
<form method='post'>
  <?php 
  if(isset($schoolclass)){echo "<input type='hidden' name='schoolclass' value='".$schoolclass."'>";}
  if(isset($schoolyear)){echo "<input type='hidden' name='schoolyear' value='".$schoolyear."'>";}
  if(isset($term)){echo "<input type='hidden' name='term' value='".$term."'>";}
  ?>
<input type='submit' class='btn btn-md btn-primary' name='submit_students' style='float:right;' value='Submit'>
<table class='table table-bordered table-hover table-striped sortable'>
<thead>
<tr>
<th>No</th>
<th>Student Name</th>
<th>Discount</th>
<th>Increments</th>
<th>Balance</th>

</tr>
<?php
$result_children = $dbh->query("select * from children where status=2 $fquery order by autoid desc $limiter");
$row_children = $result_children->fetchObject();
$count_children = $result_children->rowCount();

if($count_children>0){ $x=1; $y=1; echo "<input type='hidden' name='fquery' value='".$fquery."'>";
  do{ $z=$row_children->cid;
    $result5 = $dbh->query("select * from scrap where item='$row_children->schoolclass'");
$row5 = $result5->fetchObject();

$result_homeland = $dbh->query("select * from homeland where cid='$row_children->cid' and schoolyear='$row_children->schoolyear' and schoolclass='$row_children->schoolclass' and schoolterm='$row_children->term'");
$row_homeland = $result_homeland->fetchObject();
$count_homeland = $result_homeland->rowCount();

if($count_homeland>0){
  $txtValD="value='".$row_homeland->discount."'";
  $txtValI="value='".$row_homeland->increment."'";
  $txtValB="value='".$row_homeland->amount."'";
} else {
  $txtValD="placeholder='Enter Discount'";
  $txtValI="placeholder='Enter Increment'";
  $txtValB="placeholder='Enter Balance'";
}

    echo "<tr>
    <td>".$x++."</td>
    <td>".$row_children->fname." ".$row_children->sname."</td>
    <td><input type='number' class='form-control' name='discount".$z."' ".$txtValD."></td>
    <td><input type='number' class='form-control' name='increment".$z."' ".$txtValI."></td>
    <td><input type='number' class='form-control' name='balance".$z."' ".$txtValB."></td>
    <td><a href='manage_paychanger.php?cid=$row_children->cid' class='btn btn-md btn-warning'>Manage Pay Changer</a> 
    <input type='hidden' name='cid".$z."' value='".$row_children->cid."'>
    <input type='hidden' name='schoolyear".$z."' value='".$row_children->schoolyear."'> </td>
    <input type='hidden' name='schoolclass".$z."' value='".$row_children->schoolclass."'>
    <input type='hidden' name='term".$z."' value='".$row_children->term."'> </td>
 
    </tr>
    ";

  }while($row_children = $result_children->fetchObject());
}
?>
</table>

                  
                  </div> 
                   </div>


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

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

    </div>
    <!-- /#wrapper -->
<?php useractivity("on page","2"); ?>
    <!-- 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>

    <!-- Morris Charts JavaScript -->

</body>

</html>
