<?php
session_start();
$menu=$_SESSION['menu'];
include $menu;
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Απουσίες Μαθήματος</title>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:10pt; }
<p style="font-size:10px;"></p>
</style>

</head>

<body>
<h2>ΑΠΟΥΣΙΕΣ ΜΑΘΗΜΑΤΟΣ</h2>
<?php
require "dbconnect.php"; 

$dt=$_SESSION['dt'];

//$dt=date_format(new DateTime('2014/09/05'), 'Y-m-d'); 
//........................................DATE_FORMAT($dt,'%Y-%m-%d')
$name = $_SESSION['n'];
$usr=$_SESSION['usr'];
echo "<br>"."ΗΜΕΡΟΜΗΝΙΑ:".$dt;
echo "<br>"; 

 $pas=$_GET['pas'];
 $arr = explode("/", $pas);
 $lesson = $arr[0];
 $per = $arr[1];
   $_SESSION['per']=$per;
   $day=date('D').$per;
   

$less=$lesson;
$result = mysqli_query($con,"SELECT mitroo,eponimo,onoma FROM allstudentinfo WHERE tmima='$lesson' ORDER BY eponimo,onoma");
$crow1=mysqli_num_rows($result);
if ($crow1 > 0) {
$i=1;
 while($row = mysqli_fetch_array($result))
     {
	  $mt[$i]=$row['mitroo'];
	  $epo[$i]=$row['eponimo']." ".$row['onoma'];
	  $tmim[$i]=$lesson;
	  $i++;}
}
else
{
$result = mysqli_query($con,"SELECT syndidaskalies.pmp,allstudentinfo.eponimo,allstudentinfo.onoma,allstudentinfo.tmima FROM 
syndidaskalies as sy,allstudentinfo WHERE sy.synd = '$lesson' AND sy.pmp=allstudentinfo.mitroo 
ORDER BY allstudentinfo.eponimo,allstudentinfo.onoma");
$crow1=mysqli_num_rows($result);
	$cr=1;
	while($row=mysqli_fetch_array($result)){	
		$mt[$cr]=$row['pmp'];
	    $epo[$cr]=$row['eponimo']." ".$row['onoma'];	
		$tmim[$cr]=$row['tmima'];
		$cr++;}
}	
		
//..............................................

$res1=mysqli_query($con, "SELECT code,onoma FROM ekdilosi WHERE date='$dt' ");
$c1=mysqli_num_rows($res1);
echo "<br >"."Ο ΑΡΙΘΜΟΣ ΤΩΝ ΔΡΑΣΤΗΡΙΟΤΗΤΩΝ ΤΗΣ ΗΜΕΡΑΣ ΕΙΝΑΙ ".$c1."<br >";

 if ($c1 > 0)  {  
 $i=0;
 while($row = mysqli_fetch_array($res1))
     {
	 $codekd[$i]=$row['code'];
	 $onomaekd[$i]=$row['onoma'];
	 $i++;
    }
 }
//echo " Διδάσκων :<input type='text' name='name' value='$name' />" ;
echo " Περίοδος :<input type='number' name='per' value='$per' /> ";
echo " Τμήμα/Συνδ.:<input type='text' name='lesson' value='$lesson' /> <br /><br />  ";
$_SESSION['less']=$lesson;
//////////////////////////////////////////////////////////////////////////////////////////
$i=1;
 while($i<=$crow1){$ap[$i]='unchecked';$stoli[$i]='unchecked'; $kath[$i]=0;$i++;}


$sql=mysqli_query($con,"SELECT apousies.mitroo, apousies.apousia, allstudentinfo.tmima FROM apousies,allstudentinfo WHERE apousies.mitroo=allstudentinfo.mitroo AND apousies.dateout='$dt' AND apousies.teacher='$usr' AND apousies.mathima='$lesson' AND apousies.period='$per' AND apousies.apousia=1" );

$c2=mysqli_num_rows($sql);



    while($row = mysqli_fetch_array($sql)){	
	for($i=1;$i<=$crow1;$i++)
     {
		if ( $mt[$i]== $row['mitroo']){
			 $ap[$i]='checked';}
	 }}
	 
$sql=mysqli_query($con,"SELECT apousies.mitroo, apousies.stoli,allstudentinfo.tmima FROM apousies,allstudentinfo WHERE apousies.mitroo=allstudentinfo.mitroo AND apousies.dateout='$dt' AND apousies.teacher='$usr' AND apousies.mathima='$lesson' AND apousies.period='$per' AND apousies.stoli=1" );

$c2=mysqli_num_rows($sql);

    while($row = mysqli_fetch_array($sql)){	
	for($i=1;$i<=$crow1;$i++)
     {
		if ( $mt[$i]== $row['mitroo']){
			 $stoli[$i]='checked';}
	 }}

$sql=mysqli_query($con,"SELECT apousies.mitroo, apousies.kath, allstudentinfo.tmima FROM apousies,allstudentinfo WHERE apousies.mitroo=allstudentinfo.mitroo AND apousies.dateout='$dt' AND apousies.teacher='$usr' AND apousies.mathima='$lesson' AND apousies.period='$per' AND apousies.kath > 0" );
$c2=mysqli_num_rows($sql);

	while($row = mysqli_fetch_array($sql)){	
	for($i=1;$i<=$crow1;$i++)
     {
		if ( $mt[$i]== $row['mitroo']){
	    	$kath[$i]=$row['kath'];}
			
	 }}
?>

<form method="POST" name = "eggrafi" action="eggrafi.php" >
 
<?php
echo "<table class='myTable' >
<tr style='border:1px solid black' >
<tr>
<th>α/α</th>
<th>Α.Μ</th>
<th>Τμήμα</th>
<th>Όνοματεπώνυμο</th>
<th>Απουσία</th>
<th>Καθυστ.</th>
<th>Στολή/Εμφάνιση</th>
<th>Απουσιάζει για Δραστηριότητα</th>

</tr>";


$i=1;
 while($i<=$crow1)
     {
	 
	 echo "<tr>";
	 echo "<td>" .$i;
	 echo "<td>". "<input type='text' name='mt[]' value='$mt[$i]' size='6' >" . "</td>";
	 echo "<td>". "<input type='text' name='tmim' value='$tmim[$i]' size='6' >" . "</td>";
	 echo "<td>". "<input type='text' name='epo[]' value='$epo[$i]' size='30' >" . "</td>";  
	 echo "<td>". "<input type='checkbox' value='$mt[$i]' name='att[]' $ap[$i] >" ."</td>";
	 echo "<td>". "<input type='text' name='kath[]' value='$kath[$i]' size='6'   >" ."</td>";
	 echo "<td>". "<input type='checkbox' value='$mt[$i]' name='stoli[]' $stoli[$i] size='6'>" ."</td>"; 
	 
 	 //................
	 if ($c1 > 0 ) {	 
	 $i1=0;
	 
	 foreach ($codekd as $cdo) {
	 
	 	 $q2 =mysqli_query($con, "SELECT p1,p2,p3,p4,p5,p6,p7 FROM ekdilosistu WHERE pmp = '$mt[$i]' AND code='$cdo' ");
	 $c2=mysqli_num_rows($q2);
	 if ($c2 > 0){
	   
	 $row2 = mysqli_fetch_array($q2);
	  $p1=$row2['p1'];$p2=$row2['p2'];$p3=$row2['p3'];$p4=$row2['p4'];$p5=$row2['p5'];$p6=$row2['p6'];$p7=$row2['p7'];		
		$np="p".$per;
	    if ($$np == 1) { echo "<td>". $onomaekd[$i1] ."</td>"; }}
			   
	 $i1++; }}

	 echo "</tr>";
$i++;
}

echo "</table>";
$_SESSION['fg']=$i;

echo "<br>"."ΚΑΤΑΧΩΡΗΣΗ ΤΗΣ ΥΛΗΣ / ΠΕΡΙΓΡΑΦΗ ΔΡΑΣΤΗΡΙΟΤΗΤΑΣ";
echo "<br>";
$sql=mysqli_query($con,"SELECT perigrafi FROM syllabi  WHERE pmp='$usr' AND per='$per' AND date='$dt'  "); 
 $row = mysqli_fetch_array($sql);
 $perigrafi=$row['perigrafi'];
echo "<textarea name='perigrafi' cols='80' rows='3'> $perigrafi </textarea >"."<br >";

?>
<input type="Submit" name="Submitapou" value="Καταχώρηση">
</form>

<br />
<a  href=<?php echo $menu; ?>  style="color:blue "  >  Επιστροφή στο Μενού  </a>

</body>
</html>