<?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];
		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 "Error: " . $query . "<br>" . mysqli_error($con);
        }
		}
	  }
	}
}	

//.................................. insert anaplirosi gia 7h
$fe=0;
if (!empty($ebdomi)) $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>