<?php
include "scrinit.php";
include "logo.php";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function popupCenter(pageURL, title, w, h) 
{
	var left = (screen.width / 2)  - (w / 2);
    var top  = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no,scrollbars=yes, resizable=no, copyhistory=no, width=800, height=850, top='+top+', left='+left );
}

$(document).ready(function() {

  $('#f1').submit(function() {
     
    if ($(this).find('input[name="smsx[]"]:checked').length == 0) 
	{
      alert('Πρέπει να κάνετε τουλάχιστον ΜΙΑ ΕΠΙΛΟΓΗ!');
      return false;
    }
    if($("#keimeno").val().trim().length < 1)
    {
       alert("Δεν Έχετε γράψει Ανακοίνωση!");
       return false; 
    }
	

  });
		
});	

function submitform()
{
    document.forms["diagonismanew"].submit();
}

function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=no, width=1000, height=600, directories=no,location=no') 
}
</script>

<title>Ανακοινώσεις</title>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:10pt; }
<p style="font-size:10px;"></p>
</style>
</head>
<body>
<h2>ΚΑΤΑΧΩΡΙΣΗ ΑΝΑΚΟΙΝΩΣΗΣ</h2>
<?php
require "dbconnect.php"; 
$usr=$_SESSION['usr'];
$dt=$_SESSION['dt'];
$datein=$dt;
$dateout=$dt;
$sql = $dbcon->prepare("SELECT Name,access FROM teachers where PMP=? ");
$sql->execute([$usr]);
$row=$sql->fetch(PDO::FETCH_ASSOC);
$yp=$row['Name'];
$ac=$row['access'];
$words = explode(" ",$yp);
$acro = "";
foreach(explode(' ', $yp) as $word) $acro .= mb_substr($word, 0, 1, 'utf-8');
if ($ac==4)
{$acro=" (Δτής)";}
elseif ($ac==3)
{ $acro=" (".$acro." ΒΔΑ)";}
else if ($ac==2)
{ $acro=" (".$acro." ΒΔ)";}

$result = $dbcon->prepare("SELECT Name,PMP FROM teachers ORDER BY Name ");
$result->execute();
?>
<form name="f1" id="f1" method="POST" enctype="multipart/form-data" accept-charset="UTF-8">
Ημερομηνία Έναρξης : <input type="date" name="datein" value= <?php echo $datein; ?>  />
Ημερομηνία Λήξης : <input type="date" name="dateout" value=<?php echo $dateout; ?> /><br>

<h4> ΕΠΕΛΕΞΕ ΤΟΥΣ ΕΚΠΑΙΔΕΥΤΙΚΟΥΣ  </h4>
<form  method='post' ">
<div style="overflow:scroll; border:1px solid black;background:#F5F9F0;width:320px; height: 450px">
<?php
echo "<table style='width:300px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#adcd82'  >
<th>ΚΑΘΗΓΗΤΗΣ/ΚΑΘΗΓΗΤΡΙΑ</th>
<th> </th>
<th>ΕΠΙΛΟΓΗ</th>
</tr>";
$i=1;
echo "<tr>";
echo "<td>"." ΓΙΑ ΟΛΟΥΣ "."</td>";
	echo "<td>"." "."</td>";
    echo "<td ALIGN=center>"."<input type='checkbox' name='smsx[]' value='ALL' >"."</td>";
	echo "</tr>";
	$i++;
while ($row = $result->fetch(PDO::FETCH_ASSOC)) 
  { 
    $name=$row['Name'];
	$pmp = $row['PMP'];	
	echo "<tr>";
	echo "<td>".$name."</td>";
	echo "<td>"." "."</td>";
    echo "<td ALIGN=center>"."<input type='checkbox' name='smsx[]' value='$pmp' >"."</td>";
	echo "</tr>";
    $i++;
}
echo "</table>";
?>
</div>
<p>
<?php
echo "<table style='width:300px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#adcd82'  >
<th>Κείμενο Ανακοίνωσης</th>
<th>Αρχείο Ανακοίνωσης</th>
</tr>";
echo "<tr>";
echo "<td>"; 
echo "<textarea name='keimeno' id='keimeno' cols='60' rows='5'> $keimeno </textarea >"."<br >";
echo "</td>";
echo "<td>"; 
?>
<input type="file" name="upfile" />
<?php
echo "</td>"; 
echo "</tr>";
echo "</table>";
?>
<input type="hidden" name="acro" value=<?php echo $acro;?> >
</p>
<input type="submit" name="anakoinosi" value="Καταχώρηση Ανακοίνωσης" />
</form>
<?php
if (isset($_POST['anakoinosi']))
{
	$datein=$_POST['datein'];
	$dateout=$_POST['dateout'];
	$keimeno=$_POST['keimeno']." ".$_POST['acro'];
	
	$teach = $_POST['smsx'];
	$file_name = $_FILES['upfile']['name'];
	echo $file_name;
    $file_size =$_FILES['upfile']['size'];
    $file_tmp =$_FILES['upfile']['tmp_name'];
    $file_type=$_FILES['upfile']['type'];
    $ar=explode('.',$file_name);
	$file_ext=$ar[1];
	$errors= array();
	$extensions= array("pdf","docx","txt");
	if(in_array($file_ext,$extensions)== false)
	{
         $errors[]="Μόνο αρχεία κειμένου επιτρέπονται.";
    }
	if(empty($errors))
	  {
         move_uploaded_file($file_tmp,"files/".$file_name);
		 
	  }
    if(!empty($_FILES['upfile']['name'][0]))
	{
        echo "<h1>" . "File ". $_FILES['upfile']['name'] ." uploaded successfully." . "</h1>";
	}
	else
	{
	     echo "Λάθος";	
	}
 
	if (count($teach)== 1 AND $teach[0]=='ALL') 
	{
		$t=$teach[0];
	    $q1 = $dbcon->prepare("INSERT INTO anakoinosi (datein,dateout,keimeno,teacher,parents,arxeio) 
	                 VALUES (?,?,?,?,?,?)");
        $q1->execute([$datein,$dateout,$keimeno,0,0,$file_name]);					 
	}
	else
	{
       foreach ($teach as $t)
	   {
          $q1 = $dbcon->prepare("INSERT INTO anakoinosi (datein,dateout,keimeno,teacher,parents,arxeio) 
	                 VALUES (?,?,?,?,?,?)");
		  $q1->execute([$datein,$dateout,$keimeno,$t,0,$file_name]);			
	   }
    }
	
    echo 'alert()';
	?>
      <script type="text/javascript">
      alert("Η ανακοίνωση καταχωρήθηκε με επιτυχία");
      window.location.href = '<?php echo $menu; ?>'
      </script>
    <?php
		

}

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