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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel='stylesheet' type='text/css' href='multimcss.css' />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<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() {
     $.ajax({
         url: "changeallagi.php",
         data: "kathig="+$("#kathig").val(),
         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 green;padding:2px;font-size:12pt; }
<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));



$old=mysqli_query($con,"SELECT Class_id,yt,voi_id,room FROM voithos order by Class_id");
$i=1;
while($row = mysqli_fetch_array($old))
{
 $class[$i]=$row['Class_id'];
 $yt[$i]=$row['yt'];
 $vd[$i]=$row['voi_id'];
 $room[$i]=$row['room'];
 $i++;
}	
$classx=mysqli_query($con, "select DISTINCT tmima from allstudentinfo order by tmima");
$nclass=mysqli_num_rows($classx);
//******************************************************* read from allages and delete unnecessary
$teachr = mysqli_query($con,"SELECT Name,PMP FROM teachers where access=1 order by Name");
$bd=mysqli_query($con,"SELECT Name,PMP FROM teachers where access=2 or access=8");
$roomx=mysqli_query($con,"SELECT roomnum from room order by roomnum");
$i=0;
while ($rm = mysqli_fetch_array($roomx)) 
      {
	        $i++;
			$rm1[$i]=$rm['roomnum'];
			
       } 
$rn=$i;	   
$i=0;
while ($br = mysqli_fetch_array($bd)) 
      {
	        $i++;
			$vd1[$i]=$br['Name'];
			$vd2[$i]=$br['PMP'];
			$vd3[$vd2[$i]]=$br['Name'];
			
       }	   
$vn=$i;	   
$i=0;
while ($tr = mysqli_fetch_array($teachr)) 
      {
	        $i++;
			$t1[$i]=$tr['Name'];
			$t2[$i]=$tr['PMP'];
			$t3[$t2[$i]]=$t1[$i];
			
       }
  
$tn=$i;   	   

echo "<br>";
?>
<form method="POST"  >
<h3> ΚΑΤΑΧΩΡΗΣΗ ΥΠΕΥΘΥΝΩΝ ΤΜΗΜΑΤΩΝ </h3>
<?php
echo "<table style='width:400px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#66CC99' >
<th>ΤΜΗΜΑ</th>
<th>ΑΙΘΟΥΣΑ</th>
<th>ΥΤ</th>
<th>ΒΟΗΘΟΣ</th>
</tr>";

for ($k=1;$k<=$nclass;$k++)
 {
	 echo "<tr>";
	 $cr = mysqli_fetch_array($classx);
	 echo "<td>";
	 echo $cr['tmima'];
	 echo "<td>"."<select name='rm[]'>";
	 if (!empty($room[$k]))
	 {
			 echo "<option value ='".$room[$k]."' selected>".$room[$k]."</option>";
	 }
	 for($i=1;$i<=$rn;$i++)
     {
	        echo "<option value ='".$rm1[$i]."'>".$rm1[$i]."</option>";
     }     
	 echo "</select>"."</td>";
	 //******************* yt
	 
	 echo "<td>"."<select name='td[]'>";
	 if (!empty($yt[$k]) or $yt[$k] > 0)
		  {
			 echo "<option value ='".$yt[$k]."' selected>".$t3[intval($yt[$k])]."</option>";
		  } 
	 for($i=1;$i<=$tn;$i++)
      {
	        echo "<option value ='".$t2[$i]."'>".$t1[$i]."</option>";
		           }     
	  echo "</select>"."</td>";
	  
	 echo "<td>"."<select name='bd[]'>";
	 if (!empty($vd[$k]))
		  {
			 echo "<option value ='".$vd[$k]."' selected>".$vd3[intval($vd[$k])]."</option>";}
	 
	 for($i=1;$i<=$vn;$i++)
      {
	        echo "<option value ='".$vd2[$i]."'>".$vd1[$i]."</option>";
	  }     
	  
	  echo "</select>"."</td>";
	  
	  
	  echo "</tr>";
      
}
echo "</table>";


?>
<input type="hidden" name="nc" value=<?php echo $nclass;?> >
<br><input type="Submit" name="Submitallages" value="Καταχώρηση  Αλλαγών" style="background-color: #336699; color: #ffffff;padding:4px;" />
</form>

<?php
if (isset($_POST['Submitallages']))
{
	$bdx=$_POST['bd'];
	$ytx=$_POST['td'];
	$rmx=$_POST['rm'];
	$nclass=$_POST['nc'];
	mysqli_query($con, "delete from voithos" );
	$classx=mysqli_query($con, "select DISTINCT tmima from allstudentinfo order by tmima");
	$i=0;
	while ($rm = mysqli_fetch_array($classx)) 
      {
			$rm1[$i]=$rm['tmima'];$i++;
			
       }
	for ($i=0;$i<$nclass;$i++)
	{
		$sql= "insert into voithos (Class_id,yt,voi_id,room) 
					 values ('$rm1[$i]','$ytx[$i]','$bdx[$i]','$rmx[$i]')";
			if (mysqli_query($con, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}		 
	}


}                 
?>

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

</body>
</html>