<?php
include "scrinit.php";
include $menu;
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function validate () 
{
  if(document.getElementById('mes').value=="") 
  {
    alert("You need to complete message");
    return false;
  }
  return true;
}
</script>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:1px;font-size:10pt; }
<p style="font-size:10px;"></p>
</style>

<body>
<h3> ΑΠΟΣΤΟΛΗ SMS ΓΙΑ ΔΡΑΣΤΗΡΙΟΤΗΤΑ </h3>
<?php
require("dbconnect.php");
$usr=$_SESSION['usr'];
$dt=$_SESSION['dt'];
$ac=$_SESSION['access'];

echo "<BR>"."<b>"."EΠΕΛΕΞΕ ΤΗ ΔΡΑΣΤΗΡΙΟΤΗΤΑ:"."</b>"."<br>"."<br>";
if ($ac < 3 ) 
{
	$sql = "SELECT onoma,code,date FROM ekdilosi WHERE teacher='$usr' AND katddk<>0  order by date DESC";
} 
if ($ac <= 4 and $ac >=3) 
{ 
   $sql = "SELECT onoma,code,date FROM ekdilosi WHERE katddk<>0 order by date desc";
}
$result = $dbcon->prepare($sql);
$result->execute();

?>
<form  method="post" /> 

<?php
echo "<select name='drast'>";
while ($row =$result->fetch(PDO::FETCH_ASSOC)) 
{
	$ekdilo=$row['onoma'];
	$code=$row['code'];
	$dat= date('d-m-Y', strtotime($row['date']));
   	echo "<option value ='".$code."'>".$dat." / ".$ekdilo." (".$code.") "."</option>";
}
echo "</select>";
?>
<br><br>
<p>	
<h4>ΑΠΟΣΤΟΛΗ ΜΗΝΥΜΑΤΩΝ ΣΕ:</h4>
<input type="radio" name="pousms" value="1">ΜΑΘΗΤΕΣ/ΤΡΙΕΣ
<input type="radio" name="pousms" value="2">ΓΟΝΕΙΣ
</p>
<input type="Submit" name="ekd1" value="Επιλογή Δραστηριότητας" >
</form>	
<br><br>
<?php

if (isset($_POST['ekd1'])) 
{

$dat=$dt;
$codex=$_POST['drast'];
$psms=$_POST['pousms'];
$sql = $dbcon->prepare("SELECT e.pmp,a.eponimo,a.onoma,a.tmima,a.mitroo,a.kmathiti,a.kinito FROM ekdilosistu as e,allstudentinfo as a WHERE a.mitroo=e.pmp and e.code=? ");
$sql->execute([$codex]);
$c=$sql->rowCount();
$pr=1;

if ($c > 0)  
{  
?>	
<h3> ΑΠΟΣΤΟΛΗ ΜΗΝΥΜΑΤΩΝ</h3>
<form method="POST"  >
<?php

echo "<BR>"."<b>"."ΚΑΤΑΛΟΓΟΣ ΜΑΘΗΤΩΝ/ΤΡΙΩΝ "."</b>";
echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>Α/Α</th>
<th>Α.Μητρ</th>
<th>Τμήμα</th>
<th>Όνομα</th>
<th>Κιν. Μαθητή</th>
<th>Κιν. Κηδεμόνα</th>
</tr>";

$cc=0;

while($row=$sql->fetch(PDO::FETCH_ASSOC)) 
{
	$mt[$cc]=$row['mitroo'];
	$tmima[$cc]=$row['tmima'];
	$name[$cc]=$row['eponimo']." ".$row['onoma'];
	$kinito[$cc]=trim($row['kinito']);
	$kmathiti[$cc]=trim($row['kmathiti']);
	echo "<td>". $cc . "</td>";
	echo "<td>". $mt[$cc]."</td>";
	echo "<td>". $tmima[$cc]."</td>";
	echo "<td>". $name[$cc]."</td>";
	echo "<td>". "<input type='text' name='telefm[]' value='".$kmathiti[$cc]."' size='10'>"."</td>";
	echo "<td>". "<input type='text' name='telefk[]' value='".$kinito[$cc]."' size='10'>"."</td>";
	if ($psms=='1')
	{
		$kin[$cc]=$kmathiti[$cc];
	}
	else
	{
	    $kin[$cc]=$kinito[$cc];	
	}
	if (!empty($kin[$cc]))
	{
	   $x=$kin[$cc]."/".$mt[$cc]."/".$name[$cc];
	   echo "<td>". "<input type='checkbox' name='tsms[]' value='$x'  checked>" ."</td>";
	}
	else
	{
		$x=$kin[$cc]."/".$mt[$cc]."/".$name[$cc];
	    echo "<td>". "<input type='checkbox' name='tsms[]' value='$x'  unchecked>" ."</td>";	
	}
	echo "</tr>"; 
    $cc++;
}
echo "</table>";
echo "<p>";
$sms=" ";
echo "<br >"."MHNYMA :"."<br >";; 
echo "<textarea id='mes' name='sms' cols='80' rows='3'> $sms </textarea >"."<br >";	
?>
<br>
<input type="hidden" value="<?php echo $psms; ?>" name="psms">
<input type="hidden" value="<?php echo $codex; ?>" name="codex">
<input type="submit" value="Αποστολη SMS " name="submit" onsubmit='return validate ()'>
</form>

<?php

}
else
   echo "Δεν υπάρχουν εγγραφές με αυτή την ημερομηνία";
}

if (isset($_POST['submit'])) 
{
	$tm=array();
	$i=0;
	$sms1=$_POST['sms'];
	$tel=$_POST['tsms'];
	$psms=$_POST['psms'];
	$codex=$_POST['codex'];
	if ($psms==1){$rec='ΜΑΘΗΤΕΣ-ΤΡΙΕΣ';} ELSE {$rec='ΓΟΝΕΙΣ';}
	if (count($tel) > 0)
	{
	echo "<p>"."ΘΑ ΣΤΑΛΕΙ ΤΟ ΑΚΟΛΟΥΘΟ ΜΗΝΥΜΑ ΣΤΟΥΣ ".$rec." :"."</p>";
	echo "<p>".$sms1."</p>";
	echo "<table class='myTable' >
	<tr style='border:1px solid black' bgcolor='#d6ff73'  >
	<th>Α/Α</th>
	<th>Α.Μητρ</th>
	<th>Όνομα</th>
	<th>Τηλέφωνο</th>
	";
	
	foreach ($tel as $t)
	{
	   $ar=explode("/",$t);
	   $tm[$i]=$ar[0];
	   $mit[$i]=$ar[1];
	   $nam[$i]=$ar[2];
	   echo "<tr>"."<td>".$i."<td>".$mit[$i]."<td>".$nam[$i]."<td>".$tm[$i]."</tr>";
	   $i++;
	}	   
	echo "</table>";
	echo "<br>";
	
	$_SESSION['sms1']=$sms1;
	$_SESSION['tel']=$tm;
	$_SESSION['onoma']=$nam;
	$_SESSION['fl']="dra".$codex;
	include "smssecret.php";
	}
	else
	{
		echo "Δεν υπάρχουν μηνύματα";
	}
}

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