<?php
include "scrinit.php";
include "logo.php";
?>

<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:3px;font-size:14pt; }
<p style="font-size:14px;"></p>
</style>

</head>
<body>
<?php 

require("dbconnect.php");
$gym=$_SESSION['gym'];


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.kinito,al.tmima,al.eponimo,al.onoma FROM apousies as a,allstudentinfo as al 
WHERE a.apousia=1 AND a.dateout=? AND a.mitroo=al.mitroo ORDER BY al.tmima,a.mitroo,a.period " );
$sql->execute([$dt]);
$c=$sql->rowCount();

$pr=1;

if ($c > 0)  
{  

?>	
<form method="POST"  >
<?php
$xls_filename = 'apousiesprotis_'.date('Y-m-d').'.txt'; // Define Excel (.xls) file name
//create a file and send to browser for user to download
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0"); 
// Start of printing column names as names of MySQL fields
$header= "a/a".","."Mhtroo".","."Tmima".","."Mathitis".","."subject".","."onoma".","."Thlefono".","."Ekdilosi";
print "$header\n";
//...........................................................	 
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>";
	echo "<td>". "<input type='checkbox' name='tsms[]' value='$x'  checked>" ."</td>";
	echo "<td>". $onomaekd."</td>";
	$bname=" Αποστολη SMS ";
     
	
	echo "</tr>"; 
    $cc++;
}
echo "</table>";	

$_SESSION['anaf']=$anaf; 	
?>
<br>
<input type="submit" value="<?php echo $bname ; ?>" name="submit">
</form>
<?php



$i=1;
while($row = mysqli_fetch_array($sql)) 
{
	  $teach=$row['Name'];$pmpt=$row['teacher'];
	  $sign=$row['sign'];
      $sqls=mysqli_query($con,"SELECT posted FROM apousiologio WHERE date='$dt' AND teacher='$pmpt' ");
	  $rows= mysqli_fetch_array($sqls);
	  $q = mysqli_query($con,"SELECT id FROM dailytab WHERE Teacher='$teach'");
      $rowx = mysqli_fetch_array($q);
      $id1=$rowx["id"]+1;
	  	 $mathima=$row['mathima'];
	     $mitroo=$row['mitroo'];
	     $tmima=$row['tmima'];
	     $onoma=$row['eponimo']." ".$row['onoma'];
	     $per=$row['period'];
		 $mathc=$row['mathc'];
		 //................
		 $dtab="day".$per;
		 $q = mysqli_query($con,"SELECT ".$dtab." FROM dailytab WHERE id='$id1'");
         $rowx = mysqli_fetch_array($q);
		 $d1=$rowx["$dtab"];
         $arr=explode("/",$d1);
		 $subject=$arr[1];
		 if ($subject =="" )
		 {
			switch ($mathc) 
			{
               case 'Α':
			   case 'Σ':
			   $subject = 'ΕΠΙΒΛΕΨΗ';
               break;
               case 'Δ':$subject = 'Δ.Δ.Κ';
               break;
			}
         } 
	  $row = mysqli_fetch_array($sql);
	  $teach = $row['Name'];
	  
   $data = $i.",".$mitroo.",".$tmima.",".$mathima.",".$subject.",".$onoma.",".$teach.",".$per;
   $data = trim( $data ) . "\n";
   $data = str_replace( "\r" , "" , $data );
   print "$data";
  $i++; 
}
}
?>

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