<?php
include "scrinit.php";
//include "logo.php";
?>

<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:3px;font-size:14pt; }
<p style="font-size:14px;"></p>
</style>

</head>
<body>
<?php 
require("dbconnect.php");
$gym=$_SESSION['gym'];
$gymx=$_SESSION['gymx'];
$dt=$_SESSION['dt'];

echo "<b>"."<p style='font-size:16px;'>"."ΑΝΑΦΟΡA ΗΜΕΡΗΣΙΩΝ ΑΠΟΥΣΙΩΝ: ".date('d-m-Y', strtotime($dt))."</p>"."</b>";

$day = date('D', strtotime($dt));

if ($day=="Sat" or $day=="Sun")
{
    echo "<br>"."ΔΕΝ ΥΠΑΡΧΟΥΝ ΜΑΘΗΜΑΤΑ ΣΗΜΕΡΑ"."<br>";
}
else 
{
    $sql = $dbcon->prepare("SELECT DISTINCT a.teacher,a.mitroo,t.Name FROM apousies as a,teachers as t 
	        WHERE a.teacher=t.PMP AND a.dateout=? ");
	$sql->execute([$dt]);	

    $tnames = $tpmp=array();
	$i=1;
	while ($row=$sql->fetch(PDO::FETCH_ASSOC)) 
	{
	   $tpmp[$i]=$row[teacher];
	   $i++;
	}
/////////////////////////////

include "allagiprog.php";

///////////////////////////
$sql= mysqli_query($con,"SELECT * FROM dailytab  " );
$c=mysqli_num_rows($sql); 
$day = date('D', strtotime($dt));
$i=1;
$teach=$t2pmp=array();
while($row = mysqli_fetch_array($sql))
{
	$teacher=$row['Teacher'];
	$flg=0;
	for ($j=1;$j<=$gym;$j++) {
	    $d=$day.$j;
		$dtab="day".$j;
	    if (!empty($row["$dtab"])) $flg=1; }
	
	if ($flg == 1) {
		$teach[$row['pmp']]=$teacher;
		$t2pmp[$i]=$row['pmp'];
		$i++;
	}
	$row = mysqli_fetch_array($sql);
}
$limit=$i-1;
	
$noapousies=array_diff($t2pmp,$tpmp);
$t=$limit-count($noapousies);

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

$i=1;
foreach($noapousies as $nap) 
{
	echo $i." ".$teach[$nap]."<br>";$i++;
}
$sqls = mysqli_query($con,"SELECT e.*,ek.onoma FROM ekdilosistu as e,ekdilosi as ek 
		        WHERE e.code=ek.code  AND ek.date='$dt' ");
	    $numsqls=mysqli_num_rows($sqls);
		$sekd=array();
	    while($rows = mysqli_fetch_array($sqls))
		{
	       $ename=$rows['onoma'];
		   $mitr=$rows['pmp'];
		   switch ($ename)
		   {
			   case "ΑΔΕΙΑ ΕΞΟΔΟΥ":
			   $ae="**ΑΔ.ΕΞ**";
			   break;
			   case "ΑΔΕΙΑ ΕΙΣΟΔΟΥ":
			   $ae="**ΑΔ.ΕΙΣ**";
			   break;
			   case "ΠΜ ΚΑΤΟΙΚΟΝ":
			   $ae="**ΑΠΟΒ**";
			   break;
			   case "ΑΣΘΕΝΕΙΑ":
			   $ae="**ΑΣΘ**";
			   break;
			   default:
			   $ae="**ΕΚΔ**";
		   }
		   for ($j=1;$j<=$gym;$j++)
		   {
			 $p="p".$j;  
			 if ($rows["$p"]==1) {$sekd[$mitr][$j]=$ae;}
		   }
		 }
echo "<br>";
////////////////////////////////////////////////////////////////////////////////////////////////////////
 $sql = $dbcon->prepare("SELECT apkath,aptime FROM parameters");
 $sql->execute();
 $row=$sql->fetch(PDO::FETCH_ASSOC);
 if ($row['apkath']==1)
 {
	 $aptime=$row['aptime'];	
 $sql = $dbcon->prepare("SELECT a.dateout, a.mitroo, a.period, a.teacher,
 a.mathima,a.mathc, al.onoma, al.eponimo, al.tmima, t.Name, t.sign 
 FROM apousies as a,allstudentinfo as al, teachers as t WHERE (a.apousia=1 or a.kath >=?) AND a.dateout=?
 AND a.mitroo=al.mitroo AND a.teacher=t.PMP AND (a.mitroo not in (select mitroo from apousies where kath > 0 and dateout='$dt' and a.period=period))
 ORDER BY a.dateout,a.teacher,a.period,a.mathc,al.eponimo,al.onoma " );
 $sql->execute([$aptime,$dt]);	
 }
 else
 {
$sql = $dbcon->prepare("SELECT a.dateout, a.mitroo, a.period, a.teacher,
 a.mathima,a.mathc, al.onoma, al.eponimo, al.tmima, t.Name, t.sign 
 FROM apousies as a,allstudentinfo as al, teachers as t WHERE (a.apousia=1 and a.kath IS NULL) AND a.dateout=?
 AND a.mitroo=al.mitroo AND a.teacher=t.PMP AND (a.mitroo not in (select mitroo from apousies where kath > 0 and dateout='$dt' and a.period=period)) 
 ORDER BY a.dateout,a.teacher,a.period,a.mathc,al.eponimo,al.onoma " );	 
 $sql->execute([$dt]);	 
 }
 
 $sqls = $dbcon->prepare("select s.syndid,COUNT(sc.mitroo) as cnt,s.synd from syndcodes as s, syndidaskalies as sc where s.syndid=sc.syndid group by s.synd");
 $sqls->execute();
 $syndcnt=array();
 while ($rows=$sqls->fetch(PDO::FETCH_ASSOC))
 {
	$syndcnt[$rows['syndid']]=$rows['cnt'];
 }
 
 
 
echo "<table class='myTable' >
<tr style='border:1px solid black' >
<tr>
<th>ΚΑΘΗΓΗΤΗΣ</th>
<th>ΠΕΡ.</th>
<th>ΜΑΘΗΜΑ</th>
<th>ΌΝΟΝΑΤΕΠΩΝΥΜΟ ΜΑΘΗΤΗ / ΤΡΙΑΣ</th>
<th>ΣΧΟΛΙΑ</th>
<th>ΥΠΟΓΡΑΦΗ</th>
</tr>";

$row=$sql->fetch(PDO::FETCH_ASSOC);
while(!empty($row)) 
{
	  $teach=$row['Name'];
	  $pmpt=$row['teacher'];
	  $sign=$row['sign'];
      $oldt=$teach;
	  $sqls=mysqli_query($con,"SELECT posted FROM apousiologio WHERE date='$dt' AND teacher='$pmpt' ");
	  $rows= mysqli_fetch_array($sqls);
	  $q = mysqli_query($con,"SELECT id FROM dailytab WHERE Teacher='$teach'");
      $rowx = mysqli_fetch_array($q);
      $id1=$rowx["id"]+1;
	  //$sign="POSTED";
	  echo "<tr>";
	  echo "<td colspan='4'>".$teach." (".$pmpt." )" . "</td>";
	  echo "</tr>";
	  $subject="";
	  while ($oldt==$teach)
	  {
		 $mathima=$row['mathima'];
		 $cc=0;
		 
		 while ($mathima==$row['mathima'])
		 {
		 $cc++;	
	     $mit=$row['mitroo'];
	     $tmima=$row['tmima'];
	     $onoma="( ".$mit." ".$tmima." "." ) ".$row['eponimo']." ".$row['onoma'];
	     $per=$row['period'];
		 $mathc=$row['mathc'];
		 if (empty($mathc))
		 {
		 //................
			 $dtab="day".$per;
			 $q = mysqli_query($con,"SELECT ".$dtab." FROM dailytab WHERE id='$id1'");
			 $rowx = mysqli_fetch_array($q);
			 $d1=$rowx["$dtab"];
			 $arr=explode("/",$d1);
			 $subject=$arr[1];
		 }
		 else
		 {
		 	switch ($mathc) 
			{
               case 'Α':
			   case 'Σ':
			   $subject = 'ΕΠΙΒΛΕΨΗ';
               break;
               case 'Δ':$subject = 'Δ.Δ.Κ';
               break;
			}
         } 
	  
   
		 //.......
		 
		 echo "<tr>";
		 echo "<td>". " " . "</td>"; 
	     echo "<td>". $per . "</td>";
	     echo "<td>". $mathima."/".$subject . "</td>";
	     echo "<td>". $onoma."</td>";
		 if (isset($sekd[$mit][$per]))
		 {
		     echo "<td>". $sekd[$mit][$per]."</td>";
		 }
		 else
		 {
		     echo "<td>". " "."</td>";	 
		 }
	     echo "</tr>";
		 $row=$sql->fetch(PDO::FETCH_ASSOC);
		 }
		 $teach = $row['Name'];
		 
		 //echo $cc;
      }
	  echo "<tr>";
	  echo "<td colspan='5'>". " " . "</td>";
	  $posted=$rows['posted'];
	  $sign1="signs/".$sign;
	 // if ($posted<>1){$sign="Δεν καταχωρήθηκε";}
	  ?>
	  <td > <img src=<?php echo $sign1;?>  /> </td>
	  <?php
	  echo "</tr>";
}
echo "</table>";
 
}

?>

</body>
</html>