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

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">


function atleast_onecheckbox(e) 
{
  var empt = document.forms["selectpoini"]["pini"].value;
  if ($("input[type=checkbox]:checked").length === 0) {
      e.preventDefault();
      alert('Πρέπει να επιλέξετε τις καθυστερήσεις που θα τιμωρηθούν');
      return false;
  }
  if (empt == "")
  {
     alert("Το πεδίο της ποινής δεν μπορεί να είναι κενό!");
     return false;
  }
}


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=800, height=800, top='+top+', left='+left);
}

function pop_up(url)
{
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=800,height=800,directories=no,location=no') 
}
</script>

<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:9pt; }
<p style="font-size:09px;"></p>
</style>
</head>
<body>
<?php

require("dbconnect.php");
require_once "functions.php";
$dt=$_SESSION['dt'];
$name = $_SESSION['n'];
$mitr=$_GET['am'];
dispst($mitr);
$sql = $dbcon->prepare("SELECT * FROM apousies  
	   WHERE   kath > 0 AND mitroo=? and timoriacode > 0 ORDER BY timoriacode,dateout,period");
$sql->execute([$mitr]);
$count=$sql->rowCount();
if ($count==0)
{
	echo " Ο μαθητής δεν έχει τιμωρημένες καθυστερήσεις";
}
else
{
	
	?><p style="color: red"> Τιμωρημένες καθυστερήσεις  </p><?php
	echo "<table class='myTable' >
	<tr style='border:1px solid black' >
	<th>Α/Α</th>
	<th>Ημερομηνία</th>
	<th>Περίοδ.</th>
	<th>Καθυστ.</th>
	<th>Κωδικός</th>
	<th>Είδος Ποινής</th>
	<th>Ποινή</th>
	</tr>";
	$i=0;
	$row=$sql->fetch(PDO::FETCH_ASSOC);
	
	while(!empty($row))
	{	 
	     $tcode=$row['timoriacode'];
		 $sql1 = $dbcon->prepare("SELECT * FROM timoria WHERE timoriacode = ? ");
		 $sql1->execute([$tcode]);
		 $row1=$sql1->fetch(PDO::FETCH_ASSOC);
		 $pini=$row1['peramount'];
		 $etimo=$row1['eidostimo'];
		 if ($etimo == 0 and $pini==0) {$epini='ΠΑΙΔ. ΔΙΑΛΟΓΟΣ';}
		 if ($etimo == 0 and $pini > 0) {$epini='ΜΕ ΑΝΑΣΤΟΛΗ';}
		 if ($etimo ==1 ){$epini='ΚΑΤ΄ ΟΙΚΟΝ';}
		 if ($etimo == 2) {$epini='ΑΠΟΒΟΛΗ';}
		 while($tcode==$row['timoriacode'])
		 {
		     $per=$row['period'];
		     $datet=$row['dateout'];
		     $kath=$row['kath'];
		     $tcode=$row['timoriacode'];
			 echo "<tr>";
			 echo "<td>" .$i;
			 echo "<td>" .date('d-m-Y', strtotime($datet));
			 echo "<td>" .$per;
			 echo "<td>". $kath;
			 echo "<td>". "";
			 echo "<td>". "";
			 echo "<td>". "";
			 echo  "</tr>";
			 $row=$sql->fetch(PDO::FETCH_ASSOC);
		 }
			 echo "<tr>";
			 echo '<td colspan="4">' ." ";
			 echo "<td>". $tcode;
			 echo "<td>". $epini;
			 echo "<td>". $pini;
			 echo  "</tr>";
			 $i++;
	}
	echo "</table>";	
}

$sql = mysqli_query($con,"SELECT * FROM apousies  
	   WHERE  kath > 0 AND mitroo='$mitr' and (timoriacode = 0 or timoriacode is NULL) ORDER BY dateout,period" );
if (mysqli_num_rows($sql)==0)
{
	echo "<p>"."Ο μαθητής δεν έχει ατιμώρητες καθυστερήσεις"."</p>";
}
else
{	
	$kth=array();
/*<input type="button" style="color:blue " onclick="window.open('SistimatikiKathisterisiBD.php?mitr=<?php echo $mitr;?>','','width=400,height=200')" value="Κλίκ για Έλεγχο συστηματικής καθυστέρησης" /><br>*/	
?>	
<p style="color: red"> Να επιλέξετε τις καθυστερήσεις που θα τιμωρηθούν </p>
<form method="POST"  onsubmit="return atleast_onecheckbox(event)" name="selectpoini" action="poinikath.php">
<div style="overflow:scroll; border:1px solid black;background:#F5F9F0;width:340px; height: 300px">
<?php

echo "<table style='width:300px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#adcd82'  >
<th>A/A</th>
<th>ΗΜΕΡΟΜΗΝΙΑ </th>
<th>ΠΕΡ.</th>
<th>ΚΑΘ/ΣΗ</th>
<th></th>
<th>ΕΠΙΛΟΓΗ</th>
</tr>";
$i=1;
while ($row = mysqli_fetch_array($sql)) 
  { 
   	$kth[$i]=$row['kath']."/".$row['period']."/".$row['dateout'];
	echo "<tr>";
	echo "<td>".$i."</td>";
	echo "<td>".date('d-m-Y', strtotime($row['dateout']))."</td>";
	echo "<td>".$row['period']."</td>";
	echo "<td>".$row['kath']."</td>";
	echo "<td>"." "."</td>";
	$dis='';
	
    echo "<td ALIGN=center>"."<input type='checkbox' name='kath[]' id= 'kth1' value='$kth[$i]' >"."</td>";
		
	echo "</tr>";
    $i++;
}
echo "</table>";
$etimo="";
?>
</div>
<input type='hidden' name='mitroo' value='<?php echo $mitr; ?>' >
<p>
ΠΟΙΝΗ ΣΕ ΠΕΡΙΟΔΟΥΣ:<input type='text'  name= 'pini' value="<?php echo $pini; ?>" size=9 />

<Input type = 'Radio' name ='etimo' value= 0 <?php if ($etimo==0) {echo 'checked';} else {echo "";}?> >ΜΕ ΑΝΑΣΤΟΛΗ/ΑΛΛΗ</td>
<Input type = 'Radio' name ='etimo' value= 1 <?php if ($etimo==1) {echo 'checked';} else {echo "";}?> >ΚΑΤ' ΟΙΚΟΝ </td>
<Input type = 'Radio' name ='etimo' value= 2 <?php if ($etimo==2) {echo 'checked';} else {echo "";}?> >ΑΠΟΒΟΛΗ </td>
</p>
<input type="submit" name="subpini" value="Επιβεβαίωση Επιλογών"  />
<br>
<?php
if (isset($_POST['subpini']))
{
	$pini=$_POST['pini'];
	$mitroo=$_POST['mitroo'];
	$argo=$_POST['kath'];
	$vd=$_SESSION['usr'];
	$etimo = $_POST['etimo'];
    $_SESSION['eidostimo']=$etimo;
	if ($etimo == 0 and $pini==0) {$epini='ΠΑΙΔ. ΔΙΑΛΟΓΟΣ';}
		 if ($etimo == 0 and $pini > 0) {$epini='ΜΕ ΑΝΑΣΤΟΛΗ';}
		 if ($etimo ==1 ){$epini='ΚΑΤ΄ ΟΙΚΟΝ';}
		 if ($etimo == 2) {$epini='ΑΠΟΒΟΛΗ';}
	?>
	<h4>ΘΑ ΤΙΜΩΡΗΘΟΥΝ ΟΙ ΠΙΟ ΚΑΤΩ ΚΑΘΥΣΤΕΡΗΣΕΙΣ ΜΕ ΠΟΙΝΗ : <?php echo $pini." ΠΕΡΙΟΔΩΝ ".$epini; ?>   </h4>
	<?php
	$per=$datet=$kath=$dat=array();
	echo "<table class='myTable' >
	<tr style='border:1px solid black' >
	<th>Α/Α</th>
	<th>Ημερομηνία</th>
	<th>Περίοδ.</th>
	<th>Καθυστ.</th>
	</tr>";
	$i=0;
	foreach ($argo as $t)
	{
	 $i++;
	 $ar=explode("/",$t);
	 $per[$i]=$ar[1];
	 $datet[$i]=$ar[2];
	 $dat[$i]=date('d-m-Y', strtotime($ar[2]));
	 $kath[$i]=$ar[0];
	 echo "<tr>";
	 echo "<td>" .$i;
	 echo "<td>" .$dat[$i];
	 echo "<td>" .$per[$i];
	 echo "<td>". $kath[$i];
     echo  "</tr>";
	}
	echo "</table>";	
	?>
	
    <?php
	$sql = mysqli_query($con,"SELECT MAX(timoriacode) FROM timoria " );
    $row = mysqli_fetch_array($sql);
    $mtim=$row['MAX(timoriacode)']+1;

    $q="INSERT INTO timoria (timoriacode, stu_id, dateappl, peramount,vd,eidostimo) 
	             VALUES ('$mtim', '$mitroo','$dt', '$pini','$vd','$etimo') ";
    if (mysqli_query($con, $q)) 
		{
			echo "<p>"."Η ποινή ".$pini." περιόδων έχει καταχωρηθεί "."</p>";
        } 
		else 
		{
            echo "Error: " . $query . "<br>" . mysqli_error($con);
        }
    for ($k=1;$k<=$i;$k++) 
	{
        $q="UPDATE apousies SET timoriacode='$mtim' 
		WHERE mitroo='$mitroo' AND dateout='$datet[$k]' AND period='$per[$k]' AND kath > 0 " ; 
		   if (mysqli_query($con, $q)) 
		{
			
        } 
		else 
		{
            echo "Error: " . $query . "<br>" . mysqli_error($con);
        }
    }
 
    
	
}
}
?>
<br /><br />
<input type="button" name="epistrofi" value="Επιστροφή" onClick="self.close()"> 
</body>
</html>