<?php
session_start(); // start up your PHP session!
 if ($_SESSION['islogin']=='' ) { 
  echo "ΠΡΕΠΕΙ ΝΑ ΚΑΝΕΤΕ ΕΓΓΡΑΦΗ ΓΙΑ ΧΡΗΣΗΣ ΤΟΥ ΣΥΣΤΗΜΑΤΟΣ"."<BR>";
  sleep(3);
  header("Location: loginall.php"); /* Redirect browser */
  exit();
  }
$menu=$_SESSION['menu'];
include $menu;
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>entopismos mathiti</title>
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">

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">
input.right{  text-align:right;  }
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:10pt; }
</style>
</head>

<body>

<br><h3> ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ  ΜΑΘΗΜΑΤΟΣ </h3><br />
<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
$name = $_SESSION['n'];
$usr=$_SESSION['usr'];
$access=$_SESSION['access'];

echo " ΕΚΠΑΙΔΕΥΤΙΚΟΣ : "."</b>".$name."  "."<b>"." ΗΜΕΡΟΜΗΝΙΑ : "."</b>".$dt."<br>";
echo "<br>";
   $day=array("Mon","Tue","Wed","Thu","Fri");
   $result = mysqli_query($con,"SELECT * FROM timetable WHERE pmp='$usr'  " ); 
   $row = mysqli_fetch_array($result);
   $c = mysqli_num_rows($result);
   $id1=$row['id']+1;
   $result1 = mysqli_query($con,"SELECT * FROM timetable WHERE id='$id1'  " ); 
   $row1 = mysqli_fetch_array($result1);
   $i=0;
   $ind=1;
   while($i <= 4)
     {
	  $per=1;
      
	  while ($per <= 7) {
	  $dayf=$day[$i].$per;
	  $lesson= $row["$dayf"];
	  if (!empty($lesson)){
	      $lesson=ltrim(rtrim($lesson));
	      $subj=$row1["$dayf"];

		$mathima[$ind]=$lesson;
		$ar=explode('/',$subj);
		$subject[$ind]= $ar[1];
		$mathsubj[$ind]=$mathima[$ind]."/".$subject[$ind];
	    $ind++;
	}
		$per++;

}
    
	  $i++;
	 }

$mathsubj=array_unique($mathsubj);
$ind=$ind-1;
$arr = explode("/", $math);
$lesson = ltrim(rtrim($arr[0]));
$subject = ltrim(rtrim($arr[1]));
//*******************
/*
$day=array(" ","Mon","Tue","Wed","Thu","Fri");
$result = mysqli_query($con,"SELECT * FROM timetable WHERE Teacher='$name'  " ); 
$row = mysqli_fetch_array($result);
$k=0;
for ($i=1;$i<=5;$i++){
  for ($per=1;$per<=7;$per++){
    $dayf=$day[$i].$per;
    $m=$row["$dayf"];
    if ($m <> NULL and $m==$lesson) {
	      $k++;
		  $dday[$k]=$day[$i];
		  $dper[$k]=$per;	  
    }
  }
}
*************************************
*/
?>
<form  method='post'> 
  Επιλογή Μαθήματος :<select name='mathid' >
  <?php
  foreach ($mathsubj as $m){ 
          echo "<option value='$m'>$m</option>";
  }
  ?>
  </select>
<input type="submit" name="submitb" value="Επιβεβαίωση Επιλογής" >
</form>
<?php
if (isset($_POST['mathid']))
{
	$math=$_POST['mathid'];
	$arr = explode("/", $math);
    $lesson = ltrim(rtrim($arr[0]));
	$subject = ltrim(rtrim($arr[1]));
	if ($lesson=='Α3ΦΥΣ' or $lesson=='Α3ΦΥΣκατ')
	{
	$sql=mysqli_query($con,"select * from syllabi where pmp='$usr' and (mathima='Α3ΦΥΣ' or mathima='Α3ΦΥΣκατ')  order by date,per");	
	}
	else
	{
	$sql=mysqli_query($con,"select * from syllabi where pmp='$usr' and mathima='$lesson' order by date,per");
	
	}
echo "<br>"."<b>"."Προγραμμaτισμός για: ".$math."</b>";	
echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>α/α</th>
<th>Ημερομηνία</th>
<th>Περ</th>
<th>Περιγραφή Αναλυτικού</th>
<th>Απόντες/ούσες</th>
</tr>";
$i=1;
while($row=mysqli_fetch_array($sql))
{
  echo "<tr>";
  echo "<td>" .$i;
  echo "<td>". date('d-m-Y',strtotime($row['date']));$row['date'] . "</td>";
  echo "<td>". $row['per'] . "</td>";
  echo "<td>". $row['perigrafi'] . "</td>";
  $dd=$row['date'];
  $pr=$row['per'];
  $disp="";
  $sqlx = $dbcon->prepare("SELECT a.mitroo, al.onoma, al.eponimo FROM apousies as a,allstudentinfo as al WHERE (a.apousia=1 and a.kath IS NULL) AND a.dateout=?
  and period=? AND a.mitroo=al.mitroo AND a.teacher=? AND a.mathima=? AND (a.mitroo not in (select mitroo from apousies where kath > 0 and dateout=? and a.period=?)) " );	 
  $sqlx->execute([$dd,$pr,$usr,$lesson,$dd,$pr]);
  while($rowx=$sqlx->fetch(PDO::FETCH_ASSOC)) 
	{
      $disp=$disp.$rowx['eponimo']." ".$rowx['onoma']."-(".$rowx['mitroo']."),";
	  
	}
	$disp=rtrim($disp, ",");
	echo "<td>". $disp . "</td>";
  $i++;
}
echo "</table>";	
}
?>
</body>
</html>