<?php 
include "scrinit.php";
$menu = $_SESSION['menu'];
include $menu;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">

    function do_this(){

        var checkboxes = document.getElementsByName('smsx[]');
        var button = document.getElementById('toggle');

        if(button.value == 'Επιλογή Όλων'){
            for (var i in checkboxes){
                checkboxes[i].checked = 'FALSE';
            }
            button.value = 'Αποεπιλογή Όλων'
        }else{
            for (var i in checkboxes){
                checkboxes[i].checked = '';
            }
            button.value = 'Επιλογή Όλων';
        }
    }
</script>
<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>


<?php 

require("dbconnect.php");
$usr=$_SESSION['usr'];
$access=$_SESSION['access'];
$dt=$_SESSION['dt'];
$dtc= date('d-m-Y', strtotime($dt));
echo "<p>"."ΗΜΕΡΟΜΗΝΙΑ: ".$dtc."</p>";
$result = mysqli_query($con,"SELECT tmima,eponimo,onoma,mitroo FROM allstudentinfo order by tmima,eponimo,onoma");
$rp=mysqli_query($con,"select mitroo,date from dailyrapid");
$rm = array();
$i=1;
while ($row = mysqli_fetch_array($rp))
{
    $rm[$i] = $row['mitroo'];
    $dat1=$row['date'];
	$i++;
}
if (isset($_POST['dat'])) $dat1=$_POST['dat'];
?>
<h4> ΕΠΕΛΕΞΕ ΤΟΥΣ ΜΑΘΗΤΕΣ/ΤΡΙΕΣ ΠΟΥ ΚΑΝΟΥΝ RAPID TEST - ΧΡΗΣΗ ΜΟΝΟ ΤΗΝ ΗΜΈΡΑ ΠΟΥ ΕΡΧΕΤΑΙ ΤΟ ΣΥΝΕΡΓΕΙΟ </h4>
<form  method='post'>
<div style="overflow:scroll; border:1px solid black;background:#F5F9F0;width:700px; height: 600px">
<p><b>ΗΜΕΡ/ΝΙΑ ΔΙΕΞΑΓΩΓΗΣ ΤΕΣΤ: <input type="date" name="dat" value='<?php echo $dat1;?>' required></b>
&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp <input type="submit" name="emptyt" value="Άδειασμα Πίνακα-Στην αρχή μόνο" 
style='color:blue;font-size:14px;border:solid' onClick='return confirm("Να προχωρήσει το άδειασμα του πίνακα;")' >

</p>
<?php
echo "<table style='width:680px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#adcd82'  >
<th>A/A</th>
<th>ΤΜΗΜΑ</th>
<th>ΜΑΘΗΤΗΣ/ΜΑΘΗΤΡΙΑ</th>
<th>ΕΜΒΟΛΙΟ</th>
<th>ΝΟΣΗΣΗ</th>
<th>RAPID TEST</th>
<th></th>
</tr>";
$i=1;

while ($row = mysqli_fetch_array($result)) 
  { 
    $name=$row['eponimo']." ".$row['onoma'];
	$mit=$row['mitroo'];
	$tmima=$row['tmima'];
	$rd=mysqli_query($con,"select * from rapidimg where mitroo='$mit'");
	$row1=mysqli_fetch_array($rd);
	$mpoli='';$fl=0;
	if ($row1['mpoli']==1) {$mpoli='ΕΜΒΟΛΙΑΣΜΕΝΟΣ/Η';$fl=1;}
	$dcov='';
	if ($row1['covid'] > 0) {$dcov=date('d-m-Y', strtotime($row1['covid']));$fl=1;}
	echo "<tr>";
	echo "<td>".$i."</td>";
	echo "<td>".$tmima."</td>";
	echo "<td>".$name."</td>";
	echo "<td>".$mpoli."</td>";
	echo "<td>".$dcov."</td>";
	echo "<td >".$row1['testdate']."</td>";
	$ck='';
	if (in_array($mit,$rm) ) $ck='checked';
	if ($fl==0)
	{
    echo "<td ALIGN=center style='background-color:#ff0000'>"."<input type='checkbox' name='rtest[]' value='$mit'".$ck.">"."</td>";
	}
	else
	{
    echo "<td ALIGN=center >"."<input type='checkbox' name='rtest[]' value='$mit'".$ck.">"."</td>";
	}	
	echo "</tr>";
    $i++;
}
echo "</table>";

?>
</div>
<p> </p>
<input type="submit" name="rapidt" value="Καταχώριση Επιλογών" >
&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp
<input type="submit" name="rapidup" value="Καταχώριση Στο Αρχείο Τμήματος" >
</form>
<?php

if (isset($_POST['rapidt']))
{
   $tst=$_POST['rtest'];
   $dat=$_POST['dat'];
   mysqli_query($con,'TRUNCATE TABLE dailyrapid;');
   foreach ($tst as $mit)
   {
	   $q="insert into dailyrapid (mitroo,date) values ('$mit','$dat')";
	   if (!mysqli_query($con, $q)) 
		{
            echo "Error: " . $q . "<br>" . mysqli_error($con);
        }
   }
   header("Location: rapidtests.php");
}
if (isset($_POST['emptyt']))
{
	mysqli_query($con,"truncate dailyrapid");
	header("Location: rapidtests.php");
}
if (isset($_POST['rapidup']))
{
	//$dt=$_SESSION['dt'];
	$dat=$_POST['dat'];
	$rp=mysqli_query($con,"select mitroo from dailyrapid");
	$j=0;
	while ($row = mysqli_fetch_array($rp))
	{
		$rm= $row['mitroo'];
		
		$qx=mysqli_query($con, "select mitroo from  rapidimg where mitroo='$rm'");
		$rowx=mysqli_fetch_array($qx);
		if (!empty($rowx))
		{
			
			$q="update rapidimg set testdate='$dat' where mitroo='$rm'";
			if (!mysqli_query($con, $q)) 
			{
	            echo "Error: " . $q . "<br>" . mysqli_error($con);
			}
		}
		else
		{
			$q="insert into rapidimg (testdate, mitroo) values ('$dat', '$rm')";
			if (!mysqli_query($con, $q)) 
			{
	            echo "Error: " . $q . "<br>" . mysqli_error($con);
			}
		}
		$j++;
	}
	
	echo " ΕΝΗΜΕΡΩΘΗΚΑΝ ".$j." ΕΓΓΡΑΦΕΣ";
	
}

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