<?php
include "scrinit.php";
$fn=$_GET['fn'];
if ($fn == 1 ) 
{ 
  include $menu ; 
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style1.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
$usr=$_SESSION['usr'];
$gym=$_SESSION['gym'];

$sql = $dbcon->prepare("SELECT perapo FROM offperiods WHERE datefrom = ?");
$sql->execute([$dt]);
$row=$sql->fetch(PDO::FETCH_ASSOC);
$count=$sql->rowCount();
$gym1=$gym+1;
if ($count > 0 and $row['perapo']==$gym1)
{
	$gymx=$gym;
	$_SESSION['gymx']=$gymx;
	$gym=$gym1;
	$_SESSION['gym']=$gym;
}
else
{
    $sql = $dbcon->prepare("SELECT gymnasio FROM parameters" ); 
	$sql->execute();
	$row=$sql->fetch(PDO::FETCH_ASSOC);
	switch ($row['gymnasio']) {
	  case "1":    $gym= 8;    break;
	  case "2":    $gym= 7;    break;
	  case "3":    $gym= 9;    break;
	}
	$_SESSION['gym']=$gym;
	$_SESSION['gymx']=$gym;
}

$access=$_SESSION['access'];
  
$day = date('D', strtotime($dt));
if ($day=="Sat" or $day=="Sun")
{
    echo "<br>"."ΔΕΝ ΥΠΑΡΧΟΥΝ ΜΑΘΗΜΑΤΑ ΣΗΜΕΡΑ"."<br>";
}
else 
{ 

include "allagiprog.php";
//................................

$q = $dbcon->prepare("SELECT * FROM dailytab WHERE pmp =?");
$q->execute([$usr]);
$rowx=$q->fetch(PDO::FETCH_ASSOC);
$id=$rowx['id'];
$teacher=$rowx['Teacher'];
$i=1;
while ($i<=$gym1) 
{
      $in='day'.$i;
      $lesson[$i]= $rowx["$in"];
      $i++;
}   
$id=$id+1;
$q = $dbcon->prepare("SELECT * FROM dailytab WHERE id =?");
$q->execute([$id]);
$rowx=$q->fetch(PDO::FETCH_ASSOC);
$i=1;
$rm=array();
while ($i<=$gym1) 
{
      $in='day'.$i;
      $d1=$rowx["$in"];
	  $ar=explode("/",$d1);
      $subject[$i]= $ar[1];
	  $rm[$i]=$ar[0];
      $i++;
}

///////////////////////////////////////////////////////
if ($lesson[$gym1]<> "") {$ddk=1;}

if ($anapl==1)
{
  echo "ΑΝΑΠΛΗΡΩΣΗ ΓΙΑ : ".$teachapon;
  $q1 = $dbcon->prepare("SELECT * FROM dailytab WHERE pmp =?");
  $q1->execute([$pmpaponx]);
  $rowq1=$q1->fetch(PDO::FETCH_ASSOC);
  $aponid=$rowq1['id'];
  $in='day'.$ora;
  $lesn=$rowq1["$in"];
  $lesson[$ora]= $lesn;
  $q1 = $dbcon->prepare("SELECT * FROM dailytab WHERE pmp =?");
  $q1->execute([$usr ]);
  $rowq1=$q1->fetch(PDO::FETCH_ASSOC);
  $tid=$rowq1['id'];
  
  if ($subject[$ora]<> 'ΑΝΑΠΛΗΡΩΣΗ')
  {	
	$subject[$ora]='ΑΝΑΠΛΗΡΩΣΗ';
	$q1=$dbcon->prepare("INSERT INTO anaplirosi (teacher,tid,date,period,pmpapon,aponid,mathima) VALUES (?,?,?,?,?,?,?)");
	$q1->execute([$usr,$tid,$dt,$ora,$pmpaponx,$aponid,$lesn]);
  }
  else
  {
	$q1=$dbcon->prepare("update anaplirosi set period=?,pmpapon=?,aponid=?,mathima=? where date='$dt' and teacher='$usr' ");
	$q1->execute([$ora,$pmpaponx,$aponid,$lesn]);  
  }
  $q2=$dbcon->prepare("update dailytab SET ".$in."='$lesn' WHERE id=?"); 
  $q2->execute([$tid]);
  $tid1=$tid+1;
  if ($pmpaponx <> 0)
  {
  $q3=$dbcon->prepare("update dailytab SET ".$in."='ΑΝΑΠΛΗΡΩΣΗ' WHERE id=?");
  $q3->execute([$tid1]);
  }
  else
  {
	  $q3=$dbcon->prepare("update dailytab SET ".$in."=' ' WHERE id=?");
	  $q3->execute([$tid1]);
	  $res1 = $dbcon->prepare("delete FROM anaplirosi where pmpapon=0 and teacher=? ");
	  $res1->execute([$usr]);
  }
}  
//................................  
if ($efim==1) 
{
	$room=" ";
	if ($ora >= $gym)
	  { $subject[$ora]='ΣΧΟΛΑΝΕΙ';}
    else
	{$subject[$ora]='ΕΠΙΒΛΕΨΗ';}
    
    $lesson[$ora]= $lesn;
	$in='day'.$ora;
	$insub=$room."/".$subject[$ora];
    $q1 = $dbcon->prepare("SELECT * FROM dailytab WHERE pmp =?");
    $q1->execute([$usr]);
    $rowq1=$q1->fetch(PDO::FETCH_ASSOC);
    $tid=$rowq1['id'];
	$in = "`".str_replace("`","``",$in)."`";
    $q2=$dbcon->prepare("update dailytab  SET {$in} = ? WHERE id = ? "); 
    $q2->execute([$lesn,$tid]);
    $tid1=$tid+1;
    $q3=$dbcon->prepare("update dailytab SET {$in}=? WHERE id=?");
    $q3->execute([$subject[$ora],$tid1]);
	$sx1=$dbcon->prepare("select * from anaplirosi where date=? and period=? and teacher=?");
	$sx1->execute([$dt,$ora,$usr]);
	if ($sx1->rowCount()==0)
	{
	  $q1=$dbcon->prepare("INSERT INTO anaplirosi (teacher,tid,date,period,mathima) VALUES (?,?,?,?,?)");
	  $q1->execute([$usr,$tid,$dt,$ora,$lesn]);
	}
	else
	{
	  if ($lesn <> "")
	  {
	    $q1=$dbcon->prepare("update anaplirosi set period=?,mathima=? where  date=? and teacher=?");
        $q1->execute([$ora,$lesn,$dt,$usr]);
	  }
	  else
	  {
		$q1=$dbcon->prepare("delete from anaplirosi where period=? and date=? and teacher=?");
		$q1->execute([$ora,$dt,$usr]);  
	  }
    
    }
} 
//.................................. 
$sqddk=$dbcon->prepare("select * from apouddk where date=? and teacher=?");
$sqddk->execute([$dt,$usr]);
if ($sqddk->rowCount()<>0) 
{
	$rowddk=$sqddk->fetch(PDO::FETCH_ASSOC);	
	$ora=$rowddk['period'];
	$lesn=$rowddk['tmima'];
	$ddk=1;
}	
if ($ddk==1) 
{
	$room=" ";
	$subject[$ora]='Δ.Δ.Κ';
    $lesson[$ora]= $lesn;
	$in='day'.$ora;
	$insub=$room."/".$subject[$ora];
    $q1 = $dbcon->prepare("SELECT * FROM dailytab WHERE pmp =?");
    $q1->execute([$usr ]);
    $rowq1=$q1->fetch(PDO::FETCH_ASSOC);
    $tid=$rowq1['id'];
    $q2=$dbcon->prepare("update dailytab SET {$in}=? WHERE id=?"); 
    $q2->execute([$lesn,$tid]);
    $tid1=$tid+1;
	$q3=$dbcon->prepare("update dailytab SET {$in}=? WHERE id=?");
    $q3->execute([$insub,$tid1]);
    $sqddk=$dbcon->prepare("select * from apouddk where date=? and period=? and teacher=?");
	$sqddk->execute([$dt,$ora,$usr]);
	if ($sqddk->rowCount()==0)
	{
	  
	  $q1=$dbcon->prepare("INSERT INTO apouddk (teacher,tid,date,period,tmima) VALUES (?,?,?,?,?)");
      $q1->execute([$usr,$tid,$dt,$ora,$lesson[$ora]]);
    }
	else
	{
	  
	  if ($lesn <> "")
	  {
	  $q1=$dbcon->prepare("update apouddk set period=?,tmima=? where  date=? and teacher=?");
      $q1->execute([$ora,$lesn,$dt,$usr]);
	  }
	  else
	  {
		$q1=$dbcon->prepare("delete from apouddk where period=? and date=? and teacher=?");
		$q1->execute([$ora,$dt,$usr]);  
	  }
	}
} 
//..................................

if ($access <> 4  )
{
echo "<p class='p1'>"."ΠΡΟΓΡΑΜΜΑ ΗΜΕΡΑΣ"."</p>";
echo "ΕΚΠΑΙΔΕΥΤΙΚΟΣ: ".$teacher."<BR>";
if ($ddk==1){$d=$gym+1;} else {$d=$gym;}
echo "<table  class='mytable3' >" ;
for ($i=1;$i<=$d;$i++)
{
	$titep="Περίοδος ".$i;
	echo "<th>".$titep."</th>";
}
echo "</tr>";

 $i=1;
 echo "<tr>";

 while($i<=$d)
     {
	  if(!empty($lesson[$i])){$disp=$lesson[$i]." [".$rm[$i]."] ";}else {$disp="";}
	  $pas=$lesson[$i]."/".$subject[$i]."/".$i."/".""."/".""."/".$rm[$i]; 
	  echo "<td>" . "<a href=\"newmath2.php?pas=".$pas."\">".$disp. "</a>". "</td>";	
	  //}
	 $i++;
	 }
 echo "</tr>";
 $i=1;
 echo "<tr>";
 while($i<=$d)
     {
	  echo "<td>" . $subject[$i]."</td>";	
	  
	 $i++;
	 }
 echo "</tr>";  
echo "</table>";
echo "<br>";
}
else
{
	include "allageshmeras.php";
}

}
include "anakoinoseis.php";
include "vdegkiklios.php";
include "ergasies.php";
include "arxicheckkat.php";
include "apouyt.php";
?>

</body>
</html>