<?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 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();
 $tmcnt=array();
 while ($rows=$sqls->fetch(PDO::FETCH_ASSOC))
 {
	$tmcnt[$rows['synd']]=$rows['cnt'];
 }

 $sqls1 = $dbcon->prepare("select tmima,COUNT(tmima) as cnt1 from allstudentinfo group by tmima");
 $sqls1->execute();
 
 while ($rows1=$sqls1->fetch(PDO::FETCH_ASSOC))
 {
	$tmcnt[$rows1['tmima']]=$rows1['cnt1'];
 }

 
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="";
	  $mm=1;
	  while ($oldt==$teach)
	  {
		 $mathima=$row['mathima'];
		 $mathc=$row['mathc'];
		 $cc=0;
		 $onomax=$perx=array();
		 while ($mathima==$row['mathima'] and $oldt==$row['Name'] and  $mathc==$row['mathc'])
		 {
			 $cc++;	
			 $mit[$cc]=$row['mitroo'];
			 $tmima=$row['tmima'];
			 $onoma="( ".$mit[$cc]." ".$tmima." "." ) ".$row['eponimo']." ".$row['onoma'];
			 $per=$row['period'];
			 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;
				}
			 } 
		  
			 $onomax[$cc]=$onoma;
			 $perx[$cc]=$per;
			 $row=$sql->fetch(PDO::FETCH_ASSOC);
			 
		 }
		 $teach = $row['Name'];
		 
		 if ($tmcnt[$mathima]==$cc)
		 {
			 echo "<tr>";
			 //echo "<td>". " " . "</td>"; 
			 echo "<td colspan='2' align='center'>". $per . "</td>";
			 echo "<td>".$mathima."/".$subject."</td>";
			 echo "<td>"." ΟΛΟΙ ΑΠΟΝΤΕΣ "."</td>";
			 echo "</tr>";
		 }
		 else
		 {
			 for ($j=1;$j<=$cc;$j++)
			 {
				 echo "<tr>";
				 //echo "<td>". $mm. "</td>"; 
				 echo "<td colspan='2' align='center'>". $perx[$j] . "</td>";
				 echo "<td>". $mathima."/".$subject . "</td>";
				 echo "<td>". $onomax[$j]."</td>";
				 if (isset($sekd[$mit[$j]][$perx[$j]]))
				 {
					 echo "<td>". $sekd[$mit[$j]][$perx[$j]]."</td>";
				 }
				 else
				 {
					 echo "<td>". " "."</td>";	 
				 }
				 echo "</tr>"; 
			 }
		 }
		 
      }
	  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>