<?php
include "scrinit.php";
include 'logo.php';
?>
<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>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function popupCenter(pageURL, title, w, h) {
    var left = (screen.width / 2)  - (w / 2);
    var top  = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=900, height=300, top='+top+', left='+left);
}
</script>
</head>
<body>
<h4> ΑΠΟΥΣΙΕΣ ΠΡΟΣ ΔΙΕΡΕΥΝΗΣΗ</h4>
<?php
$dt=$_SESSION['dt'];
$day = date('D', strtotime($dt));
$gym=$_SESSION['gym'];
$usr=$_SESSION['usr'];
$ac=$_SESSION['access'];
if ($ac==4)
{	
require "allagiprog.php";
$sqlz = $dbcon->prepare("select count(a.period) as cp,a.mitroo,t.Name,al.tmima,al.eponimo,al.onoma
from apousies as a,allstudentinfo as al,voithos as v,teachers as t where (a.dateout=? and a.apousia=1 and a.mitroo > 0 
and al.mitroo=a.mitroo and v.Class_id=al.tmima and t.PMP=v.voi_id ) group by mitroo having count(period) < 7 order by tmima, mitroo DESc "); 
$sqlz->execute([$dt]);
}
else
{
require "allagiprog.php";
$sqlz = $dbcon->prepare("select count(a.period) as cp,a.mitroo,al.tmima,al.eponimo,al.onoma,t.Name
from apousies as a,allstudentinfo as al,voithos as v,teachers as t where (a.dateout=? and a.apousia=1 and a.mitroo > 0 
and al.mitroo=a.mitroo and v.Class_id=al.tmima and t.PMP=v.voi_id and v.voi_id=?) group by mitroo having count(period) < 7 order by mitroo DESc "); 	
$sqlz->execute([$dt,$usr]);
}

$nr=$sqlz->rowCount();
if ( $nr> 0)
{
echo "Για εντοπισμό μαθητή/τριας να κάνετε κλικ στην αντίστοιχη περίοδο";	
$apouall=array();
echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>Α/Α</th>
<th>Α.Μητρ</th>
<th>Τμήμα</th>
<th>Αρ. Περ</th>
<th>ΒΔ</th>
<th>Όνομα</th>";
for ($i=1;$i<=$gym;$i++)
{
	$titep="Περ".$i;
	echo "<th>".$titep."</th>";
}
echo "</tr>";

$cc=1;
while($row=$sqlz->fetch(PDO::FETCH_ASSOC)) 
{
	$per1=$per2=array();
	$mitroo=$row['mitroo'];
	$tmima=$row['tmima'];
	$stname =$row['eponimo']." ".$row['onoma'];
	$bd=$row['Name'];
	$cp=$row['cp'];
	
	$sqlx = $dbcon->prepare("SELECT a.period FROM apousies as a WHERE a.apousia=1 AND a.dateout=? AND 
    a.mitroo=? AND (a.mitroo not in (select mitroo from apousies where kath > 0 and dateout=? and a.period=period )) 
    ORDER by a.period " );
    $sqlx->execute([$dt,$mitroo,$dt]);
	$count=$sqlx->rowCount();
	if ($count > 0)
	{	
	echo "<td>". $cc . "</td>";
	echo "<td>". $mitroo. "</td>";
	echo "<td>". $tmima. "</td>";
	echo "<td>". $cp. "</td>";
	echo "<td>". $bd. "</td>";
	echo "<td>". $stname. "</td>";   
	  while ($rowx=$sqlx->fetch(PDO::FETCH_ASSOC))
	  {
	     $per1[$rowx['period']]=$rowx['period'].$mitroo;
		 
      }
    //.....elexos gia ekdiloseis tin parousa hmerominia
    $q=$dbcon->prepare("SELECT e.onoma,s.* FROM ekdilosi as e,ekdilosistu as s 
	                    WHERE e.date = ? and e.code=s.code and s.pmp=?");
    $q->execute([$dt,$mitroo]);
    $ce=$q->rowCount();
	if ($ce > 0 ) 
	{
      $row2=$q->fetch(PDO::FETCH_ASSOC);
	  $onomaekd=$row2['onoma'];  
	}
	else
	{
	  $onomaekd="";	
	}
    for ($i=1;$i<=$gym;$i++)
	{
		 if ($per1[$i] < 1)
		 { 
			$per2[$i]="  ";
		 }
	     else 
		 { 
		    $per2[$i]="?";
		 }
		 if ($ce > 0 ) 
		 {
			  $p="p".$i;  
			  if ($row2["$p"]==1) {$per2[$i]="***"; }
		 }
		
		?>
		 <td>
         <a href="#" onClick="popupCenter('locate2.php?per=<?php echo $per1[$i];?>')"><?php echo $per2[$i]; ?></a>
         </td>
         <?php 
	}
	 echo "<td>". $onomaekd."</td>";
     echo "</tr>";
	 
	 $cc++;
	}	 
}
echo "</table>";	 
}
else
{
	echo "<br>"."ΔΕΝ ΥΠΑΡΧΟΥΝ ΑΠΟΥΣΙΕΣ";
}	

?>
<br><br>
<button onClick="window.print()">ΕΚΤΥΠΩΣΗ ΟΘΟΝΗΣ</button>
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού  </button> </a>
</body>
</html>