<?php
	include('../include/config.php');
	$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
    $timenow=$date->format('Y-m-d H:i:s');
    $timenow2=$date->format('Y-m-d');
    $exa=$_POST['exa'];
    $grd=$_POST['grd'];

    $sql = "SELECT * FROM pupils where Claz='$grd' AND Status='Active'";
	$result = $con->query($sql);
	if ($result->num_rows > 0) 
	{
	$sql2 = "SELECT * FROM CBC1 where Grad='$grd' AND EID='$exa'";
	$result = $con->query($sql2);
	if ($result->num_rows > 0) 
	{
	    
	 ?>
	 <table class="table table-bordered" id="tablecbc" style="font-size:12px;font-family:Tahoma;width:auto">
     <thead>
         <tr>
             <th>#</th>
             <th>Pupil's Details</th>
             <th>Maths</th>
             <th>Eng</th>
             <th>Kisw</th>
             <th>CRE</th>
             <th>Creative Arts</th>
             <th>Environmental Activities</th>
             <th class="bg-secondary">Overall Performance</th>
             <th>Action</th>
         </tr>
     </thead>
     <tbody id="" style="font-size:11px;font-family:Arial">
	 <?php
	$sql = "SELECT CBC1.*, pupils.Names as nm, pupils.AdmNo as adm from CBC1 left join pupils ON CBC1.PID=pupils.count where CBC1.EID='$exa' AND Grad='$grd' AND pupils.Status='Active'
	GROUP BY CBC1.PID ORDER BY CBC1.TMks DESC";
	$result = $con->query($sql);
	    $cnt=1;
		while($row = $result->fetch_assoc()) 
		{
     ?>	
		<tr>
			<td><?php echo $cnt; ?></td>
			<td><?php  echo $row['nm'].' ('.$row['adm'].')'; ?></td>
			<td> <strong><?php echo $row['Math']; ?> </strong> - <?php echo $row['Ruma']; ?></td>
			<td> <strong><?php echo $row['Eng']; ?> </strong> - <?php echo $row['Ruen']; ?></td>
		    <td> <strong><?php echo $row['Kis']; ?> </strong> - <?php echo $row['Ruks']; ?></td>
		    <td> <strong><?php echo $row['CRE']; ?> </strong> - <?php echo $row['CRE1']; ?></td>
		    <td> <strong><?php echo $row['CrA']; ?> </strong> - <?php echo $row['CA1']; ?></td>
            <td> <strong><?php echo $row['EA']; ?> </strong> - <?php echo $row['EA1']; ?></td>
            <td style="background-color:#d1e0e0"> 
               <strong><?php echo $row['TMks']; ?> </strong> - <?php echo $row['TRub']; ?>
            </td>
            <td> <input type="hidden" class="exadataid"  value="<?php echo $row['mid']; ?>" />
               <i class="fa fa-edit btn text-green records" style="font-size:12px" title="click to Update"></i>
               <a href="linklower.php?Exam=<?php echo $row["EID"]; ?>&Pupil=<?php echo $row["PID"]; ?>" style="font-size:11px" target="_blank">Report <i class="fa fa-file"></i></a>
             </td>
		</tr>
    <?php
    $cnt=$cnt+1;
	}
	?>
	<tr>
	    <td colspan="10">
	    </td>
	</tr>
	</tbody>
 </table>
 
 <?php
	}else
	{
	 $sqll=mysqli_query($con,"UPDATE pupils SET EID='$exa' where Claz='$grd'");
     $query3=mysqli_query($con,"INSERT CBC1(PID,Grad,EID) SELECT count,Claz, EID FROM pupils WHERE Claz='$grd' AND Status='Active'");
     
	 ?>
	 <table class="table table-bordered" id="tablecbc" style="font-size:12px;font-family:Tahoma;width:auto">
     <thead>
         <tr>
             <th>#</th>
             <th>Pupil's Details</th>
             <th>Maths</th>
             <th>Eng</th>
             <th>Kisw</th>
             <th>CRE</th>
             <th>Creative Arts</th>
             <th>Environmental Activities</th>
             <th class="bg-secondary">Overall Performance</th>
             <th>Action</th>
         </tr>
     </thead>
     <tbody id="" style="font-size:11px;font-family:Arial">
	 <?php
	$sql = "SELECT CBC1.*, pupils.Names as nm, pupils.AdmNo as adm from CBC1 left join pupils ON CBC1.PID=pupils.count where CBC1.EID='$exa' AND Grad='$grd' AND pupils.Status='Active' GROUP BY CBC1.PID ORDER BY CBC1.TMks DESC";
	$result = $con->query($sql);
	    $cnt=1;
		while($row = $result->fetch_assoc()) 
		{
     ?>	
		<tr>
			<td><?php echo $cnt; ?></td>
			<td><?php  echo $row['nm'].' ('.$row['adm'].')'; ?></td>
			<td> <strong><?php echo $row['Math']; ?> </strong> - <?php echo $row['Ruma']; ?></td>
			<td> <strong><?php echo $row['Eng']; ?> </strong> - <?php echo $row['Ruen']; ?></td>
		    <td> <strong><?php echo $row['Kis']; ?> </strong> - <?php echo $row['Ruks']; ?></td>
		    <td> <strong><?php echo $row['CRE']; ?> </strong> - <?php echo $row['CRE1']; ?></td>
		    <td> <strong><?php echo $row['CrA']; ?> </strong> - <?php echo $row['CA1']; ?></td>
            <td> <strong><?php echo $row['EA']; ?> </strong> - <?php echo $row['EA1']; ?></td>
            <td style="background-color:#d1e0e0"> 
               <strong><?php echo $row['TMks']; ?> </strong> - <?php echo $row['TRub']; ?>
            </td>
            <td> <input type="hidden" class="exadataid"  value="<?php echo $row['mid']; ?>" />
               <i class="fa fa-edit btn text-green records" style="font-size:12px" title="click to Update"></i>
             </td>
		</tr>
    <?php
    $cnt=$cnt+1;
	}
	?>
	<tr>
	    <td colspan="10">
	    </td>
	</tr>
	</tbody>
 </table>
 
 <?php

	}
	}
	else {
		echo "0 results";
	}
		
	mysqli_close($con);

?>