<?php
include "scrinit.php";
$fn=$_GET['fn'];
if ($fn == 0 and is_numeric($fn)) 
{ 
  include "logo.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'];
$daymap = array('Κυριακή', 'Δευτέρα', 'Τρίτη', 'Τετάρτη', 'Πέμπτη', 'Παρασκευή', 'Σάββατο');
$datedisp= date('d-m-Y', strtotime($dt)); 
$daytext = $daymap[date('w', strtotime($dt))];
$day = date('D', strtotime($dt));

?>
<h3>ΑΝΑΠΛΗΡΩΣΕΙΣ / ΑΛΛΑΓΕΣ - <?php echo $daytext." ( ".$datedisp." )"; ?> </h3>
<?php
//**************************** display efimerevontes
$dayper=$day."0";
$sqlv=mysqli_query($con, "SELECT e.pmp,t.Name  FROM efimeries as e,teachers as t  WHERE e.pmp=t.PMP and dayper='$dayper' ");
$rowv=mysqli_fetch_array($sqlv);
$vdname=$rowv['Name'];
echo "<table  style='width:300px'class='myTable' >
<tr style='border:1px solid black' bgcolor='#66CC99' >
<th>ΕΦΗΜΕΡΕΥΩΝ ΒΟΗΘΟΣ</th> </tr>";
echo "<tr>";
echo "<td>"."<b>"."<p style='font-size:16px;'>".$vdname."</p>";"</b>"."</td>";
echo "</tr>"."</table>";
//**************************************
echo "<br>"."<b>"."ΕΦΗΜΕΡΕΥΟΝΤΕΣ/ΟΥΣΕΣ ΠΕΡΙΟΔΟΥ"."</b>"."<br>";

$sqlv=$dbcon->prepare("SELECT e.*,t.Name  FROM efimeries as e,teachers as t  WHERE dayper LIKE ?  and e.pmp=t.PMP  order by e.dayper ASC");
$dp=$day.'%';
$sqlv->execute([$dp]);
while($rowv=$sqlv->fetch(PDO::FETCH_ASSOC))
{
	$dx=$rowv['dayper'];
	$per=substr($dx, -1);
	$vdname=$rowv['Name'];
	$ef[$per]=$vdname;
}

echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<td ></td>
<td>$daytext</td>
</tr>";
for ($k=0;$k<=$gym;$k++)
{
   echo "<tr>";
   echo "<td >".$k."</td>";
   echo "<td>".$ef[$k]."</td>";
   }
echo "</table>";



$idapon=$pmpapon=$kathapon=array();

$sql=mysqli_query($con,"SELECT t.id,t.pmp,t.Teacher FROM timetable as t,apousieskath as a where t.pmp=a.pmp and a.datefrom='$dt' ");
$i=0;
while($row=mysqli_fetch_array($sql))
{
	$i++;
	$idapon[$i]=$row['id'];
	$pmpapon[$i]=$row['pmp'];
	$kathapon[$i]=$row['Teacher'];
}
$tk=$i;
$tidx=array();
if ($tk > 0)
{
echo "<br>";
echo "<br>"."<b>"."ΚΑΘΗΓΗΤΕΣ/ΤΡΙΕΣ ΠΟΥ ΑΠΟΥΣΙΑΖΟΥΝ - ΑΝΑΠΛΗΡΩΤΕΣ/ΤΡΙΕΣ"."</b>"."<br>";
//echo "<br>";
echo "<table style='width:800px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#66CC99' >
<th>Α/Α</th>
<th>Εκπαιδευτικός</th>";
for ($i=1;$i<=$gym;$i++)
{
	$titep="Περίοδος ".$i;
	echo "<th>".$titep."</th>";
}
echo "</tr>";
$jj=1; 
for ($ka=1;$ka<=$tk;$ka+=2)
{
   $fil=array();
   echo "<tr>";//first row
   $jj++;
   echo "<td>".$jj. "</td>";
   echo "<td>"."<font face='arial' size='2pt'>".$kathapon[$ka]." (".$idapon[$ka].")"."</td>";
   for ($i=1;$i<=$gym;$i++)
   {
	    $dayper=$day.$i;
		$sql=mysqli_query($con,"SELECT ".$dayper.",id FROM timetable where id='$idapon[$ka]' ");
		$row=mysqli_fetch_array($sql);
		$tmi=$row["$dayper"];
		if (!empty($tmi))
		{
		$fil[$i]=1;
		$id=$row['id'];
		$nid=$id+1;
		$res1 = mysqli_query($con,"SELECT ($dayper) FROM timetable WHERE id='$nid'  " ); 
		$row3 = mysqli_fetch_array($res1); 
		$ar= explode("/",$row3["$dayper"]);
		$room=$ar[0];
	    echo "<td>" ."<font face='arial' size='2pt'>"."<b>". $row["$dayper"]."</b>"."(".$room.")". "</td>";	
		}
		else {
			$fil[$i]=0;
			echo "<td>" ." ". "</td>";
		}		
   }
   echo "</tr>";
   echo "<tr>"; echo "<td>". "   " . "</td>"; echo "<td>". "   " . "</td>";
   //second row
   $sql=mysqli_query($con, "SELECT t.Teacher,a.tid,a.period,a.pmpapon FROM timetable as t,anaplirosi as a  
		      WHERE a.aponid=t.id and a.aponid='$idapon[$ka]' and date='$dt' ");
   $taid=array();
   while($row=mysqli_fetch_array($sql))
   {
	  $er=$row['pmpapon'];
	  if (empty($er) or $er=='')
	  {
	    $p=$row['period'];
	    $taid[$p]=$row['tid'];
	  }
   }
   for ($i=1;$i<=$gym;$i++)
   {

		 if ($taid[$i] > 0 and $taid[$i] < 999 )
		 {
		   $dayper=$day.$i;
		   $sql=mysqli_query($con,"SELECT Teacher FROM dailytab where id='$taid[$i]' ");
		   $row=mysqli_fetch_array($sql);
		   echo "<td>"."<b>"."<font face='arial' size='2pt'>".$row['Teacher']."</b>"."</td>"; 
		 }
		 elseif ($taid[$i]==999)
		 {
		    echo "<td>"."<b>"."<font face='arial' size='2pt'>"."ΣΤΟ ΤΜΗΜΑ ΤΟΥΣ"."</b>"."</td>";
		 }
		 elseif ($taid[$i]=="!")
		 {
			echo "<td>"."<b>"."<font face='arial' size='2pt'>"."ΠΑΡΩΝ/ΟΥΣΑ"."</b>"."</td>"; 
		 }
		 elseif ($taid[$i]=="ΔΔΚ")
		 {
			echo "<td>"."<b>"."<font face='arial' size='2pt'>"."ΕΚΔΗΛΩΣΗ"."</b>"."</td>"; 
		 }
		 else
		 {
		   if($fil[$i]==1)
		   {
			echo "<td>"."<b>"."<font face='arial' size='2pt'>"."ΑΛΛΑΓΗ"."</b>"."</td>";   
		   }
		   else
		   {
		   echo "<td>"."<b>"."<font face='arial' size='2pt'>"." "."</b>"."</td>"; 	 
		 } }
		 
	}
    echo "</tr>"; 
	
}
echo "</table>";
//..........***************** 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;$room=array();
while($row=$result->fetch(PDO::FETCH_ASSOC))
{
	 $tp=$row['toper'];
	 $last=trim($row['last']);
	 $ch = $row['chwith'];
	 $kath=$row['idteacher'];
	 $room[$i]=$row['room'];
	 if (!($tp==$gym and in_array($kath,$tidx)))
	 {
		 
	 $toperiod[$i]=$row['toper'];
	 $kathigitis[$i]=$kath;
	 $fromperiod[$i]=$row['fromper'];
	 $dd=$day.$fromperiod[$i];
	 $resx = $dbcon->prepare("SELECT ".$dd." FROM timetable WHERE id=?");
	 $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 timetable 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'];
		 $dd=$day.$fromperiod[$i];
		 $resx = $dbcon->prepare("SELECT ".$dd." FROM timetable WHERE id=?");
	     $resx->execute([$kathigitis[$i]]);
	     $rowx=$resx->fetch(PDO::FETCH_ASSOC);
	     $tmx[$i]=$rowx["$dd"];
	     $less=$tmx[$i];
		 $i++; 
	 }
	}
	
}	
$allagi=$i-1;
//*************************************************************************
echo "<br>";
echo "<br>"."<b>"."ΑΛΛΑΓΕΣ ΠΡΟΓΡΑΜΜΑΤΟΣ"."</b>";
echo "<br>";

echo "<table style='width:600px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73' >
<th>Α/Α</th>
<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 * 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'];
	 $tmima[$i]=$row['tmima'];
	 $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>";
if(empty($ktapo[$i]))
{
	echo "<td>"."<font face='arial' size='3pt'>".$tmima[$i]."</td>";
	echo "<td>"."<font face='arial' size='3pt'>".$roomam[$i]. "</td>";
	echo "<td>"." ". "</td>";
}
else
{
	$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>";
}
if ($fn == 0 and is_numeric($fn)) 
{ 

?>	
<br /><br /><br />
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού  </button> </a>
</body>
</html>
<?php  
} 

