<?php
session_start();
$menu = $_SESSION['menu'];
include "logo.php";
date_default_timezone_set("Europe/Athens");
?>

<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<head>
</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=800, height=800, 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:14px; }
<p style="font-size:14px;"></p>
</style>
</head>
<body>
<h4>ΕΠΙΛΟΓΗ ΤΜΗΜΑΤΟΣ ΓΙΑ ΔΔΚ</h4>
<?php
require "dbconnect.php"; 
$usr=$_SESSION['usr'];
$_SESSION['link1']="ddktmimalist1.php";

$result = $dbcon->prepare("SELECT access FROM teachers WHERE PMP=? and (access >1 and access <=4)" );
$result->execute([$usr]);
$row=$result->fetch(PDO::FETCH_ASSOC);
$access=$row['access'];
$c=$result->rowCount();
if ($c > 0)
{
	if ($access==2)
	{
	    $result = $dbcon->prepare("SELECT Class_id FROM voithos WHERE voi_id=? " );
	    $result-> execute([$usr]);
		$c1=$result->rowCount();
	}
	else
	{
		$result = $dbcon->prepare("SELECT DISTINCT tmima FROM allstudentinfo order by tmima" );
	    $result->execute([]);
		$c1=$result->rowCount();
	}
	?>
	<form method='post'>
    
    <label>Τμήμα:</label>
	<select name='tmimaid' id='tmimaid'>
    <?php
	echo "<option value='---'>select</option>";
    while($row=$result->fetch(PDO::FETCH_ASSOC)) 
	{
		if ($access==2)
		{
			echo "<option value='$row[Class_id]'>$row[Class_id]</option>";	
		}
		else
		{
          echo "<option value='$row[tmima]'>$row[tmima]</option>";
		}
    }
    ?>
    </select>
	<input type="submit" name="submit1" value="Επιβεβαίωση Επιλογής" >
	</form>
	<?php
if (isset($_POST['tmimaid'])) 
{
    $tmima=$_POST['tmimaid'];
	$result = $dbcon->prepare("SELECT mitroo,eponimo,onoma FROM allstudentinfo WHERE tmima=? order by eponimo,onoma" ); 
	$result->execute([$tmima]);   
	echo "TMHMA : ".$tmima."<br>"."<br>";
	echo "<table class='myTable' >
	<tr style='border:2px solid black' >
	</tr>";
	echo "<tr > 
	<th>Α/Α</th>
	<th>Μητρώο</th>
	<th>Ονοματεπώνυμο Μαθητή/τριας</th>
	<th>Σύνολο Μονάδων</th>
	</tr>";

	$i=1;
	 while($row=$result->fetch(PDO::FETCH_ASSOC))
		 {
		   $mitroo[$i]=$row['mitroo'];
		   $res1 = $dbcon->prepare("SELECT * FROM ddk WHERE mitroo=? ");
		   $res1->execute([$mitroo[$i]]);
		   $row1=$res1->fetch(PDO::FETCH_ASSOC); 
		   $n=$res1->columnCount()-1;
		   $sum=0;
		   for ($j=1;$j<=$n;$j++)
			  {	
				 $field="f".$j;
				 $sum=$sum+$row1["$field"];
			  }
		  $name[$i]=$row['eponimo']." ".$row['onoma'];
		  echo "<td>" . $i. "</td>";
		  echo "<td>" . $mitroo[$i]. "</td>";
		 ?>
		 <td>
	   <a href='ddkbathmos.php?mitr=<?php echo $mitroo[$i]; ?>')"><?php echo $name[$i]; ?></a>
		</td>
	<?php

		  echo "<td>" . $sum. "</td>";
		  echo "</tr>";
		  $i++;
     }
	
echo "</table>";  
}
}
?>
<br />
<a  href=<?php echo $menu; ?>  style="color:blue "  >  Επιστροφή στο Μενού  </a>

</body>
</html>