<?php
include "scrinit.php";
include "logo.php";
?>

<head>
<html>
<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:2px;font-size:12px; }
<p style="font-size:12px;"></p>
</style>
</head>

<body>

<?php
$dt=$_SESSION['dt'];
$gym=$_SESSION['gym'];
require "dbconnect.php";

if (isset($_POST['Submitanapl'])) 
{
$lesson1=$kath1=$anaplir1=array();
$anaplir1 = $_POST['anaplir'];//anaplirotes ! den apousiazoun
$lesson1 = $_POST['lesson1'];// mathima
$kath = $_POST['kath'];// kathigitis pou apousiazei
$kath1=$_POST['kathn'];
$ka=$_POST['ka1'];//arithmos aponton
$ebdomi=$_POST['ebdomi']; //mathima/tmima ebdomis
$apone=$_POST['apone']; 
$evdfrom=$_POST['evdfrom'];//poia periodos metafertike stin 7h
$anaplire=$_POST['anaplire']; //poios anaplironei
$rme=$_POST['rme']; //se poia ethousa
//********************** **********************store anapliroseis into table anaplirosi
mysqli_query($con,"DELETE FROM anaplirosi where date='$dt' ");
$tk=$ka;
$r=1;
for ($ka=1;$ka<$tk;$ka++)
{
 $kathapon=$kath[$ka];
 $sql=mysqli_query($con, "SELECT id,pmp FROM timetable WHERE pmp='$kathapon' ");
 $pmp=$row['pmp'];
 $row=mysqli_fetch_array($sql);
 $aponid=$row['id'];
 for ($i=1;$i<=$gym;$i++)
   {
	  $les=$lesson1[$ka][$i];
	  if ($les <> "")
	  {
	    $tid=$anaplir1[$ka][$i];
		echo $tid;
		if ($tid >0 or $tid <> "")
		{
		$sql=mysqli_query($con, "SELECT pmp FROM timetable WHERE id='$tid' ");
		$pmp="";
	    if (mysqli_num_rows($sql) > 0)
		{
		  $row=mysqli_fetch_array($sql);
		  $pmp=$row['pmp'];
		}
	    $per=$i;
	    $query="INSERT INTO anaplirosi (teacher,tid,date,period,pmpapon,aponid,mathima) VALUES ('$pmp','$tid','$dt','$per','$pmpapon','$aponid','$les') ";
	    
		if (mysqli_query($con, $query)) 
		{
			echo "New record created successfully";
        } 
		else 
		{
            echo "Error: " . $query . "<br>" . mysqli_error($con);
        }
		}
	  }
	}
}	
//**************************************
/*
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>";

 
for ($ka=1;$ka<$tk;$ka++)
{
 echo "<tr>";
 echo "<td>".$r. "</td>";
 $kathapon=$kath1[$ka];
 
 $sql=mysqli_query($con, "SELECT PMP  FROM teachers WHERE Name='$kathapon' ");
 $row=mysqli_fetch_array($sql);
 $pmpapon=$row['PMP'];
 $sql=mysqli_query($con, "SELECT id FROM timetable WHERE Teacher='$kathapon' ");
 $row=mysqli_fetch_array($sql);
 $aponid=$row['id'];
 echo "<td>"."<font face='arial' size='2pt'>".$kathapon." (".$aponid.")"."</td>";
 for ($i=1;$i<=$gym;$i++)
 {
	    echo "<td>" ."<font face='arial' size='2pt'>". $lesson1[$ka][$i]. "</td>";	 
 }     
		 
 echo "</tr>"; echo "<tr>"; echo "<td>". "   " . "</td>"; echo "<td>". "   " . "</td>";
 for ($i=1;$i<=$gym;$i++)
     {
		 $tid=$anaplir1[$ka][$i];
		 $sql=mysqli_query($con, "SELECT Teacher FROM timetable  WHERE id='$tid' ");
	     $row=mysqli_fetch_array($sql);
		 $teachname=$row['Teacher'];
		 if ($tid > 0 and $tid < 999 )
		 {
		   echo "<td>"."<b>"."<font face='arial' size='2pt'>".$teachname."</b>"."</td>"; 
		 }
		 elseif ($tid==999)
		 {
		    echo "<td>"."<b>"."<font face='arial' size='2pt'>"."ΣΤΟ ΤΜΗΜΑ ΤΟΥΣ"."</b>"."</td>";
		 }
		 elseif ($tid=="!")
		 {
			echo "<td>"."<b>"."<font face='arial' size='2pt'>"."ΠΑΡΩΝ/ΟΥΣΑ"."</b>"."</td>"; 
		 }
		 elseif ($tid=="ΔΔΚ")
		 {
			echo "<td>"."<b>"."<font face='arial' size='2pt'>"."ΕΚΔΗΛΩΣΗ"."</b>"."</td>"; 
		 }
		 else
		 {
		   echo "<td>"."<b>"."<font face='arial' size='2pt'>"." "."</b>"."</td>"; 	 
		 }
		 
	 } 
 
 echo "</tr>"; 
 
  $ka++;
  $r++;  
  }
echo "</table>";
*/
//.................................. insert anaplirosi gia 7h
$fe=count($ebdomi);
for ($i=1;$i<=$fe;$i++)
{
 $aponid=$apone[$i];
 
 $sql=mysqli_query($con, "SELECT t.Teacher,s.PMP FROM timetable as t,teachers as s WHERE t.Teacher=s.Name AND t.id='$aponid' ");
 $row=mysqli_fetch_array($sql);
 $pmpapon=$row['PMP'];
 $teachname=$row['Teacher'];
 $per=$gym;
 $perevd=$evdfrom[$i];
 //
 $tid=$anaplire[$i];
 $sql=mysqli_query($con, "SELECT t.Teacher,s.PMP FROM timetable as t,teachers as s WHERE t.Teacher=s.Name AND t.id='$tid' ");
 $row=mysqli_fetch_array($sql);
 $pmp=$row['PMP'];
 $teachname=$row['Teacher'];
 $ebd=$ebdomi[$i]." [".$rme[$i]."]";
 if ($pmp > 0)
 {
     mysqli_query($con,"INSERT INTO anaplirosi (id,teacher,tid,date,period,aponevd,pmpapon,aponid,mathima) 
              VALUES (NULL,'$pmp','$tid','$dt','$per','$perevd','$pmpapon','$aponid','$ebd')");	
 }
}
}
include "readdailytab.php";
include "ensoanaplir.php";
include "katahorisi.php";
//************* emfanise anapliroseis kai allages
$_SESSION['fn'] =1; 
include "allageshmeras.php";
?>	
<br />
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού   </a>
</body>
</html>