<?php
session_start();
ob_start();
if ($_SESSION['islogin']=='' ) { 
  echo "ΠΡΕΠΕΙ ΝΑ ΕΓΓΡΑΦΕΙΤΕ ΓΙΑ ΝΑ ΧΡΗΣΙΜΟΠΟΙΗΣΕΤΕ ΤΟ ΣΥΣΤΗΜΑ"."<BR>";
  sleep(3);
  header("Location: loginall.php"); /* Redirect browser */
  exit();
  }
$menu = $_SESSION['menu'];
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'];

if(isset($_POST['submit1']))
{
  $dt=$_POST['dt'];
  $_SESSION['dt']=$dt;
}
echo "<b>"."<p style='font-size:16px;'>"."ΑΝΑΦΟΡA ΗΜΕΡIΣΙΩΝ ΑΠΟΥΣΙΩΝ: ".date('d-m-Y', strtotime($dt))."</p>"."</b>";

$day = date('D', strtotime($dt));
$_SESSION['day']=$day;

if ($day=="Sat" or $day=="Sun"){
    echo "<br>"."ΔΕΝ ΥΠΑΡΧΟΥΝ ΜΑΘΗΜΑΤΑ ΣΗΜΕΡΑ"."<br>";
	}
else 
{

 	
$sql = mysqli_query($con,"SELECT DISTINCT ap.teacher,t.Name FROM apousies as ap,teachers as t 
 WHERE ap.teacher=t.PMP AND ap.dateout='$dt' AND (ap.apousia=1 or ap.kath >=15) ");	
 $t=mysqli_num_rows($sql);
 
 $tnames = array();
 $i=1;
 while ($row = mysqli_fetch_array($sql)) {
    $tnames[$i] = rtrim(ltrim($row['Name']));
	$i++;
 }
/////////////////////////////
include "allagiprog.php";
///////////////////////////
$sql= mysqli_query($con,"SELECT * FROM dailytab  " );
$c=mysqli_num_rows($sql); 

$i=1;
$teach=array();
while($row = mysqli_fetch_array($sql))
{
	$teacher=$row['Teacher'];
	$flg=0;
	for ($j=1;$j<=$gym;$j++) {
	    $d=$day.$j;
		$dtab="day".$j;
	    if (!empty($row["$dtab"])) $flg=1; }
	
	if ($flg == 1) {
		$teach[$i]=rtrim(ltrim($teacher));
		$i++;
	}
	$row = mysqli_fetch_array($sql);
}
$limit=$i-1;

////////////////////////////////////////////////////////////////////////////////////////////////////////	
$sql = mysqli_query($con,"SELECT ap.dateout, ap.mitroo, ap.period, ap.teacher,
 ap.mathima,ap.mathc, a.onoma, a.eponimo, a.tmima, t.Name, t.sign 
 FROM apousies as ap,allstudentinfo as a, teachers as t WHERE (ap.apousia=1 or ap.kath >=15) AND ap.dateout='$dt'
 AND ap.mitroo=a.mitroo AND ap.teacher=t.PMP  
 ORDER BY ap.dateout,ap.teacher,ap.period " );

$xls_filename = 'apousies_'.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".","."mitroo".","."tmima".","."mathima".","."subject".","."onoma".","."kathigitis".","."periodos";
print "$header\n";

$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>