<?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">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
function addit()
{
    $("#allages").off().on('click','.btnSelect',function()
	{
         // get the current row
         var currentRow=$(this).closest("tr");
		 var col0=currentRow.find("td:eq(1)").text();
		  var col1=currentRow.find("td:eq(2)").text();
		 var col2=currentRow.find("td:eq(3)").text(); 
         var col3=currentRow.find("td:eq(4)").text(); // get current row 4th TD
		 var table = document.getElementById("dataTable");
		 var rowCount = table.rows.length;
		 var rc=rowCount-1;
		 
		 var row = table.insertRow(1);
			for(var i=0; i<8; i++) 
			{
				var newcell	= row.insertCell(i);
				newcell.innerHTML = table.rows[rc].cells[i].innerHTML;
				switch(newcell.childNodes[0].type) 
				{
					case "text":
							newcell.childNodes[0].value = "";
							break;
					case "checkbox":
							newcell.childNodes[0].checked = false;
							break;
					case "select-one":
							newcell.childNodes[0].selectedIndex = 0;
							break;
				}
				
			}
		 $("#kkk").val(col1); 
           
			table.rows[1].cells[1].childNodes[0].value=7;
			table.rows[1].cells[2].childNodes[0].value=col2;
			table.rows[1].cells[5].childNodes[0].value=col3;
    
});
}



function addRow(tableID) 
		{

			var table = document.getElementById(tableID);
			var rowCount = table.rows.length;
			var row = table.insertRow(rowCount);
			var rc=rowCount-1;
			var colCount = table.rows[rowCount-1].cells.length;

			for(var i=0; i<colCount; i++) 
			{

				var newcell	= row.insertCell(i);

				newcell.innerHTML = table.rows[rc].cells[i].innerHTML;
				//alert(newcell.childNodes);
				switch(newcell.childNodes[0].type) 
				{
					case "text":
							newcell.childNodes[0].value = "";
							break;
					case "checkbox":
							newcell.childNodes[0].checked = false;
							break;
					case "select-one":
							newcell.childNodes[0].selectedIndex = 0;
							break;
				}
			}
			
		}

function deleteRow(tableID) {
			try
			{
			var table = document.getElementById(tableID);
			var rowCount = table.rows.length;
			var cl = table.rows[0].cells.length-1;
			for(var i=0; i<rowCount; i++) {
				var row = table.rows[i];
				var chkbox = row.cells[cl].childNodes[0];
				if(null != chkbox && true == chkbox.checked) {
					if(rowCount <= 1) {
						alert("Cannot delete all the rows.");
						break;
					}
					table.deleteRow(i);
					rowCount--;
					i--;
				}
			}
			}
			catch(e) {
				alert(e);
			}
		}

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=1000, height=700, 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:1px;font-size:10pt; }
<p style="font-size:10px;"></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 
{
?><h4> ΠΡΟΓΡΑΜΜΑ ΑΛΛΑΓΩΝ/ΑΝΑΠΛΗΡΩΣΕΩΝ : <?php echo date('D', strtotime($dt))." , ".date('d-m-Y', strtotime($dt));?> </h4><?php

$sql=mysqli_query($con,"select pmp from apousieskath where datefrom <= '$dt' and dateto >='$dt' ");	
$cx=mysqli_num_rows($sql);
$i=0;
while($row=mysqli_fetch_array($sql))
{
  $apontes[$i]=$row['pmp'];
  $i++;
}


$lesson=$kath=array();

//.............. elegxos gia ddk hmeras
echo "<b>"."ΔΡΑΣΤΗΡΙΟΤΗΤΕΣ ΗΜΕΡΑΣ"."</b>"."<br>";
$sql=mysqli_query($con,"select * from offperiods where datefrom='$dt' ") ;
$c=mysqli_num_rows($sql);
if ($c > 0)
{
  while($row=mysqli_fetch_array($sql))
  {
  $perapo=$row['perapo'];
  $permexri=$row['permexri'];
  $descr=$row['description'];
  $tmf=$row['tmimafrom'];
  $tmt=$row['tmimamexri'];
  echo "<b>"."<font face='arial' size='2pt' color='#ff0000'>". $descr.
  " ΑΠΟ ΠΕΡΙΟΔΟ: ".$perapo."-".$permexri." ΤΜΗΜΑ: ".$tmf." - ".$tmt."</font> "."</b>";
  echo "<br>";
  }
}
else echo "<font face='arial' size='2pt' color='#ff0000'> ΔΕΝ ΥΠΑΡΧΟΥΝ ΔΡΑΣΤΗΡΙΟΤΗΤΕΣ ΣΗΜΕΡΑ </font> ";
  //.............

$cc=count($apontes);
if ($cc > 0)
{
echo "<br>"."<br>"."<b>"."ΠΡΟΓΡΑΜΜΑ ΑΠΟΝΤΩΝ"."</b>";
//************************* prepare column list for query
$ta="";
$all="id,pmp,Teacher,";
for ($i=1;$i<=$gym;$i++){$ta.="day".$i.',';}
$all=$all.$ta;
$all = rtrim($all, ',');
//*************************	

echo "<table style='width:900px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#FFAF80'  >
<th>Α/Α</th>
<th>Εκπαιδευτικός</th>";
for ($i=1;$i<=$gym;$i++)
{
	$titep="Περ".$i;
	echo "<th>".$titep."</th>";
}
echo "</tr>";
echo "<br>"."<br>"; 
$ka=1;$r=1;$j=1;
foreach($apontes as $teach)
{
  $result = mysqli_query($con,"SELECT ".$all." FROM dailytab WHERE pmp='$teach'");
  $row = mysqli_fetch_array($result);
  $kath[$ka]=$row['Teacher']; 
  $id=$row['id'];
  $id1=$id+1;
  $idteach[$j]=$id;
  $idt[$j]=$id;
  $result1 = mysqli_query($con,"SELECT ".$all."  FROM dailytab WHERE id='$id1'");
  $row1 = mysqli_fetch_array($result1);
  $i=1;
  while ($i<=$gym)
  {
      $in="day".$i;
      $l1=$row["$in"];
      $l2=$row1["$in"];
	  $rm=explode("/",$l2);
	  $room=$rm[0];
      $lesson[$ka][$i]= rtrim(ltrim($l1));
	  $ls[$j][$i]=rtrim(ltrim($l1));
	  if ($lesson[$ka][$i]==" " or empty($lesson[$ka][$i])){$lesson[$ka][$i]="";} else {
		  $lesson[$ka][$i]=$lesson[$ka][$i];}
	  $lesson[$ka+1][$i]= rtrim(ltrim($l2));
      $i++;
   }

 $i=1;
 //..................... display table
 echo "<tr>";
 echo "<td>".$r. "</td>";
 echo "<td>"."<b>".$kath[$ka]."(".$idteach[$j].")"."</b>"."</td>";
 while($i<=$gym)
     {
	 $lkai=$lesson[$ka][$i];
	 ?>
		 <td><b>
         <a href="#" onClick="popupCenter('findallages.php?tm=<?php echo $lkai; ?>')" style="text-decoration:none;"><?php echo $lkai;?></a>
		 </b></td>
     <?php	 
	 $i++;
	 } 
 echo "</tr>";
 $i=1;
 echo "<tr>";
 echo "<td>". "   " . "</td>";
 echo "<td>". "   " . "</td>";
 while($i<=$gym)
     {
	  echo "<td>".$lesson[$ka+1][$i]."</td>";
	 $i++;
	 } 
 echo "</tr>"; 
 //......................................
  $j++;
  $ka=$ka+2;
  $r++;  
  }
echo "</table>";
}
$ka=$ka-1;
$_SESSION['idapon']=$idteach;
//.................. check for possible allagi 7hs periodou //////////////////////////////////////
echo "<br>"."<b>"."ΠΙΘΑΝΕΣ ΑΛΛΑΓΕΣ 7ης ΠΕΡΙΟΔΟΥ: "."</b>"."<br>";
?>
<table id='allages' style='width:700px' class='myTable' >
<thead style='border:1px solid black' bgcolor='#FFAF80' >
<th>Τμήμα</th>
<th>Εκπαιδευτικός</th>
<th>ΙD</th>
<th>Σε Περ από 7η</th>
<th>ΙD Απον</th>
<th>Εκτέλεση</th>
</thead>
<?php
$n=$j-1;
$d=0;
for ($i=1;$i<=$n;$i++)
{
 $result = mysqli_query($con,"SELECT * FROM timetable  order by id");
 while($row = mysqli_fetch_array($result))
 {
  $xdt=$row['id'];
  if (!in_array($xdt, $idteach))
  {
    $tchr=$row['Teacher'];
    $in=$day.$gym; 
    //$l1=rtrim(ltrim($row["$in"]));
	 $l1=$row["$in"];
    if ($l1 <> "" and in_array($l1, $ls[$i]))
	{   
		$key=array_search($l1, $ls[$i]);
		$keno=$day.$key;
		//$l2=rtrim(ltrim($row["$keno"]));
		$l2=$row["$keno"];
		if ($l2 =="")
		{
			$d++;
			$xdt1[$d]=$xdt;
			$tchr1[$d]=$tchr;
			$toper1[$d]=$key;
			$fromper1[$d]=$gym;
			$apon[$d]=$idteach[$i];
			echo "<tbody id='first'>";
			echo "<tr>";
			echo "<td>"."<font face='arial' size='2pt' color='#ff0000'>".$l1."</font>"."</td>";
			echo "<td>"."<font face='arial' size='2pt' color='#ff0000'>".$tchr1[$d]."</font>"."</td>";
			echo "<td>"."<font face='arial' size='2pt' color='#ff0000'>".$xdt1[$d]."</font>"."</td>";
			echo "<td>"."<font face='arial' size='2pt' color='#ff0000'>".$toper1[$d]."</font>"."</td>";
			echo "<td>"."<font face='arial' size='2pt' color='#ff0000'>".$apon[$d]."</font>"."</td>";
			?> <td>
			
			<INPUT type="button" class="btnSelect" value="Προσθήκη Γραμμής" onclick="addit()" />
			</td>
			<?php
			//$sqlx = mysqli_query($con,"INSERT INTO allages (date, idteacher, fromper, toper, chwith) VALUES ('$dt', '$xdt1[$d]', '7','$toper1[$d]','$apon[$d]')");

		}
		echo "</tr>";
		echo "</tbody>";
	} 
  }
  $row = mysqli_fetch_array($result);
}
} 
echo "</table>";
echo "</b>"; 
if(count($tchr1)==0) {echo "<font face='arial' size='1pt' color='#ff0034'> Δεν υπάρχουν εισηγήσεις </font> ";}

$capon=count($idteach);

$r = mysqli_query($con,"SELECT idteacher FROM allages WHERE date='$dt' ");
$c=mysqli_num_rows($r);

//...... ******************************prepare array for allages programmatos
$kathida=$kath1a=$allclassa=$kt=$fp=$tp=$room1=$miso=$chwith=array();

$ta="";
$all="id,pmp,Teacher,";
for ($i=1;$i<=$gym;$i++){$ta.="day".$i.',';}
$all=$all.$ta;
$all = rtrim($all, ',');
$result = mysqli_query($con,"SELECT ".$all. " FROM dailytab where id & 1");
$a=1;
while($row = mysqli_fetch_array($result)) 
{
  $notallblank=0;
  for($i=1;$i<=$gym;$i++)
     {
		 $in="day".$i;
		 $l1=$row["$in"];
		 if (!empty($l1) or $l1 <> "") 
		 {
			$kathida[$a]=$row['id'];
			$kath1a[$a]=$row['Teacher'];
			$teachname[$row['id']]=$row['Teacher'];		
			$allclassa[$a][$i]=$l1;
			$notallblank=1;
		 }
		 else
		 {
			 $allclassa[$a][$i]="";
		 }
     }
  if ($notallblank==1){$a++;}
  
}
$a=$a-1; 
//******************************************************* read from allages and delete unnecessary
$result = mysqli_query($con,"SELECT idteacher,fromper,toper,room,miso,chwith,last,id FROM allages 
          WHERE date='$dt' ORDER BY id  ");
$crow1=mysqli_num_rows($result);
if ($crow1 > 0)
{
  $i=1;
  while($row = mysqli_fetch_array($result))
  {
	 $kt[$i]=$row['idteacher'];
	 $teacher[$i]=$teachname[$row['idteacher']];
	 $fp[$i]=$row['fromper'];
	 $tp[$i]=$row['toper'];
	 $room1[$i]=$row['room'];
	 $miso[$i]=$row['miso'];
	 $chwith[$i]=$row['chwith'];
	 $last[$i]=$row['last'];
	 $i++;
  }	
  $allagi=$i-1;
}

echo "<br>";
?>
<form method="POST" action="allagieggrafi.php" >
<h3> ΑΛΛΑΓΕΣ ΗΜΕΡΗΣΙΟΥ ΠΡΟΓΡΑΜΜΑΤΟΣ </h3>
<?php
array_multisort($kath1a,$kathida);
$kathig1=array();
?>
<INPUT type="button" value="Προσθήκη Γραμμής" onclick="addRow('dataTable')" />
<INPUT type="button" value="Διαγραφή Γραμμής" onclick="deleteRow('dataTable')" />
<TABLE id="dataTable" style='width:400px' class='myTable'>
<thead style='border:1px solid black' bgcolor='#66CC99' >
<th>ΕΚΠΑΙΔΕΥΤΙΚΟΣ</th>
<th>ΑΠΟ ΠΕΡ.</th>
<th>ΣΕ ΠΕΡ.</th>
<th>ΑΙΘΟΥΣΑ</th>
<th>+ ΜΙΣΑ</th>
<th>ΑΛΛΑΓΗ ΜΕ</th>
<th>7h ΠΑΡΑΜ.</th>
<th>ΕΠΙΛΟΓΗ</th>
</thead>
<tbody >
<?php
$i=1;
while($i<=14)
 {
	 ?>
	 <tr>
	 <td> <select id='kkk' name='kathigr[]'>
     <?php
	 if (!empty($teacher[$i]))
		  {
			 echo "<option value ='".$kt[$i]."' selected>".$teacher[$i]."</option>";}
		   else{
			 echo "<option value =0> </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='fromp[]'  size='4' value= '$fp[$i]'>"."</td>";
	 echo "<td>"."<input type='text' name='top[]'  size='4'  value= '$tp[$i]'>"."</td>";
	 echo "<td>"."<input type='text' name='room[]' size='15' value= '$room1[$i]'>"."</td>";
	 echo "<td>"."<input type='text' name='miso[]' size='4' value= '$miso[$i]'>"."</td>";
	 echo "<td>"."<input type='text' name='chwith[]' size='4' value= '$chwith[$i]'>"."</td>";
	 echo "<td>"."<input type='text' name='last[]' size='4' value= '$last[$i]'>"."</td>";
	 ?><TD><INPUT type="checkbox" name="chk"/></TD><?php
	 echo "</tr>";
     $i++;
 }
echo "</tbody>";
echo "</table>";

////////////////// allagh aithousas didaskalias
?>
<h3> ΑΛΛΑΓΗ ΑΙΘΟΥΣΑΣ ΔΙΔΑΣΚΑΛΙΑΣ </h3>
<?php
$idteacher=$teachern=$period=$aporoom=$seroom=array();

$result1 = mysqli_query($con,"SELECT idteacher,period,seroom,aporoom FROM allagiroom WHERE date='$dt' ORDER BY id  ");
$crow2=mysqli_num_rows($result1);
$i=1;
while($row = mysqli_fetch_array($result1))
{
	 $idteacher[$i]=$row['idteacher'];
	 $teachern[$i]=$teachname[$row['idteacher']];
	 $period[$i]=$row['period'];
	 $seroom[$i]=$row['seroom'];
	 $aporoom[$i]=$row['aporoom'];
	 $i++;
}	
$allagi3=$i-1;

echo "<table style='width:400px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#66CC99' >
<th>ΕΚΠΑΙΔΕΥΤΙΚΟΣ</th>
<th>ΠΕΡΙΟΔΟΣ</th>
<th>ΣΕ ΑΙΘΟΥΣΑ</th>
<th>ΑΠΟ ΑΙΘΟΥΣΑ</th>
</tr>";

$i=1;
while($i<=8)
 {
	 echo "<tr>";
	 echo "<td>"."<select name='kathig[]'>";
     
	 if (!empty($teachern[$i]))
		  {
			 echo "<option value ='".$idteacher[$i]."' selected>".$teachern[$i]."</option>";}
		   else{
			 echo "<option value =0> </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='period[$i]'  size='4' value= '$period[$i]'>"."</td>";
	 echo "<td>"."<input type='text' name='seroom[$i]'  size='4'  value= '$seroom[$i]'>"."</td>";
	 echo "<td>"."<input type='text' name='aporoom[$i]' size='15' value= '$aporoom[$i]'>"."</td>";
	 echo "</tr>";
     $i++;
 }
echo "</table>";
////////////////////////////////////////////////
?>

<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

}

                  
?>

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

</body>
</html>