<?php
include "scrinit.php";
include "logo.php";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Άδεια Εισόδου</title>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:3px;font-size:11pt; }
<p style="font-size:11px;"></p>
</style>
</head>

<body>
<pre><h2>         ΑΔΕΙΑ ΕΙΣΟΔΟΥ ΣΤΗΝ ΤΑΞΗ</h2></pre>

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

if (isset($_POST['nameid'])) {
	$mitr=$_POST['nameid'];
	$papo=$_POST['ora'];
	$d=$_POST['d'];
}
else
{
   echo '<script type="text/javascript">';
   echo 'alert("ΔΕΝ ΕΧΕΤΕ ΕΠΙΛΕΞΕΙ ΜΑΘΗΤΗ ΓΙΑ ΤΗΝ ΑΔΕΙΑ")';
   echo '</script>';	
   $time = 1; //Time (in seconds) to wait. 
   $url = "adeiaeisodou.php"; //Location to send to. 
   header("Refresh: $time; url=$url");	
}
/*
switch ($papo) {
    case 1:
        $kath=strtotime(date("H:i:s"))-strtotime('07:30:00');
        break;
    case 2:
        $kath=strtotime(date("H:i:s"))-strtotime('08:15:00');
        break;
    case 3:
        $kath=strtotime(date("H:i:s"))-strtotime('07:30:00');
        break;
}
*/
	
$tme=date("H:i:s");
$dat=date("Y:m:d");
$sql = mysqli_query($con,"SELECT eponimo,onoma,tmima,kinito FROM allstudentinfo WHERE mitroo='$mitr'  " );
$row = mysqli_fetch_array($sql);
$stname=$row['eponimo']." ".$row['onoma'];
$tmimaid=$row['tmima'];
//*********** gia sms
$tele=array($rowt['kinito']);
$name=$row['onoma'];
//************
$sql = mysqli_query($con,"SELECT e.code,ek.* FROM ekdilosistu as e,ekdilosi as ek 
       WHERE e.pmp='$mitr' and ek.code=e.code and ek.onoma='ΑΔΕΙΑ ΕΙΣΟΔΟΥ'" );
$row = mysqli_fetch_array($sql);
$c=mysqli_num_rows($sql);

echo "ΜΑΘΗΤΗΣ/ΤΡΙΑ : "."<b>".$stname."</b>"." ( ".$mitr.")".",  ".$tmimaid."<br>"."<br>";
if ($c>0){
echo " ΠΡΟΗΓΟΥΜΕΝΕΣ ΑΔΕΙΕΣ ΕΙΣΟΔΟΥ"."<br>";	
echo "<table class='myTable' >
<tr style='border:1px solid black;font-size:9px'  >
<tr>
<th>Αρ.</th>
<th>Ημερομ.</th>
<th>Βοηθός Διευθ.</th>
<th>Ωρα</th>
<th>ΠΕΡΊΟΔΟΣ</th>
</tr>";	

$sql = mysqli_query($con,"SELECT code FROM ekdilosistu WHERE pmp='$mitr' " );
$i=1;
while ($row = mysqli_fetch_array($sql))
{
$code=$row['code'];
$tmima=$row['tmima'];
$sql1 =mysqli_query($con,"SELECT e.*,t.Name FROM ekdilosi as e,teachers as t 
WHERE e.code='$code' AND e.onoma='ΑΔΕΙΑ ΕΙΣΟΔΟΥ' and e.teacher=t.PMP") ;

while ($row1 = mysqli_fetch_array($sql1))
    {
	  $date=$row1['date'];
	  $time=$row1['time'];
	  $perapo=$row1['perapo'];
	  $vd=$row1['Name']; 
	  echo "<tr>";
      echo "<td>" . $i. "</td>";
      echo "<td>". $date . "</td>";
      echo "<td>". $vd . "</td>";
	  echo "<td>". $time . "</td>";
      echo "<td>". $perapo . "</td>";
	  $i++;
	 }
echo "</tr>";
}
echo "</table>";
}
//......................................save to files	
$met=array_fill(1,9,0);
$ekdilo="ΑΔΕΙΑ ΕΙΣΟΔΟΥ";	
$sql="INSERT INTO ekdilosi (onoma,date,time,perapo,teacher) VALUES ('$ekdilo','$dat','$tme','$papo','$usr')";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));   }

$query = mysqli_query($con,"SELECT MAX(code) FROM ekdilosi ");
    $results = mysqli_fetch_array($query);
    $code = $results['MAX(code)'];
	for ($i=1;$i<=$gym;$i++)
    {
	   $met[$i]=0;
	   if ($i == $papo) $met[$i]=1;
    }
    $sql="INSERT INTO ekdilosistu (code,pmp,tmima,p1,p2,p3,p4,p5,p6,p7,p8,p9) VALUES 
    ('$code','$mitr','$tmimaid','$met[1]','$met[2]','$met[3]','$met[4]','$met[5]','$met[6]','$met[7]','$met[8]','$met[9]' )";

    if (!mysqli_query($con,$sql)) die('Error: ' . mysqli_error($con));
	
$sql = $dbcon->prepare("SELECT code,perapo,permexri FROM ekdilosi WHERE date = ? and onoma='ΑΔΕΙΑ ΕΞΟΔΟΥ'");
$sql->execute([$dt]);
$count=$sql->rowCount();
if ($count > 0)
{
	while ($row=$sql->fetch(PDO::FETCH_ASSOC))
	{
		$sql1 = $dbcon->prepare("SELECT * FROM ekdilosistu WHERE code = ? ");
		$sql1->execute([$row['code']]);
		$row1=$sql1->fetch(PDO::FETCH_ASSOC);
		if ($row1['pmp']==$mitr)
		{
			$pa=$row['perapo'];
			$pm=$papo-1;
			$cod=$row['code'];
			$i=1;
			for ($i=1;$i<=$gym;$i++)
			{
				$met[$i]=0;
				if ($i >= $pa AND $i <= $pm) $met[$i]=1;
			}
			$sqlx="update ekdilosistu set p1='$met[1]',p2='$met[2]',p3='$met[3]',p4='$met[4]',
			p5='$met[5]',p6='$met[6]',p7='$met[7]',p8='$met[8]',p9='$met[9]' where code='$cod' and pmp='$mitr' ";
            if (!mysqli_query($con,$sqlx)) die('Error: ' . mysqli_error($con));
			$sqlx="update ekdilosi set permexri='$pm' where date='$dt' and code='$cod'";
            if (!mysqli_query($con,$sqlx)) {die('Error: ' . mysqli_error($con));   }
		}			
	}
}	
//....................ends save


//echo "<br>"."Μαθητής/τρια:&nbsp;&nbsp"."<b>".$stname."</b>".str_repeat('&nbsp;',20)."Τάξη/Τμήμα: "."<b>".$tmimaid."</b>"."<br>"."<br>" ;

$day = date('D', strtotime($dt));
$daymap = array('Κυριακή', 'Δευτέρα', 'Τρίτη', 'Τετάρτη', 'Πέμπτη', 'Παρασκευή', 'Σάββατο');
$datedisp= date('d-m-Y', strtotime($dt)); 
$day = date('D', strtotime($dt));
$d = $daymap[date('w', strtotime($dt))];
echo "<br>"."<br>";
echo " ΤΡΕΧΟΥΣΑ ΑΔΕΙΑ ΕΙΣΟΔΟΥ ";
echo "<table class='myTable' >
<tr style='border:1px solid black;font-size:12px'  >
<tr>
<th>ΗΜΕΡΟΜΗΝΙΑ</th>
<th>ΩΡΑ ΠΡΟΣΕΛΕΥΣΗΣ ΓΙΑ ΑΔΕΙΑ</th>
<th>ΠΕΡΙΟΔΟΣ</th>
</tr>";
echo "<tr>";	
echo "<td>".$d." , ".$dt."</td>";
echo "<td>".str_repeat('&nbsp;',7).date("H:i:s")."</td>";
//echo "<td>".str_repeat('&nbsp;',3).$kath. " λεπτά"."</td>";
echo "<td>".str_repeat('&nbsp;',3).$papo."</td>";
echo "</tr>";
echo "</table>";
echo "<br>";
//**************************************************
/*
?>
<form name='smsadeia' action='smsadeiain.php' method='post' target="DoSubmit" onSubmit="DoSubmit = window.open('about:blank','DoSubmit','width=500,height=400');">
  <input type="hidden" name="act" value="run">
  <input type="hidden" name="act" value="run">
  <input type="submit" value="ΑΠΟΣΤΟΛΗ ΜΥΝΗΜΑΤΟΣ!">
</form>
<?php
*/

//**************************************************
echo "<br>";echo "<br>";
echo "O Bοηθός Διευθυντής "."<br>"."<br>"."<br>";
echo "............................"."<br>";
echo $namevd."<br>"."<br>"."<br>";
echo "-----------------------------------------------------------------------------------------------";

?>
<pre><h3>      ΕΠΙΒΕΒΑΙΩΣΗ ΑΠΌ ΓΟΝΕΑ / ΚΗΔΕΜΟΝΑ</h3></pre>

<PRE> Έλαβα γνώση για την πρωινή αργοπορία του παιδιού μου.

<PRE>Ημερομηνία : ..............     Υπογραφή : ............................</PRE>
<PRE>Τηλέφωνο   : ..............     Όνοματεπώνυμο: ............................</PRE>

</body>
</html>

<button onClick="window.print()">Εκτύπωση</button>