<?php 
include "scrinit.php";
include $menu;
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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>
<title>SMS Πρώτης Περιόδου</title>
</head>
<body>
<?php 

require("dbconnect.php");
$usr=$_SESSION['usr'];
$access=$_SESSION['access'];
$dt=$_SESSION['dt'];
$gym=$_SESSION['gym'];
if ($access <3 ) { echo " *** ΜΗ ΔΙΑΘΕΣΙΜΕΣ ΑΝΑΦΟΡΕΣ *** ";}
else
{

$sql = $dbcon->prepare("SELECT a.dateout, a.mitroo, a.period,al.tmima,al.eponimo,al.onoma,al.kinito 
FROM apousies as a,allstudentinfo as al WHERE (a.apousia=1 and a.kath IS NULL ) AND a.dateout=? AND 
(a.mitroo=al.mitroo) AND (a.mitroo not in (select mitroo from apousies where kath > 0 and dateout='$dat' and a.period=period )) ORDER BY al.tmima,a.mitroo,a.period " );

$sql->execute([$dt]);
$c=$sql->rowCount();

$pr=1;

if ($c > 0)  
{  
$sqlo = $dbcon->prepare("SELECT s.tel,a.mitroo,a.eponimo,a.onoma,a.tmima FROM smsout as s,allstudentinfo as a
        WHERE date(s.datetm) =  ? and s.tel=a.kinito and s.smscode in (select smscode from smsoutmes where source='proi')");		
$sqlo->execute([$dt]);
$count=$sqlo->rowCount();
$mits=array();
if ($count>0)
{
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></tr>";
$i=1;
while($rowo=$sqlo->fetch(PDO::FETCH_ASSOC)) 
{
	echo "<tr>";
	$mits[$i]=$rowo['mitroo'];
	$namex=$rowo['eponimo']." ".$rowo['onoma'];
	echo "<td>". $i . "</td>";
	echo "<td>".$mits[$i]."</td>";
	echo "<td>". $rowo['tmima']."</td>";
	echo "<td>". $namex."</td>";
	echo "<td>". $rowo['tel']."</td>";
	echo "</tr>";
	$i++;
}
echo "</table>";
}
?>	
<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>";
for ($i=1;$i<=$gym;$i++)
{
	$titep="Περ".$i;
	echo "<th>".$titep."</th>";
}
echo "<th>Τηλέφωνο</th>
</tr>";

$row=$sql->fetch(PDO::FETCH_ASSOC);
$cc=0;
$bname = "submit" ;
while(!empty($row)) 
{
	$per=$pera=array();
	$mt[$cc]=$row['mitroo'];
	$tmima[$cc]=$row['tmima'];
	$name[$cc]=$row['eponimo']." ".$row['onoma'];
	$kinito[$cc]=trim($row['kinito']);
	echo "<td>". $cc . "</td>";
	echo "<td>". $mt[$cc]."</td>";
	echo "<td>". $tmima[$cc]."</td>";
	echo "<td>". $name[$cc]."</td>";
	while ($mt[$cc]==$row['mitroo'])
	  {
	     $per[$row['period']]="X";
	     $row=$sql->fetch(PDO::FETCH_ASSOC);
      }
	
	//.....elexos gia ekdiloseis tin parousa hmerominia
    $q=$dbcon->prepare("SELECT e.onoma,s.* FROM ekdilosi as e,ekdilosistu as s 
	                    WHERE e.date = ? and e.code=s.code and s.pmp=?");
    $q->execute([$dt,$mt[$cc]]);
    $c1=$q->rowCount();
	if ($c1 > 0 ) 
	{
      $row2=$q->fetch(PDO::FETCH_ASSOC);
	  $onomaekd=$row2['onoma'];  
	}
	else
	{
	  $onomaekd="";	
	}
	
	for ($i=1;$i<=$gym;$i++)
	{
			if($per[$i]<>"")
			{
				$pera[$i]="X";
			}
			else
			{
				$pera[$i]=" ";	
			}
			if ($c1 > 0 ) 
			{
			  $p="p".$i;  
			  if ($row2["$p"]==1) {$pera[$i]="*X*"; }
			}
		   echo "<td>". $pera[$i]."</td>";	
			
	}
	$x=$kinito[$cc]."/".$mt[$cc]."/".$name[$cc];
	echo "<td>". "<input type='text' name='telef[]' value='$kinito[$cc]' size='10'>"."<td>";
	if (in_array($mt[$cc],$mits) or $c1 > 0)
	{
	    echo "<td>". "<input type='checkbox' name='tsms[]' value='$x'  >" ."</td>";
	}
	else
	{
		echo "<td>". "<input type='checkbox' name='tsms[]' value='$x'  checked>" ."</td>";	
	}
	echo "<td>". $onomaekd."</td>";
	$bname=" Αποστολη SMS ";
	echo "</tr>"; 
    $cc++;
}
echo "</table>";
$sql=mysqli_query($con,"SELECT tel from generalinfo");
$row=mysqli_fetch_array($sql); 
$telefon=$row['tel'];
echo "<br >"."MHNYMA :"."<br >"; 	
$sm1="Το παιδί σας";
$sm3=" <ονομα μαθητή/tριας> ";
$sm2=" απουσιάζει σήμερα. Παρακαλούμε όπως μας ενημερώνετε έγκαιρα στο τηλ:".$telefon;
echo $sm1.$sm3. "<textarea name='sm2' cols='80' rows='2'> $sm2 </textarea >"."<br >";
?>
<input type="hidden" value="<?php echo $sm1 ; ?>" name="sm1">
<br>
<input type="submit" value="<?php echo $bname ; ?>" name="submit">
</form>


<?php

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

}

if (isset($_POST['submit'])) 
{
	
	$tm=array();
	$i=0;
	$tel=$_POST['tsms'];
	if (count($tel) > 0)
	{
	echo "<p>"."ΘΑ ΣΤΑΛΟΥΝ ΜΗΝΥΜΑΤΑ ΣΤΟΥΣ:"."</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>"."<td>".$tm[$i]."</td>"."<td>".$nam[$i]."</tr>";
	   $i++;
	}	   
	echo "</table>";
	echo "<br>";
	
	$sm1=$_POST['sm1'];
	$sm2=$_POST['sm2'];
	$telef=$_POST['telef'];
	$sms=$sm1.$sm3.$sm2;
	echo "<br >"."MHNYMA :"."<br >";; 
	echo "<textarea name='sms' cols='80' rows='2'> $sms </textarea >"."<br >";
	$_SESSION['tel']=$tm;
	$_SESSION['onoma']=$nam;
	$_SESSION['sm1']=trim($sm1);
	$_SESSION['sm2']=trim($sm2);
	$_SESSION['fl']="proi";
	include "smssecret.php";    	
}
}
?>
<br /><br />
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού  </a>
</body>
</html>