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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table.myTable { border-collapse:collapse }
table.myTable td, table.myTable th { border:1px solid black;padding:5px;font-size:11pt; }
<p style="font-size:11px;"></p>
</style>

</head>
<body>
<h2> ΑΝΑΦΟΡA ΗΜΕΡΓΣΙΩΝ ΑΠΟΥΣΙΩΝ</h2>

<?php 

require("dbconnect.php");
$dt=$_SESSION['dt'];

?>
	
<form method="post" name = "form1">
ΗΜΕΡΟΜΗΝΙΑ: <input type="date"  name = 'dt' value= <?php echo $dt; ?> > 
<input type="submit" name="submit1" value="Επιβεβαίωση " >
</form>

<?php


if(isset($_POST['submit1']))
{
  $dt=$_POST['dt'];
  $_SESSION['dt']=$dt;
}

echo "<br>"."ΗΜΕΡΟΜΗΝΙΑ: ".$dt."<br>";
$day = date('D', strtotime($dt));
$_SESSION['day']=$day;

if ($day=="Sat" or $day=="Sun"){
    echo "<br>"."ΔΕΝ ΥΠΑΡΧΟΥΝ ΜΑΘΗΜΑΤΑ ΣΗΜΕΡΑ"."<br>";
	}
else 
{
	
 $sql = mysqli_query($con,"SELECT ap.teacher,t.Name,ap.period FROM apousies as ap,teachers as t WHERE ap.teacher=t.PMP AND ap.dateout='$dt' AND ap.apousia=1 ORDER BY ap.period");	
 $t=mysqli_num_rows($sql);
 $tnames = array();
 $tmathima = array();
 $i=1;
 $row = mysqli_fetch_array($sql);
 while (!empty($row)) 
 {
	 $teacher=$row['Name'];$k=1;
	 while ($teacher=$row['Name']){
        $tnames[$i] = $row['Name'];
		$tmathima[$i][$row['period']]=$row['mathima'];
		$k++;
	 }
	 $row = mysqli_fetch_array($sql);
	$i++;
 }
print_r($tmathima);

$sql= mysqli_query($con,"SELECT * FROM timetable  " );
$c=mysqli_num_rows($sql); 

$i=1;
$teach=array();
while($row = mysqli_fetch_array($sql))
{
	$teacher=$row['Teacher'];
	$flg=0;
	for ($j=1;$j<=7;$j++) {
	    $d=$day.$j;	
	    if (!empty($row["$d"])) $flg=1; }
	
	if ($flg == 1) {
		$teach[$i]=$teacher;
		$i++;
	}
	$row = mysqli_fetch_array($sql);
}
$limit=$i-1;	
$noapousies=array_diff($teach,$tnames);

echo "<b>"."ΑΠΟ ΤΟΥΣ ".$limit." ΚΑΘΗΓΗΤΕΣ ΠΟΥ ΔΙΔΑΣΚΟΥΝ ΣΗΜΕΡΑ"." ΚΑΤΑΧΩΡΗΣΑΝ ΑΠΟΥΣΙΕΣ ".$t." ΚΑΘΗΓΗΤΕΣ "."</b>"."<br>";
echo "<br>"."ΚΑΘΗΓΗΤΕΣ ΠΟΥ ΔΕΝ ΚΑΤΑΧΩΡΗΣΑΝ ΑΠΟΥΣΙΕΣ"."<br>";

$i=1;
foreach($noapousies as $nap) {
	echo $i." ".$nap."<br>";$i++; }
echo "<br>";
////////////////////////////////////////////////////////////////////////////////////////////////////////	
$sql = mysqli_query($con,"SELECT apousies.dateout, apousies.mitroo, apousies.period, apousies.teacher,
 apousies.mathima, allstudentinfo.onoma, allstudentinfo.eponimo, allstudentinfo.tmima, teachers.Name
 FROM apousies,allstudentinfo,teachers WHERE apousies.apousia=1 AND apousies.dateout='$dt'
 AND apousies.mitroo=allstudentinfo.mitroo AND apousies.teacher=teachers.PMP 
 ORDER BY apousies.dateout,apousies.teacher,apousies.period " );


echo "<table class='myTable' >
<tr style='border:1px solid black' >
<tr>
<th>ΚΑΘΗΓΗΤΗΣ</th>
<th>ΠΕΡ.</th>
<th>ΜΑΘΗΜΑ</th>
<th>ΌΝΟΝΑΤΕΠΩΝΥΜΟ ΜΑΘΗΤΗ / ΤΡΙΑΣ</th>
<th>ΥΠΟΓΡΑΦΗ</th>
</tr>";

$row = mysqli_fetch_array($sql);
while(!empty($row)) {
	  $teach=$row['Name'];
      $oldt=$teach;
	  $q = mysqli_query($con,"SELECT id FROM timetable WHERE Teacher='$teach'");
      $rowx = mysqli_fetch_array($q);
      $id1=$rowx["id"]+1;
	  $sign="POSTED";
	  echo "<tr>";
	  echo "<td colspan='4'>". $teach . "</td>";
	  echo "</tr>";
	  while ($oldt==$teach){
		 $mathima=$row['mathima'];
	     $pmp=$row['mitroo'];
	     $tmima=$row['tmima'];
	     $onoma="( ".$pmp." ".$tmima." "." ) ".$row['eponimo']." ".$row['onoma'];
	     $per=$row['period'];
		 //................
		 $d=$day.$per;
		 $q = mysqli_query($con,"SELECT ".$d." FROM timetable WHERE id='$id1'");
         $rowx = mysqli_fetch_array($q);
		 $d1=$rowx["$d"];
         $subject= trim(strrev(strstr(strrev((strstr($d1, '/'))), '(')), '/(');
  
   
		 //.......
		 
		 echo "<tr>";
		 
		 echo "<td>". " " . "</td>"; 
	     echo "<td>". $per . "</td>";
	     echo "<td>". $mathima."/".$subject . "</td>";
	     echo "<td>". $onoma."</td>";
	     echo "</tr>";
		 $row = mysqli_fetch_array($sql);
		 $teach = $row['Name'];
      }
	  echo "<tr>";
	  echo "<td colspan='4'>". " " . "</td>";
	  echo "<td >". $sign . "</td>";
	  echo "</tr>";
}
echo "</table>";	 
}
?>

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