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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Δήλωση Δραστηριότητας</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<style type="text/css">
table.o { border-collapse:collapse;border:solid 2px #00F ;}
table.o td {border:solid 2px #00F; padding:2px ; word-wrap:break-word;} 
table.o th { border:2px solid black; background-color: #CCF;padding:2px;font-size:10pt;word-wrap:break-word; }
<p style="font-size:10px;"></p>
</style>
</head>
<body>
<h3>ΔΗΛΩΣΗ ΕΠΙΤΡΟΠΩΝ</h3>
<p> Να επιλέξετε υποχρεωτικά tέσσερις επιτροπές κατά σειρά προτίμησης, βάζοντας στο αντίστοιχο κελί τον αριθμό 1-4.
Η επιλογή επιτροπών θα κλείσει την Κυριακή, 13/09/20. </p>
<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
if ($dt > '2020-09-13 11:59:00') { 
?>
<h2> ΤΟ ΣΥΣΤΗΜΑ ΕΠΙΛΟΓΗΣ ΕΠΙΤΡΟΠΩΝ ΕΧΕΙ ΑΠΕΝΕΡΓΟΠΟΙΗΘΕΙ ΠΡΟΣΩΡΙΝΑ</h2>
<?php
} 
else
{	
$name = $_SESSION['n'];
$pmp=$_SESSION['usr'];
$date=date('d-m-Y');
$datedisp= date('d-m-Y', strtotime($date)); 
echo "ΕΚΠΑΙΔΕΥΤΙΚΟΣ : "."<b>".$name."</b>"."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp"."ΗΜΕΡΟΜΗΝΙΑ:".$datedisp;"<br>";
echo "<br>"; 
$ep=$epc=array();
$sql=mysqli_query($con,"SELECT * FROM epitropes order by code" );
$c=mysqli_num_rows($sql);
$pere=array();
while ($row = mysqli_fetch_array($sql))
	{
		
		$cc[$row['code']]=$row['code'];
		$pere[$row['code']]=$row['perigrafi'];
		
	}

$coun=array();
foreach ($cc as $cd)
{	

	$sql = mysqli_query($con,"SELECT t.Name FROM teachers as t,dilosiep as d WHERE d.teach=t.PMP and code1='$cd' " );
	while ($row = mysqli_fetch_array($sql))
	{
			$coun[$cd]++;
	}
	$sql = mysqli_query($con,"SELECT t.Name FROM teachers as t,dilosiep as d WHERE d.teach=t.PMP and code2='$cd' " );
		while ($row = mysqli_fetch_array($sql))
	{
			$coun[$cd]++;
	}
	$sql = mysqli_query($con,"SELECT t.Name FROM teachers as t,dilosiep as d WHERE d.teach=t.PMP and code3='$cd' " );
	while ($row = mysqli_fetch_array($sql))
	{
			$coun[$cd]++;
	}
	$sql = mysqli_query($con,"SELECT t.Name FROM teachers as t,dilosiep as d WHERE d.teach=t.PMP and code4='$cd' " );
	while ($row = mysqli_fetch_array($sql))
		$coun[$cd]++;
	}

$sql1=mysqli_query($con,"select * from dilosiep where teach='$pmp'");
If (mysqli_num_rows($sql1) > 0)
 {
	$row1 = mysqli_fetch_array($sql1); 
    $ep[1]=$row1['code1'];
	$ep[2]=$row1['code2'];
	$ep[3]=$row1['code3'];
	$ep[4]=$row1['code4'];
 }
 
$sql=mysqli_query($con,"SELECT * FROM epitropes order by code" );
$c=mysqli_num_rows($sql);
?>
<form method="POST" >
 
<?php
echo "<table class='o' style='width:700px'>
<tr>
<th>ΚΩΔ.</th>
<th>ΕΠΙΤΡΟΠΗ</th>
<th>ΔΙΑΘΕΣΙΜΕΣ ΘΕΣΕΙΣ</th>
<th>ΕΠΙΛΟΓΕΣ</th>
</tr>";
$i=1;
$c1=$c2=$c3=$c4=0;
$ct=0;
while($row = mysqli_fetch_array($sql))
{
	$codex=$row['code'];
	$per=$row['perigrafi'];
	$max=$row['plithos'];
	$x=$coun[$codex];
	switch(true)
	{
    case ($codex==$ep[1]):
		$epilogi=1;
		$c1++;
        break;
    case ($codex==$ep[2]): 
		$epilogi=2;
		$c2++;
		break;
	case ($codex==$ep[3]):
		$epilogi=3;
		$c3++;
		break;
	case ($codex==$ep[4]):
		$epilogi=4;
		$c4++;
		break;
    default:
		$epilogi=" ";
		//$x=0;
		break;
	}

	
	 $d=$max-$x;	 
	 echo "<tr>";
	 echo "<td>" .$codex. "</td>";
	 echo "<td>". $per. "</td>";
	 echo "<td>". $d ." /".$max. "</td>";
	 echo "<td>";
	 ?><input type='text' name='epil[]' size='4' <?php if($d<-4) echo 'readonly'; ?> value= <?php echo $epilogi;?>   ></td><?php
	 echo "</tr>";
	}
echo "</table>";
}
?>
<input type='hidden' name='pmp' value=<?php echo $pmp;?> >
<br />
<input type="Submit" id="subepi" name="subepi" value="Καταχώρηση Επιλογής Επιτροπων">
</form>
<?php
if (isset($_POST['subepi']))
{
	$pmp=$_POST['pmp'];
	$ep=$_POST['epil'];
	$ep1=array_search(1,$ep)+1;echo $ep1;
	$ep2=array_search(2,$ep)+1;echo $ep2;
	$ep3=array_search(3,$ep)+1;echo $ep3;
	$ep4=array_search(4,$ep)+1;echo $ep4;
	$epx=array_unique($ep);
	sort($epx);
	$c=count($epx);
	if($c == 5 and ($epx[4] == 4))
	{
		mysqli_query($con,"delete from dilosiep where teach='$pmp' ");
		$sql1="insert into dilosiep (teach,code1,code2,code3,code4) values ('$pmp','$ep1','$ep2','$ep3','$ep4') ";	
		if (mysqli_query($con, $sql1)) {
			echo "οι επιλογές σας καταχωρίστηκαν με επιτυχία";
			header("Location: ".$_SERVER['PHP_SELF']);
		} 
		else 
		{
		   echo "Λάθος: " . $sql1 . "<br>" . mysqli_error($con);
		}
	}
	else
	{
	   echo "Λάθος: ΞΑΝΑΔΟΚΙΜΑΣΤΕ " ;	
	}
}
?>	
<br /><br />
<a  href=<?php echo $menu;?> style="background-color: #819FF7;font-weight: bold; color: #ffffff;padding:4px;text-decoration:none"  >  Ακύρωση και Επιστροφή στο Μενού  </a>

</body>
</html>