<?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">

$(document).ready(function()
{
 $("#kathig1").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig1").val(),
         success: function(html) {
             $("#subjectid1").html(html);
         }
     });
 });
 
  $("#kathig2").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig2").val(),
         success: function(html) {
             $("#subjectid2").html(html);
         }
     });
 }); 
   $("#kathig3").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig3").val(),
         success: function(html) {
             $("#subjectid3").html(html);
         }
     });
 }); 
   $("#kathig4").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig4").val(),
         success: function(html) {
             $("#subjectid4").html(html);
         }
     });
 }); 
   $("#kathig5").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig5").val(),
         success: function(html) {
             $("#subjectid5").html(html);
         }
     });
 }); 
   $("#kathig6").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig6").val(),
         success: function(html) {
             $("#subjectid6").html(html);
         }
     });
 }); 
  $("#kathig7").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig7").val(),
         success: function(html) {
             $("#subjectid7").html(html);
         }
     });
 });
  $("#kathig8").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig8").val(),
         success: function(html) {
             $("#subjectid8").html(html);
         }
     });
 });  
  $("#kathig9").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig9").val(),
         success: function(html) {
             $("#subjectid9").html(html);
         }
     });
 });  
  $("#kathig10").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig10").val(),
         success: function(html) {
             $("#subjectid10").html(html);
         }
     });
 });  

  $("#kathig11").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig11").val(),
         success: function(html) {
             $("#subjectid11").html(html);
         }
     });
 });  

  $("#kathig12").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig12").val(),
         success: function(html) {
             $("#subjectid12").html(html);
         }
     });
 });  

  $("#kathig13").on('change', 
 function()
 {
     $.ajax({
		 url: "changeallagi.php",
         data: "kath="+$("#kathig13").val(),
         success: function(html) {
             $("#subjectid13").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 where mod(id,2)=1 order by id");
$i=0;
$row = mysqli_fetch_assoc($result);
while(!empty($row)) 
{
	$k1=$row['id'];
	$k2=$row['Teacher'];
	$k3=$row['id'];
	$x2=array_slice($row, 2);
	$c=count(array_filter($x2));
	if ($c > 1)
	{
		$i++;
		$kathida[$i]=$k1;
	    $kath1a[$i]=$k2;
	    $teachname[$k3]=$k2;
	}
	$row = mysqli_fetch_assoc($result);
}
$a=$i;
$nid=$id;
$ktse=$teacherse=$teacherapo=$roomam=$ktapo=$amper=$math=array();
$result1 = $dbcon->prepare("SELECT * FROM amivallages WHERE date=? ORDER BY id  ");
$result1->execute([$dt]);
$crow2=$result1->rowCount();
$i=1;
while($row=$result1->fetch(PDO::FETCH_ASSOC))
{
	 $ktse[$i]=$row['idtse'];
	 $teacherse[$i]=$teachname[$row['idtse']];
	 $ktapo[$i]=$row['idtapo'];
	 $teacherapo[$i]=$teachname[$row['idtapo']];
	 $math[$i]=$row['mathima'];
	 $amper[$i]=$row['period'];
	 $roomam[$i]=$row['room'];
	 $tmimam[$i]=$row['tmima'];
	 $i++;
}	
$allagi2=$i-1;
$kathigse=$kathigapo=array();

$res1 = $dbcon->prepare("SELECT DISTINCT synd FROM syndcodes order by synd ASC" );
$res2 = $dbcon->prepare("SELECT DISTINCT tmima FROM allstudentinfo order by tmima ASC" ); 
$res1->execute();
$res2->execute();
$ii=1;
while($row2 = $res2->fetch(PDO::FETCH_ASSOC))
     {
	    $tmima[$ii]=$row2['tmima'];
        $ii++;
     }
while($row1 = $res1->fetch(PDO::FETCH_ASSOC))
     {
	    $tmima[$ii]=$row1['synd'];
        $ii++;
     }

?> 
<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>
<th></th>
<th>ΤΜΗΜΑ</th>
</tr>";

$k=1;
while($k<=13)
{
	 echo "<tr>";
	 echo "<td>" .$k;
	 $idx='kathig'.$k;
	 $ids='subjectid'.$k;
	 echo "<td>";
	 ?> 
	 <select name="kathigse[]" id="<?php echo $idx;?>" >
	 <?php
     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>
	 </td>
	 <td>
	 <select name="subject[]" id="<?php echo $ids;?>" >
	 <?php
	 if (!empty($math[$k]))
	 {  
		echo "<option value ='".$math[$k]."'>$math[$k]</option>";
	 }
	 else
	 {
		echo "<option value =''> </option>";  
	 }	 
	 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 "<td>"." "."</td>";
/////////////////////////	 
	 echo "<td>";
	 ?> 
	 <select name="tmima1[]" >
	 <?php
	 	   
	 if (!empty($tmimam[$k]))
	 {
		  echo "<option value ='".$tmimam[$k]."'>$tmimam[$k]</option>";
	 }
	 else
	 {
	 echo "<option value =''>--------</option>";}
	 $jk=1;
     while($jk <= $ii) 
	 {
       echo "<option value ='".$tmima[$jk]."'>".$tmima[$jk]."</option>";
       $jk++;
     }
     echo "</select>"."</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'];
	$subj=$_POST['subject'];
	$perse=$_POST['perse'];
	$room2=$_POST['room2'];
	$tm=$_POST['tmima1'];
	$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 (idtse,idtapo,mathima,period,room,date,tmima) 
		VALUES ('$kse[$k]','$kapo[$k]','$subj[$k]','$perse[$i]','$room2[$i]','$dt','$tm[$k]' ) ";

        if (!mysqli_query($con,$sql))
        {
         die('Error: ' . mysqli_error($con));
         } 
	
	    $i++;
		$k++;
   }
	header('Location: '.$_SERVER['REQUEST_URI']);
    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>