<?php 
include ("../core/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">
                           Add Learner
                        
                        </h1>
                    </div>
                </div>
                <!-- /.row -->

                    <div class="col-lg-12">
                        <div class="panel panel-yellow">
                            <div class="panel-heading">
                                <h3 class="panel-title">Basic details</h3>
                            </div>
                            <div class="panel-body">
                                <div class="list-group">
               <?php
                if(isset($_POST["addstudent"])){
$role = $_SESSION["role"];

$fname=strtoupper($_POST["fname"]);
$sname=strtoupper($_POST["sname"]);
$schoolyear=$_POST["schoolyear"];
$schoolclass=$_POST["schoolclass"];
$term=$_POST["term"];
$gender=$_POST["gender"];
$currentdate=date('d-m-Y H:i:s');
$comment=$_POST["comment"];
$category=$_POST["category"];

if (isset($_POST["existing"])){$existing="1";} else {$existing='0';}

  $result=$dbh->query("select * from children order by cid desc");
  $row=$result->fetchObject();
  $count=$result->rowCount();
  if($count>0){$cid = $row->cid + 1;}else{$cid=1000;}


//upload pic
if($_FILES["file"]["name"] != ''){
include ("../core/uploads.php");
$img=$filename;
} else { $img=''; } 
//useractivity("(Image ".$img.")(cid ".$cid.")(firstname ".$fname.")(surname ".$sname.")(year ".$schoolyear.")(term ".$term.")
  //(gender ".$gender.")(class ".$schoolclass.")(comment ".$comment.") details for new student","2");


//1- put child to children as a must
$addthestudent=$dbh->query("insert into 
  children (cid,fname,sname,gender,schoolyear,term,schoolclass,img,status,comment,category,school_id) 
  values('$cid','$fname','$sname','$gender','$schoolyear','$term','$schoolclass','$img','2','$comment','$category','$school_id')"); 

  $result_fsitems=$dbh->query("select * from fsitems where school_id = '$school_id' and (schoolclass='$schoolclass' or schoolclass='All') and (gender='$gender' or gender='All') and (category='$category' or category='All')");
$row_fsitems=$result_fsitems->fetchObject();
$count_fsitems=$result_fsitems->rowCount();

if($count_fsitems > 0){ do{
// put them in homeland.
  $insert_homeland=$dbh->query("insert into $homelandtbl (cid,schoolyear,schoolclass,schoolterm,ouruser,paytype,fsitemid,amount) values('$cid','$schoolyear','$schoolclass','$term','$rolenumber','d','$row_fsitems->fsitemid','$row_fsitems->amount')");

  $insert_bravos=$dbh->query("insert into $bravostbl (cid,fsitemid,payable,balance,lastuser,lastupdate,originalfee,schoolyear,schoolclass,schoolterm) values($cid,'$row_fsitems->fsitemid','$row_fsitems->amount','$row_fsitems->amount','$rolenumber','$rolenumber','$row_fsitems->amount','$schoolyear','$schoolclass','$term')");

}while($row_fsitems=$result_fsitems->fetchObject());

//now check if he has fullpay total. if not, make for him. if yes already, add our item to his total.


$fullpay=$dbh->query("select sum(payable) from $bravostbl where cid='$cid'");
$countfullpay=$fullpay->rowCount();
if($countfullpay>0){$rowfullpay=$fullpay->fetch();} else {$rowfullpay='0';} 
$fullpay=$rowfullpay['0'];  //total target current month

$insert_bravos=$dbh->query("insert into $bravostbl (cid,fsitemid,payable,balance,lastuser,lastupdate,originalfee) values($cid,'fullpay','$fullpay','$fullpay','$rolenumber','$rolenumber','$fullpay')");
} else {
  echo "<div class='alert alert-danger'> Alert: Structure for this learner is not attached</div>";  
}



echo "<div class='alert alert-success'> Details on ".$fname." ".$sname." - ID: ".$cid." - School ".$school_id." have been submitted. </div>";
}

?>

<form method="post" enctype="multipart/form-data" >

<div class="col-lg-3">
<select class="form-control" name="schoolclass" required>
<option value=''>Class</option>
<?php

$role = $_SESSION["role"];


  $clas2 = "type='priclas' or type='secclas' or type='clas'";


$result5 = $dbh->query("select * from scrap where school_id = '$school_id' and $clas2");
$row5 = $result5->fetchObject();

do{
echo "<option value='".$row5->item."'>".$row5->item2."</option>";
}while($row5 = $result5->fetchObject());
?>

</select>
</div>


<!--row-->
<div class="col-lg-3">
<select name="schoolyear" class="form-control" required="required">
<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>
<!--row-->

<div class="col-lg-3">
<select class="form-control" name="term" required="required">
<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-3">
<select class="form-control" name="category" >
<option value=''>Category</option>
<?php
$result_scrap = $dbh->query("select * from scrap where school_id = '$school_id' and type='category'");
$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>

<!--break-->
<div class='col-lg-12'><br></div>
<!--break-->
<!--row-->                                   
<div class="col-lg-3">
<input type="text" name="fname" class="form-control" placeholder="First Name" id='fname' required>
</div>
<!--row-->
<!--row-->                                   
<div class="col-lg-3">
<input type="text" name="sname" class="form-control" placeholder="Other names" id='surname' required>
</div>
<!--row-->
<!--row-->                                   
<div class="col-lg-3">
<select class="form-control" name="gender" required="required">
<option value="">Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<!--row-->

<div class="col-lg-3">
<input type='file' id="files" name='file' accept='.jpg' class="hidden">
<label for="files"><span style='background-color: aqua; padding: 10px; border-radius: 10px; width: 100px; color: #481717;'>Upload Student's Photo</label>
</div>
<!--row-->


<!--break-->                             

<!--row-->
<!--row-->                                   

<!--break-->
<div class='col-lg-12'><br></div>
<!--break-->
<!--row-->                                   
<div class="col-lg-3">
<input type="text" name="comment" class="form-control" placeholder="Additional Comments">
</div>
<div class="col-lg-2">
<input type="checkbox" name="existing" placeholder="Additional Comments" value='1'> Existing Student
</div>
<!--row-->
<div style='float:right;margin-right:15px;'> 
<input type='submit' class='btn btn-sm btn-primary' name='addstudent' value='Submit Student'>
</div>
</form>


                                
                                </div>
                            </div>
                        </div>
                        </div>
                        <div class="col-lg-12">
  <div class="panel panel-green">
                <div class="panel-heading">
                                <h3 class="panel-title">Recently Added Students</h3>
                            </div>
                            <div class="panel-body">

<table class='table table-bordered table-hover table-striped sortable'>
<thead>
<tr>
<th>No</th>
<th>Student Name</th>
<th>Class</th>
<th>Term</th>
<th>Year</th>
<th>Category</th>
</tr>
<?php
$result_children = $dbh->query("select * from children where status=2 and school_id='".$school_id."' order by autoid desc limit 0,20");
$row_children = $result_children->fetchObject();
$count_children = $result_children->rowCount();

if($count_children>0){ $x=1;
  do{
    $result5 = $dbh->query("select * from scrap where item='$row_children->schoolclass' and type='priclas'");
$row5 = $result5->fetchObject();

$result6 = $dbh->query("select * from scrap where item='$row_children->category'");
$row6 = $result6->fetchObject();
    echo "<tr>
    <td>".$x++."</td>
    <td>".$row_children->cid." -  ".$row_children->fname." ".$row_children->sname."</td>
    <td>".$row5->item2."</td>
    <td>".$row_children->term."</td>
    <td>20".$row_children->schoolyear."</td>
    <td>".$row6->item2."</td>
    </tr>";

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

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