<?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'];

?><p style='font-size:16px;'>ΑΝΑΦΟΡA ΗΜΕΡIΣΙΩΝ ΑΠΟΥΣΙΩΝ: <?php echo date('d-m-Y', strtotime($dt));?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button onClick="window.print()">Εκτύπωση Αναφοράς Απουσιών</button></p><?php

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

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

    $tnames = array();
	$i=1;
	while ($row=$sql->fetch(PDO::FETCH_ASSOC)) 
	{
       $tnames[$i] = rtrim(ltrim($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=array();
while($row = mysqli_fetch_array($sql))
{
	$teacher=$row['pmp'];
	$flg=0;
	for ($j=1;$j<=$gym;$j++) {
	    $d=$day.$j;
		$dtab="day".$j;
	    if (!empty($row["$dtab"])) $flg=1; }
	
	if ($flg == 1) {
		$teach[$i]=rtrim(ltrim($teacher));
		$i++;
	}
	$row = mysqli_fetch_array($sql);
}
$limit=$i-1;
$noapousies=array_values(array_filter(array_map('trim', array_diff($teach,$tnames))));


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

$i=1;
foreach($noapousies as $nap) 
{
	$sql = $dbcon->prepare("SELECT Name FROM teachers WHERE PMP=? ");
	$sql->execute([$nap]);
	$row=$sql->fetch(PDO::FETCH_ASSOC);
	$nm=$row['Name'];
	if (!empty($nm))
	{
	echo $i." ".$nm."<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=mysqli_query($con,"update apousies set apousia=1, kath=NULL where kath > '$aptime' and dateout='$dt' ");
	
 }

$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 " );	 
 $sql->execute([$dt]);	 

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 bgcolor='#ddd'>";
	  echo "<td colspan='6'>"."<b>".$teach." (".$pmpt." )" . "</b></td>";
	  echo "</tr>";
	  $subject="";
	  while ($oldt==$teach)
	  {
		 $mathima=$row['mathima'];
	     $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>";
		 //////
		 $cov=mysqli_query($con, "select * from spiticovid where mitroo='$mit'  ");
	     $cv=mysqli_num_rows($cov);
	     $dr='';
         if ($cv > 0)
	     {
			$rowc=mysqli_fetch_array($cov);
			$dout=$rowc['dateout'];
			$din=$rowc['dateback'];
			$r=$rowc['reason'];
			if (($dt >= $dout and $dt < $din) )
			{
				{
					switch ($r) 
					{
						case 1:
							$dr='ΚΡΟΥΣΜΑ';
							break;
						case 2:
							$dr='ΕΠΑΦΗ';
							break;
						case 3:
							$dr='Ε-ΟΜΑΔΑ';
							break;
					}
				}
			}
			$sekd[$mit][$per]=$dr;
	     }
	    
		 
		 //////
		 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 "<tr>";
	  echo "<td colspan='5'>". " " . "</td>";
	  //$posted=$rows['posted'];
	  $url="signs/".$sign;
	 
       if (@getimagesize($url)) 
       {
            $image = base64_encode(file_get_contents($url));
	  
	  //if ($posted<>1){$sign="Δεν καταχωρήθηκε";}
	  ?>
	  
	  <td > <img src="data:image/x-icon;base64,<?= $image ?>"> </td>
	  <?php
      } else {
         ?>
	  
	  <td >  </td>
	  <?php
      }
      
	  echo "</tr>";
	  
}
echo "</table>";
 
}

?>

</body>
</html>