<?php
include "scrinit.php";
?>
<head>
<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<head>

<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:12px; }
<p style="font-size:12px;"></p>
</style>
</head>
<body>
<?php
require "dbconnect.php";
$gym=$_SESSION['gym'];
$dt=$_SESSION['dt'];
//..........***************** anapliroseis 
$tidx=array();
$result = $dbcon->prepare("SELECT * FROM anaplirosi WHERE teacher > '' and date=? ORDER BY period");
$result->execute([$dt]);
$c=$result->rowCount();
if ($c > 0)
{
echo "<br>"."<b>"."ΑΝΑΠΛΗΡΩΣΕΙΣ"."</b>";
echo "<br>";
echo "<table style='width:800px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73' >
<th>Α/Α</th>
<th>ΚΑΘΗΓΗΤΗΣ/ΤΡΙΑ</th>
<th>ΠΕΡΙΟΔΟΣ</th>
<th>TMHMA/ΜΑΘΗΜΑ</th>
<th>ΣΤΗ ΘΕΣΗ ΤΟΥ/ΤΗΣ</th>
<th>ΚΑΤΑΣΤΑΣΗ</th>
</tr>";

$sqla=$dbcon->prepare("SELECT Teacher FROM dailytab WHERE pmp=? ");
$sql=$dbcon->prepare("SELECT Teacher FROM dailytab WHERE id=? ");
$i=1;
while($row=$result->fetch(PDO::FETCH_ASSOC))
{
	 echo "<tr>";
	 echo "<td>".$i."</td>";
	 $tidx[$i]=$row['aponid'];
	 $apmp[$i]=$row['teacher'];
	 $sqla->execute([$apmp[$i]]);
	 $rowa=$sqla->fetch(PDO::FETCH_ASSOC);
	 
	 $sql->execute([$tidx[$i]]); 
	 $rowt=$sql->fetch(PDO::FETCH_ASSOC);
	 $apon=$rowt['Teacher'];
	 echo "<td>"."<b>"."<font face='arial' size='3pt'>".$rowa['Teacher']."</b>"."</td>";
	 echo "<td>"."<b>"."<font face='arial' size='3pt'>".$row['period']."</b>"."</td>";
	 echo "<td>"."<b>"."<font face='arial' size='3pt'>".$row['mathima']."</b>"."</td>";
	 echo "<td>"."<b>"."<font face='arial' size='3pt'>".$apon."</b>"."</td>";
	 if ($row['period']==$gym)
	 {
	    echo "<td>"."<b>"."<font face='arial' size='3pt'>"."ΠΑΙΡΝΕΙ ΠΑΡΟΥΣΙΕΣ"."</b>"."</td>";	 
	 }
	 else
	 {
	    echo "<td>"."<b>"."<font face='arial' size='3pt'>".""."</b>"."</td>";	 
	 }
	 echo "</tr>";
     $i++;	 
} 
echo "</table>";
}

//..........
////////////////////////////// allages programmatos
$result = $dbcon->prepare("SELECT * FROM allages WHERE date=? ORDER BY id  ");
$result->execute([$dt]);
$c=$result->rowCount();
if ($c > 0)
{
$i=1;
while($row=$result->fetch(PDO::FETCH_ASSOC))
{
	 $tp=$row['toper'];
	 $last=trim($row['last']);
	 $ch = $row['chwith'];
	 $toperiod[$i]=$row['toper'];
	 $kathigitis[$i]=$row['idteacher'];
	 $fromperiod[$i]=$row['fromper'];
	 $dd="day".$toperiod[$i];
	 $resx = $dbcon->prepare("SELECT ".$dd." FROM dailytab WHERE id=?");
	 //$resx->execute([$kathigitis[$i]]);
	 $resx->execute([$kathigitis[$i]]);
	 $rowx=$resx->fetch(PDO::FETCH_ASSOC);
	 $tmx[$i]=$rowx["$dd"];
	 $less=$tmx[$i];
	 $room[$i]=$row['room']; 
	 if (empty($room[$i]))
	 {
		 
		 $idk=$kathigitis[$i]+1;
		 $resx = $dbcon->prepare("SELECT ".$dd." FROM dailytab WHERE id=?");
		 $resx->execute([$idk]);
	     $rowx=$resx->fetch(PDO::FETCH_ASSOC);
	     $tm=explode("/",$rowx["$dd"]);
		 $room[$i]=$tm[0];
	 }
	 $i++;

	 if ( $ch <> 0 and !in_array($ch,$tidx))
	 {
		 $toperiod[$i]=$row['fromper'];
		 $kathigitis[$i]=$row['chwith'];
		 $fromperiod[$i]=$row['toper'];
		 $tmx[$i]=$less;
		 $i++; 
	 }
	
}	
$allagi=$i-1;

echo "<br>";
echo "<br>"."<b>"."ΑΛΛΑΓΕΣ ΠΡΟΓΡΑΜΜΑΤΟΣ"."</b>";
echo "<br>";

echo "<table style='width:500px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73' >
<th>Α/Α</th>
<th>Εκπαιδευτικός</th>
<th>Τμήμα</th>
<th>Περίοδος</th>

<th>Αίθουσα</th>
</tr>";
$sqla=$dbcon->prepare("SELECT Teacher FROM dailytab WHERE id=?");
$i=1;
while($fromperiod[$i]<> 0)  
{
  echo "<tr>";
  echo "<td>".$i. "</td>";
  $sqla->execute([$kathigitis[$i]]);
  $row=$sqla->fetch(PDO::FETCH_ASSOC);
  echo "<td>"."<font face='arial' size='3pt'>".$row['Teacher']. "</td>";
  echo "<td>"."<font face='arial' size='4pt'>".$tmx[$i]. "</td>";
  echo "<td>"."<font face='arial' size='4pt'>".$toperiod[$i]. "</td>";
  //echo "<td>"."<font face='arial' size='3pt'>".$fromperiod[$i]. "</td>";
  echo "<td>"."<font face='arial' size='3pt'>".$room[$i]. "</td>";
  $i++;
  echo "</tr>"; 
} 
echo "</table>";
}

////////////////////// amoivees allages
$ktse=$teacherse=$teacherapo=$roomam=$ktapo=$amper=array();

$result1 = $dbcon->prepare("SELECT idtse,idtapo,period,room,id FROM amivallages WHERE date=? ORDER BY id  ");
$result1->execute([$dt]);
$c=$result1->rowCount();
if ($c > 0)
{
$i=1;
while($row=$result1->fetch(PDO::FETCH_ASSOC))
{
	 $ktse[$i]=$row['idtse'];
	 $ktapo[$i]=$row['idtapo'];
	 $amper[$i]=$row['period'];
	 $roomam[$i]=$row['room'];
	 $i++;
}	

echo "<br>";
echo "<br>"."<b>"."ΑΜΟΙΒΑΙΕΣ ΑΛΛΑΓΕΣ ΠΡΟΓΡΑΜΜΑΤΟΣ"."</b>";
echo "<br>";

echo "<table style='width:500px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73' >
<th>Α/Α</th>
<th>Εκπαιδευτικός</th>
<th>Περίοδος</th>
<th>Τμήμα</th>
<th>Αίθουσα</th>
<th>Στη θέση του/της</th>
</tr>";
$day = date('D', strtotime($dt));
$i=1;
$sqla=$dbcon->prepare("SELECT Teacher FROM timetable WHERE id=?");
while($amper[$i]<> 0)  
{
echo "<tr>";
echo "<td>".$i. "</td>";
$sqla->execute([$ktse[$i]]);
$row=$sqla->fetch(PDO::FETCH_ASSOC);
echo "<td>"."<font face='arial' size='3pt'>".$row['Teacher']. "</td>";
echo "<td>"."<font face='arial' size='3pt'>".$amper[$i]."</td>";
$dayper=$day.$amper[$i];
$sqlb=$dbcon->prepare("SELECT Teacher,".$dayper." FROM timetable WHERE id=?");
$sqlb->execute([$ktapo[$i]]);
$rowb=$sqlb->fetch(PDO::FETCH_ASSOC);
$les=$rowb["$dayper"];
echo "<td>"."<font face='arial' size='3pt'>".$les. "</td>";
echo "<td>"."<font face='arial' size='3pt'>".$roomam[$i]. "</td>";
echo "<td>"."<font face='arial' size='3pt'>".$rowb['Teacher']. "</td>";
 $i++;
 echo "</tr>"; 
} 
echo "</table>";
}
//////////////////// allagi aithousas
$ktse=$teacherse=$teacherapo=$roomam=$ktapo=$amper=array();
$result1 = $dbcon->prepare("SELECT idteacher,period,seroom,aporoom,id FROM allagiroom WHERE date=? ORDER BY id  ");
$result1->execute([$dt]);
$c=$result1->rowCount();
if ($c > 0)
{
$i=1;
while($row=$result1->fetch(PDO::FETCH_ASSOC))
{
	 $idx[$i]=$row['id'];
	 $ktse[$i]=$row['idteacher'];
	 $period[$i]=$row['period'];
	 $seroom[$i]=$row['seroom'];
	 $aporoom[$i]=$row['aporoom'];
	 $i++;
}	

echo "<br>";
echo "<br>"."<b>"."ΑΛΛΑΓΕΣ ΑΙΘΟΥΣΩΝ"."</b>";
echo "<br>";
echo "<table style='width:500px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73' >
<th>Α/Α</th>
<th>Εκπαιδευτικός</th>
<th>Περίοδος</th>
<th>Τμήμα</th>
<th>Απο Αίθουσα</th>
<th>Σε Αίθουσα</th>
</tr>";
$day = date('D', strtotime($dt));
$i=1;
while($period[$i]<> 0)  
{
echo "<tr>";
echo "<td>".$i. "</td>";
$dayper=$day.$period[$i];
$sqla=$dbcon->prepare("SELECT id,Teacher,".$dayper." FROM timetable WHERE id=?");
$sqla->execute([$ktse[$i]]);
$row=$sqla->fetch(PDO::FETCH_ASSOC);
echo "<td>"."<font face='arial' size='3pt'>".$row['Teacher']. "</td>";
echo "<td>"."<font face='arial' size='3pt'>".$period[$i]."</td>";
$les=$row["$dayper"];
$nextid=$row['id']+1;
$sqlb=$dbcon->prepare("SELECT ".$dayper." FROM timetable WHERE id=?");
$sqlb->execute([$nextid]);
$rowb=$sqlb->fetch(PDO::FETCH_ASSOC);
$ar=explode("/",$rowb["$dayper"]);
$aporoom=$ar[0];
echo "<td>"."<font face='arial' size='3pt'>".$les. "</td>";
echo "<td>"."<font face='arial' size='3pt'>".$aporoom. "</td>";
echo "<td>"."<font face='arial' size='3pt'>".$seroom[$i]. "</td>";
$sql=$dbcon->prepare("update allagiroom set aporoom=? where id=?");
$sql->execute([$aporoom,$idx[$i]]);
 $i++;
 echo "</tr>"; 
} 
echo "</table>";
}
?>	
<br /><br /><br />
</body>
</html>