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

<html>
<head>
<?php kheader(); ?>
</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">

      

                <!-- /.row -->

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

     <form method="post">               
<div class="col-lg-12">
  <div class="panel panel-green">
                <div class="panel-heading">
                                <h3 class="panel-title">Search using a Child ID  or Child name</h3>
                            </div>
                            <div class="panel-body">


                  <div class='col-lg-4'>
                    <input type="text" name="search" class="form-control" placeholder="Child ID or Childname(Firstname or Surname)">
                  </div>


<div class='col-lg-3'> 
<input type="submit" name='searcher' class='btn btn-sm btn-success' value="Search">
</form>
</div>


                   
                             
                                </div>
                                
                            </div>
                    <?php
                    if(isset($_POST["searcher"])){
$search = $_POST["search"];

$result10 = $dbh->query("select * from children  where (sname like '%$search%' or fname like '%$search%')");
$row10 = $result10->fetchObject();
$count10 = $result10->rowCount();

if($count10>0){
$search2 = "or cid='$row10->cid'";
}else{
  $search2 = "";
}

$role = $_SESSION["role"];

if($role=="priaccount"){
$roler = "cid>=10000 && cid<=49999 and";

}elseif($role=="secaccount"){
  $roler = "cid>=50000 and";
}

$result = $dbh->query("
  select * from t1  where (feesbalance >'0' or reqsbalance >'0') and $roler cid='$search' $search2
  union
  select * from t2  where (feesbalance >'0' or reqsbalance >'0') and  $roler cid='$search' $search2
  union
  select * from t3  where (feesbalance >'0' or reqsbalance >'0') and  $roler cid='$search' $search2");
$row = $result->fetchObject();
$count = $result->rowCount();


echo "

<div class='panel panel-yellow'><!--Initiative-->
<div class='panel-heading'><h3 class='panel-title'>Search result(s) : ".$count."</h3></div>
<div class='panel-body'>
<table class='table table-bordered table-hover table-striped sortable'>

 <thead>
<tr>
<th>ID</th>
<th>Year</th>
<th>Class</th>
<th>Names</th>
<th>Total fees</th>
<th>Total bal</th>
<th>Bal.fees</th>
<th>Bal.rqmts</th>
<th>Term</th>
<th>Comment</th>
</tr>
</thead>
<tbody>";

if($count>0){
  do{

  $result4 = $dbh->query("select * from children  where cid='$row->cid'");
$row4 = $result4->fetchObject();
$count4 = $result4->rowCount();

echo "
<tr>
<td>".$row->cid."</td>
<td>20".$row->schoolyear."</td>";

include ("class.php");

$cid=$row->cid;
$schoolyear=$row->schoolyear;
$allbal=$row->feesbalance+$row->reqsbalance;

echo "
<td>".$daclass."</td>
<td>".$row4->fname." ".$row4->sname."</td>
<td>".number_format($row->ownfee)."</td>
<td><a href='addreceived.php?cid=".$cid."&tid=1&schoolyear=".$schoolyear."'>".number_format($allbal)."</a></td>
<td>".number_format($allbal)."</td>
<td>".number_format($row->feesbalance)."</td>
<td>term</td>
<td>".number_format($row->reqsbalance)."</td>

<td>".$row->comment."</td>
</tr>
";
}while($row = $result->fetchObject());
}else{echo "There is NO match";}



echo "
</tbody>
</table>

</div>
        </div>
        </div>";

                    }

?>
                        



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

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

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

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

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