<?php 
include "scrinit.php";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kathisteriseis</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>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function() {
 $("#tmimaid").bind("change", function() {
     $.ajax({
         url: "change.php",
         data: "tmimaid="+$("#tmimaid").val(),
         success: function(html) {
             $("#nameid").html(html);
         }
     });
 }); 
});

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=800, height=900, top='+top+', left='+left);
}
</script>
</head>
<body>
<?php 

require("dbconnect.php");
$usr=$_SESSION['usr'];
$name=$_SESSION['n'];
$dt=$_SESSION['dt'];
$tmima=$mitr=$nam=$cm=array();


$sql1 = $dbcon->prepare("SELECT Class_id FROM voithos WHERE voi_id=? ORDER BY Class_id  " );
$sql1->execute([$usr]);
$c=$sql1->rowCount();
if ($c > 0)	
{

	//****skastoi
	$day = date('D', strtotime($dt));
    if ($day=="Mon")
    {
		$dbf = date( 'Y-m-d', strtotime( $dt . ' -3 day' ) );
    }
    else 
    { 
		$dbf = date( 'Y-m-d', strtotime( $dt . ' -1 day' ) );
	}
	
	$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 a.mitroo having count(a.period) < 7 order by a.mitroo DESc "); 	
	$sqlz->execute([$dbf,$usr]);

	$nr=$sqlz->rowCount();
	if ( $nr> 0)
	{
	echo "<br>"."<b>"."ΥΠΑΡΧΟΥΝ ΧΤΕΣΙΝΕΣ ΑΠΟΥΣΙΕΣ ΠΡΟΣ ΔΙΕΡΕΥΝΗΣΗ"."</b>";
	$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([$dbf,$mitroo,$dbf]);
		$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([$dbf,$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]="***"; }
			 }
			
			 echo "<td>".$per2[$i]."</td>";
			
		}
		 echo "<td>". $onomaekd."</td>";
		 echo "</tr>";
		 
		 $cc++;
		}	 
	}
	echo "</table>";	 
	}
$tmimaz=$tm=array();
//***** ΚΑΘΥΣΤΕΡΗΣΕΙΣ
$j=0;
	while ($row1=$sql1->fetch(PDO::FETCH_ASSOC))
	{
	  $j++;
	  $tmimaz[$j]=$row1['Class_id']; 
	}	 
$k=0;$jk=$j;
for ($i=1;$i<=$j;$i++)
{
  $sql = mysqli_query($con,"select count(a.mitroo) as cm, a.mitroo,al.eponimo,al.onoma 
  from allstudentinfo as al,apousies as a WHERE  (a.kath > 0 AND a.timoriacode = 0) 
  and a.mitroo=al.mitroo and al.tmima='$tmimaz[$i]' group by a.mitroo having count(a.mitroo) >=4 " );
  $cx=mysqli_num_rows($sql);
  if ($cx > 0)
  {
    while($row = mysqli_fetch_array($sql))
	{
			 $k++;
			 $cm[$k]=$row['cm'];
			 $mitr[$k]=$row['mitroo'];
			 $nam[$k]=$row['eponimo']." ".$row['onoma'];
			 $tm[$k]=$tmimaz[$i];
	}
  }
}
	echo "<br>"."<b>"."ΥΠΑΡΧΟΥΝ ΑΤΙΜΩΡΗΤΕΣ ΚΑΘΥΣΤΕΡΗΣΕΙΣ"."</b>";
	echo "<table class='myTable' >
	<tr style='border:1px solid black' bgcolor='#d6ff73'  >
	<th>Μητρώο</th><th>Τμήμα</th><th>Ονοματεπώνυμο</th>	<th>Αρ. Καθ.</th>";
	for ($i=1;$i<=$k;$i++)
	{
		echo "<tr>";
		?>
		<td>
		<a href="#" onClick="popupCenter('timoria1.php?am=<?php echo $mitr[$i]; ?>')"><?php echo $mitr[$i]; ?></a>
		</td>
		<?php
		echo "<td>".$tm[$i]."</td>";
		echo "<td>".$nam[$i]."</td>";
		echo "<td>".$cm[$i]."</td>";
		echo "</tr>";		  
	}
	echo "</table>";

//*********************** KATAGGELIES
$sql = mysqli_query($con,"select k.aranaforas,k.datekata,k.mitroo,a.eponimo,a.onoma,k.tmima,t.Name 
       from kataggelia as k,allstudentinfo as a,teachers as t where k.bd='$usr' 
       and a.mitroo=k.mitroo and k.katastasi<2  and k.kataggelon=t.PMP 
	   order by k.datekata,k.tmima,k.mitroo desc" );
$c= mysqli_num_rows($sql);
if ($c > 0)	
{	
echo "<br>"."<b>"."ΥΠΑΡΧΟΥΝ ΑΔΙΕΚΠΕΡΑΙΩΤΕΣ ΚΑΤΑΓΓΕΛΙΕΣ"."</b>";
echo "<table class='myTable' >
	<tr style='border:1px solid black' bgcolor='#d6ff73'  >
	<th>Αρ. Αναφοράς</th><th>Τμήμα</th><th>Μητρώο</th>	<th>Ονοματεπώνυμο</th>	<th>Ημερομηνία</th><th>Καταγγέλων</th> ";

while($row = mysqli_fetch_array($sql))
{

			echo "<tr>";
			$aranaforas=$row['aranaforas'];
			echo "<td>" . "<a href=\"paraptoma2.php?data=".$aranaforas."/"."1"."\">"."<b>".$aranaforas."</b>". "</a>". "</td>";
			echo "<td>".$row['tmima']."</td>";
			echo "<td>".$row['mitroo']."</td>";
			echo "<td>".$row['eponimo']." ".$row['onoma']."</td>";
			echo "<td>".$row['datekata']."</td>";
			echo "<td>".$row['Name']."</td>";
			echo "</tr>";
}
  
echo "</table>";
}
$k=0;$mitr=array();
for ($i=1;$i<=$jk;$i++)
{
$sql = mysqli_query($con,"SELECT a.dateout, a.mitroo, a.period,a.teacher,al.onoma,al.eponimo,t.Name
 FROM apousies as a,allstudentinfo as al, teachers as t 
 WHERE a.stoli=1 AND a.dateout='$dt' AND al.tmima='$tmimaz[$i]' AND a.mitroo=al.mitroo and t.PMP=a.teacher 
 ORDER BY a.mitroo,a.period " );
$cx=mysqli_num_rows($sql);
if ($cx > 0)
{
    while($row = mysqli_fetch_array($sql))
	{
						 //$cm[$k]=$row['cm'];
			 //$mitr[$k]=$row['mitroo'];
			 //$nam[$k]=$row['eponimo']." ".$row['onoma'];
			 //$tm[$k]=$tmimaz[$i];
			$mitr[$row['mitroo']]=$row['mitroo'];
			$perx[$row['mitroo']][$row['period']]=1;
			$tmimax[$row['mitroo']]=$tmimaz[$i];
			$namex[$row['mitroo']]=$row['eponimo']." ".$row['onoma'];
			$tnamex[$row['mitroo']][$row['period']]=$row['Name'];
	}
  }

}
if ($cx > 0)	
{	
echo "<br>"."<b>"."ΥΠΑΡΧΟΥΝ ΜΑΘΗΤΕΣ ΧΩΡΙΣ ΣΤΟΛΗ"."</b>";	

echo "<table class='myTable' >
	<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>Αρ.M</th>
<th>Τμήμα</th>
<th>Όνομα</th>";
for ($i=1;$i<=$gym;$i++)
{
	$titep="Περ".$i;
	echo "<th>".$titep."</th>";
}
echo "</tr>";

$day = date('D', strtotime($dt));
foreach($mitr as $mitrx)
{
	
	echo "<tr>";
	echo "<td>". $mitrx . "</td>";
	echo "<td>". $tmimax[$mitrx] . "</td>";
	echo "<td>". $namex[$mitrx]. "</td>";
	for ($i=1;$i<=$gym;$i++)
	{
		$getf=$day.$i;
		$sql = mysqli_query($con,"SELECT ".$getf." FROM stutimetable WHERE id IN (SELECT ID+1 FROM stutimetable WHERE mitroo='$mitrx')");
		$row = mysqli_fetch_array($sql);
		$x=explode("/",$row["$getf"]);
		$xx=$tnamex[$mitrx][$i];
		if(!empty($xx) or $xx <> ""){$xx=$xx."( ".$x[0]." )";}
		echo "<td>". $xx."</td>";		
	}
	echo "</tr>";
}

echo "</table>";	 
}
}
?>

</body>
</html>