<?php
include "scrinit.php";
include "logo.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script>
    function refreshParent() {
        window.opener.location.reload();
		self.close();
    }
   </script>

</head>

<body>
<?php 
require("dbconnect.php");
$dt = $_SESSION['dt'];
$gym = $_SESSION['gym'];
$day = date('D', strtotime($dt));
$teachapon=0;
if (isset($_POST['Submitapou'])) 
{
   $usr=$_POST['usr1']; 
   $subject=$_POST['subject'];
   $subjectx=$_POST['subjectx'];
   $less = $_POST['lesson'];
   $per=$_POST['per'];
   $newdr=$_POST['newdr1'];
   $pop = $_POST['pop'];
   $wc1=$_POST['wc'];
   $q=$dbcon->prepare("select * from anaplirosi where teacher=? and date=? and period=? ");
   $q->execute([$usr,$dt,$per]);
   $rowq=$q->fetch(PDO::FETCH_ASSOC);
   switch ($subject)
   {
	case "ΑΝΑΠΛΗΡΩΣΗ":
	   $mathc="Α";
	   $aponid=$rowq['aponid'];
	   $sqlid = $dbcon->prepare("SELECT pmp FROM timetable WHERE id = ? ");
	   $sqlid->execute([$aponid]);
	   $rowid=$sqlid->fetch(PDO::FETCH_ASSOC);
	   $teachapon=$rowid['pmp'];
	   break;
	case "ΣΧΟΛΑΝΕΙ":
	   $mathc="Σ";
	   $teachapon=0;
	   break;
	case "Δ.Δ.Κ":
		$mathc="Δ";
		$teachapon=0;
		break;	
	default:
		$teachapon=0;
        $mathc="";
		break;
	}
	
   $per=$_POST['per'];
   $tetramino=$_SESSION['tetramino'];
   $perigrafi=$_POST['perigrafi'];
   $c=0;$r=0;
   $mti = $_POST['mt'];
   $apousia = $_POST['att'];
   $stoli=$_POST['stoli'];
   $kathi =$_POST['kath'];
//*************   
   if ($subjectx=="Δ.Δ.Κ")
   {
	   $mathc="Δ";
	   $in='day'.$per;
	   
	   $q1 = $dbcon->prepare("SELECT Name FROM teachers WHERE PMP = ? ");
	   $q1->execute([$usr]);
	   $row=$q1->fetch(PDO::FETCH_ASSOC);
	   $teacher=$row['Name'];
       $q1 = $dbcon->prepare("SELECT ".$in.",id FROM dailytab WHERE Teacher=? ");
       $q1->execute([$teacher]);
	   $rowq1=$q1->fetch(PDO::FETCH_ASSOC);
	   $tid=$rowq1['id'];
	   $lesn=$rowq1["$in"];
	   $ar=explode("/",$lesn);
	   $insub=$ar[0]."/"."Δ.Δ.Κ";
       $tid1=$tid+1;
	   $q1=$dbcon->prepare("UPDATE dailytab SET ".$in."=? WHERE id=? ");
       $q1->execute([$insub,$tid1]);
	   
	   $sqddk=$dbcon->prepare("select * from apouddk where date=? and period=? and teacher=? ");
	   $sqddk->execute([$dt,$per,$usr]);
       if ($sqddk->rowCount()==0)
	   {
		  $qi=$dbcon->prepare("INSERT INTO apouddk (id,teacher,tid,date,period,tmima) VALUES (NULL,?,?,?,?,?)");
          $qi->execute([$usr,$tid,$dt,$per,$less]);
	   }
	   else
	   {
	      $qi=$dbcon->prepare("update apouddk set period=?,tmima=? where  date=? and teacher=?");
		  $qi->execute([$per,$less,$dt,$usr]);
       }
   }
 //***************  
	if (!empty($apousia))
	{
	  $c = count($apousia);
	}
	$_SESSION['c']=$c;
		
 $sql=$dbcon->prepare("SELECT mitroo FROM apousies WHERE dateout=? AND teacher=? AND mathima=? AND period=? " );
 $sql->execute([$dt,$usr,$less,$per]);
 $c2=$sql->rowCount();
 if ($c2 > 0) 
 {
    $sql=$dbcon->prepare("DELETE FROM apousies WHERE dateout=? AND teacher=? AND mathima=? AND period=?");
    $sql->execute([$dt,$usr,$less,$per]);
	$sql=$dbcon->prepare("DELETE FROM syllabi WHERE  date=? AND pmp=? AND mathima=? AND per=? " ) ;
	$sql->execute([$dt,$usr,$less,$per]);
 }
 $apc=0;
 $sq1=$dbcon->prepare("INSERT INTO apousies (mitroo,apousia,dateout,period,teacher,teachapon,mathima,mathc,tetramino) 
		      VALUES (?,?,?,?,?,?,?,?,?) ");
 $x=1;
 
 for ($i=0; $i<$c; $i++) 
 {
	 $sq1->execute([$apousia[$i],$x,$dt,$per,$usr,$teachapon,$less,$mathc,$tetramino]);
	 $apc++;
 }
 $c1 = count($kathi);
 $r = 0;
 $sq1=$dbcon->prepare("INSERT INTO apousies (mitroo,apousia,kath,dateout,period,teacher,teachapon,mathima,mathc,tetramino) 
		      VALUES (?,?,?,?,?,?,?,?,?,?) ");
			  $x=0;
 for ($i=0; $i<$c1; $i++) 
 {    
	if ($kathi[$i] > 0 )
	  {
	    $sq1->execute([$mti[$i],$x,$kathi[$i],$dt,$per,$usr,$teachapon,$less,$mathc,$tetramino]);
		$r++;
	  }
 }

if (!empty($stoli))
	{
	$cs = count($stoli);
	}
	$s = 0;$x=1;
	$sql=$dbcon->prepare("INSERT INTO apousies (mitroo, dateout,period,teacher,teachapon,mathima,mathc,stoli,tetramino) 
	             VALUES (?,?,?,?,?,?,?,?,?) ");  
	for ($i=0; $i<$cs; $i++) 
		{
	      $sql->execute([$stoli[$i],$dt,$per,$usr,$teachapon,$less,$mathc,$x,$tetramino]);
		  $s++;
		}
/////////////////////////////

if (!empty($wc1))
	{
	$cs=count($wc1);
	$sql=$dbcon->prepare("DELETE FROM wc WHERE date=? AND teacher=? AND lesson=? AND period=?");
    $sql->execute([$dt,$usr,$less,$per]);
	}
	
$sql=$dbcon->prepare("INSERT INTO wc (mitroo,date,period,teacher,lesson) VALUES (?,?,?,?,?) ");  		
for ($i=0; $i<$cs; $i++) 
		{
	      $sql->execute([$wc1[$i],$dt,$per,$usr,$less]);
		}
		
}

//************************************* gia rapid tests
$j=0;
if (!empty($newdr))
{
foreach ($newdr as $newd)
{
	$mitrap=$mitr[$j];
	if (!empty($newd))
	{
	$q="update rapidimg set testdate='$newd' where mitroo='$mitrap'";
	if (!mysqli_query($con, $q)) 
		{
            echo "Error: " . $q . "<br>" . mysqli_error($con);
		}
		$j++;
}}
}

//***********************
if ($apc == 0) 
{                   //OLOI PARONTES
    $ap=1;$mit=0;
	$sql=$dbcon->prepare("INSERT INTO apousies (mitroo, dateout, period, teacher, mathima,mathc,apousia,tetramino) 
       VALUES (?,?,?,?,?,?,?,?) ");
    $sql->execute([$mit,$dt,$per,$usr,$less,$mathc,$ap,$tetramino]);
}

$sql=$dbcon->prepare("INSERT INTO syllabi (pmp,per,date,perigrafi,mathima,lesson) VALUES (?,?,?,?,?,?)"); 
$sql->execute([$usr,$per,$dt,$perigrafi,$less,$subject]);
//.........................................................
$all=$c+$r+$s; 
echo "Προστέθηκαν / Ενημερώθηκαν ".$all."  εγγραφές"."<BR>";
//.............SEND SMS GIA ARGOPORIA......................
$t=time();
$dy = date('D', strtotime($dt));
if ( ($t >= strtotime('07:30') and $t < strtotime('13:30')) and (($dy <>"Sat" or $dy <> "Sun")) ) 
{
	echo "true";
$sql=$dbcon->prepare("select smskath from parameters");
$sql->execute();
$row= $sql->fetch(PDO::FETCH_ASSOC);
if($row['smskath']==1)	
{	
 for ($i=0; $i<$c1; $i++) 
 {    
	if ($kathi[$i] > 0 )
	  {
		$sqs=$dbcon->prepare("select mitroo from smsdb where mitroo=? and date=? and period=? ");
		$sqs->execute([$mti[$i],$dt,$per]);
		if ($sqs->rowCount()==0)
		 {
			 
		 $sqs=$dbcon->prepare("insert into smsdb (mitroo,date,period) values (?,?,?)");
		 $sqs->execute([$mti[$i],$dt,$per]);
		 $sqt=$dbcon->prepare("select kinito,onoma from allstudentinfo where mitroo=?");
		 $sqt->execute([$mti[$i]]);
		 $rowt=$sqt->fetch(PDO::FETCH_ASSOC);
		 $tele=array($rowt['kinito']);
		 $name=$rowt['onoma'];
		 $mess="ΤΟ ΠΑΙΔΙ ΣΑΣ ".$name." ΚΑΘΥΣΤΕΡΗΣΕ ".$kathi[$i]." ΛΕΠΤΑ ΤΗΝ ".$per." περ. ΠΑΡΑΚΑΛΟΥΜΕ ΓΙΑ ΤΗ ΣΥΝΕΡΓΑΣΙΑ ΣΑΣ";
		 $_SESSION['tel']=$tele;
	     $_SESSION['sms1']=$mess;
		 include "sendsmskath.php";
		 }
		//............................
	  }
 }
}
}

/////////////////

require "apousiologio1.php";
/////////////////
echo "<br>"."<br>";
if ($pop <> "1")
{
	?>
   <a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού   </a>
   <?php
}
else
{
   ?>	
   <input type="button" name="epistrofi" value="Επιστροφή" onClick="refreshParent()"> 
   <?php
}
?>

</body>
</html>