<?php
include "scrinit.php";
include "logo.php";
date_default_timezone_set("Europe/Athens");
?>

<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function() {
 $("#kathig").bind("change", function() {
	 var gp=$(this).val();
     $.ajax({
         url: "changeallagi.php",
         data: "kathig="+gp,
         success: function(html) {
             $("#subjectid").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=900, height=300, top='+top+', left='+left);
}

</script>

<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"; 
$usr=$_SESSION['usr'];
$dt=$_SESSION['dt'];
$gym=$_SESSION['gym'];
$day = date('D', strtotime($dt));
if ($day=="Sat" or $day=="Sun"){
	echo "ΔΕΝ ΥΠΑΡΧΟΥΝ ΜΑΘΗΜΑΤΑ ΣΗΜΕΡΑ";}
else 
{
?>
<h3> ΠΡΟΓΡΑΜΜΑ ΑΜΟΙΒΑΙΑΩΝ ΑΛΛΑΓΩΝ: <?php echo date('D', strtotime($dt))." , ".date('d-m-Y', strtotime($dt));?> </h3>

<?php
$all="id,pmp,Teacher,";
for ($i=1;$i<=$gym;$i++){$t=$day.$i;$ta.=$t.',';}
$all=$all.$ta;
$all = rtrim($all, ',');

$kathida=$kath1a=$allclassa=$kt=$fp=$tp=$room1=$miso=$chwith=array();

$result = mysqli_query($con,"SELECT ".$all. " FROM timetable");
$a=1;
while($row = mysqli_fetch_array($result)) 
{
  $notallblank=0;
  $id=$row['id'];
  $teach[$id]=$row['Teacher'];
  $pmpt[$id]=$row['pmp'];
  for($i=1;$i<=$gym;$i++)
  {
	 $in=$day.$i;
	 $l1=$row["$in"];
	 if (!empty($l1)) 
	 {
	    $tprog[$id][$i]=$l1;
	    $notallblank=1;
	 }
	 else
	 {
		 $tprog[$id][$i]="";
	 }
   }
  if ($notallblank==1)
  {   
      if($id % 2 !=0)
	  {
		  $kathida[$a]=$row['id'];
		  $kath1a[$a]=$row['Teacher'];
		  $teachname[$row['id']]=$row['Teacher'];
		  $a++; 
	  }
	  
  }
}
$a=$a-1;
$nid=$id;
$ktse=$teacherse=$teacherapo=$roomam=$ktapo=$amper=array();
$result1 = mysqli_query($con,"SELECT idtse,idtapo,period,room,id FROM amivallages WHERE date='$dt' ORDER BY id  ");
$crow2=mysqli_num_rows($result1);
$i=1;
while($row = mysqli_fetch_array($result1))
{
	 $ktse[$i]=$row['idtse'];
	 $teacherse[$i]=$teachname[$row['idtse']];
	 $ktapo[$i]=$row['idtapo'];
	 $teacherapo[$i]=$teachname[$row['idtapo']];
	 $amper[$i]=$row['period'];
	 $roomam[$i]=$row['room'];
	 $i++;
}	
$allagi2=$i-1;
$kathigse=$kathigapo=array();
?> 
 
<form method="POST"  >
<?php
echo "<table style='width:400px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#66CC99' >
<th>Α/Α</th>
<th>ΣΕ ΕΚΠΑΙΔΕΥΤΙΚΟ </th>
<th>ΜΑΘΗΜΑ</th>
<th>ΑΠΟ ΕΚΠΑΙΔΕΥΤΙΚΟ </th>
<th>ΠΕΡΙΟΔΟΣ</th>
<th>ΑΙΘΟΥΣΑ</th>
</tr>";

$k=1;
 while($k<=10)
 {
	 echo "<tr>";
	 echo "<td>" .$k;
	 $idx='kathig'.$k;
	 echo "<td>"."<select name='kathigse[]' id='kathig'>";
     if (!empty($teacherse[$k]))
		  {  
			 echo "<option value ='".$ktse[$k]."'>$teacherse[$k]</option>";
		  }
		   else
		  {
			 echo "<option value =''> </option>";  
		  }
	 $j=0;
	 while ($j<=$a) 
      {
	      $kathigitis=$kath1a[$j];
	      echo "<option value ='".$kathida[$j]."'>".$kathigitis."</option>";
		  $j++;
      }
     ?></select><?php
	 echo "</td>";
     echo "<td>"."<select name='subject[]' id='subjectid' >";
	 echo "</select>"."</td>";
	 echo "<td>"."<select name='kathigapo[]'>";
	 
	 if (!empty($teacherapo[$k]))
		  {
			 echo "<option value ='".$ktapo[$k]."'>$teacherapo[$k]</option>";}
		   else{
			 echo "<option value =''> </option>";  
		   }
	 $j=0;
	 while ($j<=$a) 
      {
	      $kathigitis=$kath1a[$j];
		  
		      echo "<option value ='".$kathida[$j]."'>".$kathigitis."</option>";
		     $j++;
      }
     echo "</select>"."</td>";
	
     echo "<td>"."<input type='text' name='perse[$k]'  size='4' value= '$amper[$k]'>"."</td>";
	 echo "<td>"."<input type='text' name='room2[$k]'  size='15' value= '$roomam[$k]'>"."</td>";
	 echo "</tr>";
     $k++;
 }
echo "</table>";
echo "<br>";
?>
<br><input type="Submit" name="Submitallages" value="Καταχώρηση  Αμοιβαίων Αλλαγών" style="background-color: #336699; color: #ffffff;padding:4px;" />
<input type="button" value="ΑΡΧΙΚΟ Ημερήσιο Πρόγραμμα" onclick="window.open('dailyprognormal.php','','width=900,height=900')" />
<input type="button" value="Διαθέσιμες Αίθουσες" onclick="window.open('roomprog.php','','width=900,height=900')" />
</form>
<?php

if(isset($_POST['Submitallages']))
{
	$kse=$_POST['kathigse'];
	$kapo=$_POST['kathigapo'];
	$perse=$_POST['perse'];
	$room2=$_POST['room2'];

    $result = mysqli_query($con,"SELECT id FROM amivallages WHERE date='$dt' ");
    $crow1=mysqli_num_rows($result);

    if ($crow1>0)
    {	
        mysqli_query($con,"DELETE FROM amivallages WHERE date='$dt'");
    }
    $i=1;$k=0;
    while($perse[$i]<> 0)  
	{
        $sql="INSERT INTO amivallages (id,idtse,idtapo,period,room,date) VALUES (NULL,'$kse[$k]','$kapo[$k]','$perse[$i]','$room2[$i]','$dt' ) ";

        if (!mysqli_query($con,$sql))
        {
         die('Error: ' . mysqli_error($con));
         } 
	
	    $i++;
		$k++;
   }
   include "amivallages1.php";
}
}
                  
?>
<br /><br /><br />
<a  href=<?php echo $menu;?> style="color:blue "  > <button>Επιστροφή στο Μενού</button></a>
<a  href="anapliroseisnew.php" style="color:blue "  > <button>ΑΝΑΠΛΗΡΩΣΕΙΣ/ΑΛΛΑΓΕΣ </button></a>
</body>
</html>