<?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=900, 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=900, 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"; 

$dt=$_SESSION['dt'];
$cdx=$_GET['data'];
$ar=explode("/",$cdx);
$anak=array();
$cds=$ar[0]; 
$cde=$ar[1];
if (count($anak) > 1)
{
  $sql = mysqli_query($con,"SELECT t.* from (select * from anakoinosi where id >= '$cds') as t where t.id <='$cde'  ");
}
else
{
  $sql = mysqli_query($con,"SELECT * from anakoinosi where id = '$cds' ");	
}
$i=1;
while($row = mysqli_fetch_array($sql))
{
	$datein=$row['datein'];
	$dateout=$row['dateout'];
	$keimeno=$row['keimeno'];
	$anakt[$i]=$row['teacher'];
	$fname=$row['arxeio'];
	$i++;
}
$pmpa=serialize($anakt);
$sql = "SELECT Name,PMP FROM teachers ORDER BY Name ";
$result = mysqli_query($con,$sql);


?>
<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>";
if ($anak[1]==0)
{
   $checked = "checked";
}
echo "<td ALIGN=center>"."<input type='checkbox' name='smsx[]' value='ALL' {$checked} >"."</td>";
echo "</tr>";
$i++;
while ($row = mysqli_fetch_array($result)) 
  { 
    $name=$row['Name'];
	$pmp = $row['PMP'];	
	if(in_array($pmp, $anakt) )
	{
	   $checked = "checked";
	}
	else
	{
		$checked = "";
    }	
	echo "<tr>";
	echo "<td>".$name."</td>";
	echo "<td>"." "."</td>";
    echo "<td ALIGN=center>"."<input type='checkbox' name='smsx[]' value='$pmp' {$checked}>"."</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='keimeno1' id='keimeno' cols='60' rows='5'> $keimeno </textarea >"."<br >";
echo "</td>";
echo "<td>"; 
?>
<input type="file" name="upfile" value="<?php echo $fname;?>" />
<?php
echo "</td>"; 
echo "</tr>";
echo "</table>";
?>
</p>
<input type="hidden" name="filename" value=<?php echo $fname; ?> />
<input type="hidden" name="cds" value=<?php echo $cds; ?> />
<input type="hidden" name="cde" value=<?php echo $cde; ?> />
<input type="submit" name="anakoinosi" value="Καταχώρηση Ανακοίνωσης" />
</form>
<?php
if (isset($_POST['anakoinosi']))
{
	$datein=$_POST['datein'];
	$dateout=$_POST['dateout'];
	$keimeno=$_POST['keimeno1'];
	$teach = $_POST['smsx'];
	$cds=$_POST['cds'];
	$cde=$_POST['cde'];
		$file_name = trim($_FILES['upfile']['name']);
		$file_size =$_FILES['upfile']['size'];
		$file_tmp =$_FILES['upfile']['tmp_name'];
		$file_type=$_FILES['upfile']['type'];
		$arr=explode('.',$file_name);
		$file_ext=trim($arr[1]);echo $file_ext;
		$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 (is_uploaded_file($_FILES['upfile']['tmp_name'])) 
		{
			echo "<h1>" . "File ". $_FILES['upfile']['name'] ." uploaded successfully." . "</h1>";
		}
		else
		{
			echo "Λάθος";	
		}
	
		if ($file_name==""){$file_name=$_POST['filename'];}
			
	
	if (count($teach)== 1 AND $teach[0]=='ALL') 
	{
		$t=$teach[0];
	    mysqli_query($con,"update anakoinosi set datein='$datein',dateout='$dateout',keimeno='$keimeno',arxeio='$file_name' where id='$cds' ");
	                 	
	}
	else
	{
       for ($i=$cds;$i<=$cde;$i++)
	   {		   
          mysqli_query($con,"delete from anakoinosi where id='$i' ");
	   }
	   
	   foreach ($teach as $t)
	   {
          mysqli_query($con,"INSERT INTO anakoinosi (id,datein,dateout,keimeno,teacher,parents,arxeio) 
	                VALUES (NULL,'$datein','$dateout','$keimeno','$t',0,'$file_name')");
	   }
    
    }
	echo '<script type="text/javascript">';
    echo 'alert("Η ανακοίνωση καταχωρήθηκε με επιτυχία")';
	echo '</script>';	

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