<?php
include "scrinit.php";
include $menu;
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:11pt; }
<p style="font-size:11px;"></p>
</style>
<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=700, height=600, top='+top+', left='+left);
}
</script>
</head>

<body>

<?php
require "dbconnect.php";
$dt=$_SESSION['dt'];
$usr=$_SESSION['usr'];
$gym=$_SESSION['gym'];

$sql = $dbcon->prepare("SELECT distinct tmima FROM allstudentinfo order by tmima asc ");
$sql->execute();
$ctm=$sql->rowCount();

echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>A/A</th>
<th>ΤΜΗΜΑ</th>
<th>ΠΜΠ ΥΤ</th>
<th>ΠΜΠ ΒΟΗΘΟΥ</th>";
?>
<h3> ΥΠΕΥΘΥΝΟΙ ΚΑΘΗΓΗΤΕΣ ΚΑΙ ΒΟΗΘΟΙ ΤΜΗΜΑΤΩΝ </h3>

<form method="post" >
<?php
$i=1;

while($row=$sql->fetch(PDO::FETCH_ASSOC))
{
		$tmima[$i]=$row['tmima'];
		$sql1 = $dbcon->prepare("SELECT * FROM voithos where Class_id=? ");
        $sql1->execute([$tmima[$i]]);
		$row1=$sql1->fetch(PDO::FETCH_ASSOC);
		if ($sql->rowCount() > 0)
		{
			$yt[$i]=$row1['yt'];
			$vd[$i]=$row1['voi_id'];
		}
		echo "<tr>";
		echo "<td>".$i. "</td>";
		echo "<td> <input type='text' name='tm[$i]' value='$tmima[$i]' readonly size='6'/> </td>";
		echo "<td> <input type='text' name='ytm[$i]' value='$yt[$i]' size='10'/> </td>";
		echo "<td> <input type='text' name='vdm[$i]' value='$vd[$i]' size='12'/> </td>";
		$i++;
}
echo "</tr>";
echo "</table>";
?>
	
<p><input type="submit" value="Καταχώριση Στοιχείων" name="submityt"></p>
<input type="button" value="ΠΜΠ ΒΟΗΘΩΝ" onclick="window.open('bdpmp.php','','width=400,height=600')" />
<?php
if(isset($_POST['submityt']))
{
	mysqli_query($con,'TRUNCATE TABLE voithos;');
	$tmima=$_POST['tm'];
	$yt=$_POST['ytm'];
    $vd=$_POST['vdm'];
	$c=count($tmima);
	try
    { 
	for ($i=1;$i<=$c;$i++)
	{
	   $sql = $dbcon->prepare("INSERT INTO voithos (Class_id,yt,voi_id) values (?,?,?) "); 	
       $sql->execute([$tmima[$i],$yt[$i],$vd[$i]]);	
	}
	}	
	catch(PDOException $e)
	{
	   handle_sql_errors($selectQuery, $e->getMessage());
	}
 
function handle_sql_errors($query, $error_message)
{
    echo '<pre>';
    echo $query;
    echo '</pre>';
    echo $error_message;
    die;
}
}
 ?>
<br /><br />
<a  href=<?php echo $menu; ?>  style="color:blue "  > <button>  Επιστροφή στο Μενού  </a>
</body>
</html>