<?php
//.......................................
$room=array_unique($room);
sort($room);
$n=count($room);
$t=$id;
////////
for ($i=1;$i<=$t;$i++) 
{
  for ($j=1;$j<=$gym;$j++) 
  {
        if( !($i & 1)){
		  if($tprog[$i][$j] <>"" )
		  {
	  	  $ar=explode("/",$tprog[$i][$j]);
		  $r=$ar[0];//room
		  $roomin[$r][$j]=$teach[$i-1];
		  }
		  
		}
  }		  
}
if (!empty($roomin)){ksort($roomin);}

//....................................
$idapon=array();
$sql=mysqli_query($con,"SELECT t.id FROM timetable as t,apousieskath as a 
	where t.pmp=a.pmp and (a.datefrom <= '$dt' and a.dateto >='$dt') ");
$i=1;
while($row=mysqli_fetch_array($sql))
{
	$idapon[$i]=$row['id'];
	$i++;
}
// ensomatosi allagon
$sql=mysqli_query($con,"SELECT * FROM allages where date='$dt' ");
$n=mysqli_num_rows($sql);
$idteacher=$fromper=$toper=$apon=array();
$i=1;
while($row=mysqli_fetch_array($sql))
{
    $idx=$row['chwith'];
	$id=$row['idteacher'];
	$mis=$row['miso'];
	$tp=$row['toper'];
	if($mis=="+")
	{
			$tprog[$id][$row['fromper']]="";
			$str1=rtrim(ltrim($tprog[$id][$row['toper']]));
			$tprog[$id][$row['toper']]="";
			$ch=mb_substr($str1,0,3);$ch1=$ch;
		//	$ch1=mb_substr($ch,0,1)."0".mb_substr($ch,-1);
			$tprog[$id][$row['fromper']]=$ch1;
			$idm=$id+1;
			$tprog[$idm][$row['toper']]=$tprog[$idm][$row['fromper']];
			if ($idx > 0)
			{
				$tprog[$idx][$row['toper']]="";
				$str1=rtrim(ltrim($tprog[$idx][$row['fromper']]));
				$tprog[$idx][$row['fromper']]="";
				$ch=mb_substr($str1,0,3);$ch1=$ch;
			//	$ch1=mb_substr($ch,0,1)."0".mb_substr($ch,-1);
				$tprog[$idx][$row['toper']]=$ch1;
				$idm=$idx+1;
				$tprog[$idm][$row['toper']]="";	
			}

			
	}
	
	if (($idx > 0 and in_array($idx,$idapon)) )
	{
		$idteacher[$i]=$row['idteacher'];
		$fromper[$i]=$row['fromper'];
		$toper[$i]=$row['toper'];
		$apon[$i]=0;
		$room[$i]=$row['room'];
		$i++;
		$idteacher[$i]=$row['chwith'];
		$fromper[$i]=$row['toper'];
		$toper[$i]=$row['fromper'];
		$apon[$i]=1;
		
	}
	elseif ($tp==$gym and in_array($id,$idapon) )
	{
		$idteacher[$i]=$row['idteacher'];
		$fromper[$i]=$row['fromper'];
		$toper[$i]=$row['toper'];
		$apon[$i]=1;
		$room[$i]=$row['room'];
		$i++;
		$idteacher[$i]=$row['chwith'];
		$fromper[$i]=$row['toper'];
		$toper[$i]=$row['fromper'];
		$apon[$i]=0;	
	}
	elseif (($idx > 0) and (!in_array($idx,$idapon)) and ($id <> $idx ))
	{
		$idteacher[$i]=$row['idteacher'];
		$fromper[$i]=$row['fromper'];
		$toper[$i]=$row['toper'];
		$apon[$i]=0;
		$room[$i]=$row['room'];
		$i++;
		$idteacher[$i]=$row['chwith'];
		$fromper[$i]=$row['toper'];
		$toper[$i]=$row['fromper'];
		$apon[$i]=0;	
		
	}
	elseif ($idx==$id)	
	{
		$idteacher[$i]=$row['idteacher'];
		$fromper[$i]=$row['fromper'];
		$toper[$i]=$row['toper'];
		$apon[$i]=0;
		$room[$i]=$row['room'];	
	}	
	else 
	{
		$idteacher[$i]=$row['idteacher'];
		$fromper[$i]=$row['fromper'];
		$toper[$i]=$row['toper'];
		$apon[$i]=0;
		$room[$i]=$row['room'];	
	}
	$i++;
	
}
$n=$i-1;

$fe=0;
for ($i=1;$i<=$n;$i++)
{		
	   if ($apon[$i]==1)
	   {
		    if ($toper[$i]==$gym )
			{
				  $temp=$tprog[$idteacher[$i]][$toper[$i]]; 
				  if ($temp <> "" or !empty($temp) or !is_null($temp))
				  {
					 $fe++;
					 if ($tprog[$idteacher[$i]][$fromper[$i]] <> $temp)
					 {
					    $ebdomi[$fe]=$tprog[$idteacher[$i]][$fromper[$i]];
					 }
					 $tprog[$idteacher[$i]][$fromper[$i]]="";
					 $apone[$fe]=$idteacher[$i];
					 $room1=$room[$i];
					 $xid=$idteacher[$i]+1;
					 $ar = explode("/",$tprog[$xid][$toper[$i]]);
					 $tprog[$xid][$fromper[$i]]="";
					 $evdfrom[$fe]=0;
					 if (!empty($room1)){$rme[$fe]=$room1;} else {$rme[$fe]=$ar[0];}
					 
				  }
				  else
				  {
					$temp=$tprog[$idteacher[$i]][$toper[$i]];
					$tprog[$idteacher[$i]][$toper[$i]]=$tprog[$idteacher[$i]][$fromper[$i]];
					$tprog[$idteacher[$i]][$fromper[$i]]=$temp;
					$xid=$idteacher[$i]+1;
					$temp=$tprog[$xid][$toper[$i]];
					$tprog[$xid][$toper[$i]]=$tprog[$xid][$fromper[$i]];
					$tprog[$xid][$fromper[$i]]=$temp;
				  }
			}
			else
			{
			    $temp=$tprog[$idteacher[$i]][$toper[$i]];
				$tprog[$idteacher[$i]][$toper[$i]]=$tprog[$idteacher[$i]][$fromper[$i]];
				$tprog[$idteacher[$i]][$fromper[$i]]=$temp;
				$xid=$idteacher[$i]+1;
				$temp=$tprog[$xid][$toper[$i]];
				$tprog[$xid][$toper[$i]]=$tprog[$xid][$fromper[$i]];
				$tprog[$xid][$fromper[$i]]=$temp;
				
			}
	   }
	   else
	   {
			
			$temp=$tprog[$idteacher[$i]][$toper[$i]];
			$tprog[$idteacher[$i]][$toper[$i]]=$tprog[$idteacher[$i]][$fromper[$i]];
			$tprog[$idteacher[$i]][$fromper[$i]]=$temp;
			$xid=$idteacher[$i]+1;
			$temp=$tprog[$xid][$toper[$i]];
			$tprog[$xid][$toper[$i]]=$tprog[$xid][$fromper[$i]];
			$tprog[$xid][$fromper[$i]]=$temp;
	   }

}

//////// ensomatosi allagon aithousas
$sql=mysqli_query($con,"SELECT * FROM allagiroom where date='$dt'");
$n=mysqli_num_rows($sql);
for ($i=1;$i<=$n;$i++)
{
	$row=mysqli_fetch_array($sql);
	$id=$row['idteacher'];
	$xid=$id+1;
	if ($tprog[$xid][$row['period']] <> "")
	{
	   $ar = explode("/",$tprog[$xid][$row['period']]);
	   $tprog[$xid][$row['period']]=$row['seroom']."/".$ar[1];
	}
}

//////// ensomatosi ddk
$sql=mysqli_query($con,"SELECT * FROM apouddk where date='$dt'");
$n=mysqli_num_rows($sql);
for ($i=1;$i<=$n;$i++)
{
	$row=mysqli_fetch_array($sql);
	$tid=$row['tid'];
	$tprog[$tid][$row['period']]=$row['tmima'];
	$tid=$tid+1;
	$rm="";
	$tprog[$tid][$row['period']]=$rm."/"."Δ.Δ.Κ";	
}