<?php
include "scrinit.php";
include $menu;
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

</head>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid green;padding:2px;font-size:10pt; }
<p style="font-size:10px;"></p>
</style>
<body>
<h3>ΑΠΟΝΤΕΣ ΚΑΘΗΓΗΤΕΣ-ΤΡΙΕΣ</h3>

<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
echo "Ημερομηνία: ".date('d-m-Y', strtotime($dt));
$name = $_SESSION['n'];
$name = stripslashes($name);
$gym=$_SESSION['gym'];
$day = date('D', strtotime($dt));
$sqanapl=$dbcon->prepare("SELECT * from apousieskath where datefrom >='$dt' and dateto >=$dt");
$sqanapl->execute([$dt]);
$pmpx=$namet=$df=$dtx=$pf=$pt=$reason=$adeia=$mred=$chk=$chk2=$chk3=array();
$i=1;
while ($rowa=$sqanapl->fetch(PDO::FETCH_ASSOC))
{
  $p=$rowa['pmp'];
  $pmpx[$p]=$rowa['pmp'];
  $df[$p]=$rowa['datefrom'];
  $dtx[$p]=$rowa['dateto'];
  if ($df[$p]==$dt or ($dt >= $df[$p] and $dt <= $dtx[$p]))
  {
	$mred[$p]='red';
  }
  
  $pf[$p]=$rowa['perfrom'];
  $pt[$p]=$rowa['perto'];
  $reason[$p]=$rowa['leptomeries'];
  $adeia[$p]=$rowa['notr'];
  if ($adeia[$p]==1) $chk2[$p]="checked"; 
  $anti[$p]=$rowa['antikatastasi'];
  if ($anti[$p]==1) $chk3[$p]="checked"; 
  $i++;	
}


?>
<h4> ΕΠΕΛΕΞΕ ΤΟΥΣ ΕΚΠΑΙΔΕΥΤΙΚΟΥΣ ΠΟΥ ΘΑ ΑΠΟΥΣΙΑΖΟΥΝ </h4>
<form   method='post' >
<div style="overflow:scroll; border:1px solid black;background:#F5F9F0;width:1150px; height: 550px">
<?php
echo "<table style='width:1130px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#adcd82'  >
<th>A/A</th>
<th>ΚΑΘΗΓΗΤΗΣ/ΚΑΘΗΓΗΤΡΙΑ</th>
<th>ΑΠΟΥΣΙΑ</th>
<th>ΑΠΟ ΗΜΕΡ</th>
<th>ΜΕΧΡΙ ΗΜΕΡ</th>
<th>ΑΠΟ ΠΕΡ</th>
<th>ΜΕΧΡΙ ΠΕΡ</th>
<th>ΛΟΓΟΣ</th>
<th>ΑΔΕΙΑ</th>
<th>ΑΝΤ/ΣΤΑΣΗ</th>
</tr>";
$namet=array();
$result = mysqli_query($con,"SELECT PMP,name FROM teachers where access <> 6 ORDER BY name ");
$i=1;
while ($row = mysqli_fetch_array($result)) 
  { 
    $namet[$i]=$row['name'];
	$p=$row['PMP'];
	if(in_array($p,$pmpx) )
	{
	   $chk[$i] = "checked"; 
	}
	else
	{
		$chk[$i] = "";
    }

	if ($mred[$p]=='red')
	{
		echo "<tr style='background-color:#FF0000'>";	
	}
	else
	{
		echo "<tr>";	
	}
	
	echo "<td>".$i."</td>";
	echo "<td >".$namet[$i]."</td>";
	echo "<td ALIGN=center>"."<input type='checkbox' id='fm1' name='apon[]' value='$p'  {$chk[$i]} >"."</td>";
	echo "<td>"."<input type='date' name='df1[$p]' value='$df[$p]'>"."</td>";
    echo "<td>"."<input type='date' name='dt1[$p]' value='$dtx[$p]'>"."</td>";
	echo "<td>"."<input type='text' id='fm2' name='pf1[$p]' value= '$pf[$p]' size='2'>"."</td>";
	echo "<td>"."<input type='text' name='pt1[$p]' value= '$pt[$p]' size='2'>"."</td>";
	echo "<td>"."<input type='text' name='reasx[$p]' value='$reason[$p]' size='50'>"."</td>";
    echo "<td ALIGN=center>"."<input type='checkbox' name='adeiax[]' value='$p' {$chk2[$p]}>"."</td>";
	echo "<td ALIGN=center>"."<input type='checkbox' name='antix[]' value='$p' {$chk3[$p]}>"."</td>";
	echo "</tr>";
    $i++;
}
echo "</table>";
?>
</div>
<br>
<input type="submit" name="aponk" value="Επιβεβαίωση Επιλογών" >
</form>
<?php
if (isset($_POST['aponk']))
{
 mysqli_query($con,"delete from apousieskath where datefrom='$dt' or dateto >=$dt ");
 $adeia=$anti=$ap=array();
 $ap=$_POST['apon'];
 $df=array_filter($_POST['df1']);
 $dt=$_POST['dt1'];
 $pf=$_POST['pf1'];
 $pt=$_POST['pt1'];
 $reason=$_POST['reasx'];
 $adeia=$_POST['adeiax'];
 $anti=$_POST['antix'];
print_r($df);
foreach($ap as $pmp)
{
	$ad=0;
	$ant=0;
	if (!empty($df)) 
	{
		if (isset($adeia)){ if(in_array($pmp, $adeia)) $ad=1;}
		if (isset($anti)){if (in_array($pmp, $anti)) $ant=1;}
		$dxf=$df[$pmp];
		$dxt=$dt[$pmp];
		$pxf=$pf[$pmp];if ($pxf=='') $pxf=1;
		$pxt=$pt[$pmp];if ($pxt=='') $pxt=7;
		$r=$reason[$pmp];
		echo $pmp;
	    $query="insert into apousieskath (pmp,datefrom,dateto,perfrom,perto,leptomeries,notr,antikatastasi) 
		values ('$pmp','$dxf','$dxt','$pxf','$pxt','$r','$ad','$ant') ";
          if (!mysqli_query($con, $query)) 
	  {
            echo "Error: " . $query . "<br>" . mysqli_error($con);
          }		
	}
}

header("Location: apontes.php");
}

?>
<br><br>
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού   </a>
</body>
</html>