<?php
include "scrinit.php";
include "logo.php";
require "dbconnect.php";
mysqli_set_charset($con, "utf8");
?>
<head>
<link href="dpm.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function pop_up(url){
		window.open(url,'win2','toolbar=yes, scrollbars=yes, resizable=yes, top=100, left=200, width=600, height=800') 
	}
</script>
</head>
<?php
// DEFAULT MONTH/YEAR = TODAY
$dt=$_SESSION['dt'];
$usr=$_SESSION['usr'];
$gym=$_SESSION['gym'];
$unix = strtotime($dt);
$monthNow = date("M", $unix);
$yearNow = date("Y", $unix); 
$cd=$_GET['mathid'];

if (!empty($cd))
{
    $cc=explode("/",$_GET['mathid']);
	$tm1=$cc[0]."/".$cc[1];
}	

// MONTHS
$months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
$mm=['Ιανουάριος','Φεβρουάριος','Μάρτιος','Απρίλιος','Μάιος','Ιούνιος','Ιούλιος','Αύγουστος','Σεπτέμβριος','Οκτώβριος','Νοέμβριος','Δεκέμβριος'];
?>
<h3> ΗΜΕΡΟΛΟΓΙΟ ΔΙΑΓΩΝΙΣΜΑΤΩΝ</h3>
<form  method='post' >
<label><b>Μήνας: </b></label>
<select name="month" onchange="this.form.submit()">
<?php
$i=0;
foreach ($months as $m) 
{
  ?><option value=<?php echo $m;?> <?php if($m==$monthNow) echo 'selected';?>  /><?php echo $mm[$i]; ?></option><?php
  $i++;
}
?>
</select>
<input type="submit" name="submitm" value="Επιλογή" >
</form>
<?php

if (isset($_POST['month']))
{
	$key = array_search($_POST['month'],$months);
	echo $mm[$key];
	$stm = sprintf("01 %s %s", $_POST['month'], 2021);
	$t=date('Y-m-d', strtotime($stm));

    $unix = strtotime($stm);
    $dm = date("t", $unix);
    $em = sprintf("%s %s %s", $dm, $_POST['month'], 2021);

    // First and last day of month
    $fd = date("N", strtotime($stm));
    $ld = date("N", strtotime($em));

    // YYYY-MM date format for later use
    $ym = date("Y-m-", $unix);	

	// DRAWING CALCULATION
    // Determine the number of blank squares before start of month
    $sq = $dq =[];
    if ($fd != 7) {
      for ($i=0; $i<$fd; $i++) {
        $sq[] = "b";
		$dq[] = "b";
      }
    }
    // Populate the days of the month
	$xt=strtotime($stm);

    for ($i=1; $i<=$dm; $i++) 
	{
      $sq[] = $i;
	  $dq[]= date('Y-m-d',$xt );
	  $xt=strtotime("+1 day", $xt);
    }

    // Determine the number of blank squares after end of month
    if ($ld != 6) {
      $blanks = $ld==7 ? 6 : 6-$ld;
      for ($i=1; $i<$blanks; $i++) {
        $sq[] = "b";
      }
    }

    // DRAW ?>
    <table id="calendar">
      <tr class="day">
        <td>Δευτέρα</td> <td>Τρίτη</td> <td>Τετάρτη</td>
        <td>Πέμπτη</td> <td>Παρασκευή</td>
      </tr>
      <tr><?php
        $total = count($sq);
        for ($i=0; $i<$total; $i++) 
		{
          $thisDay = $sq[$i];
		  $dat=$dq[$i];
		  $dave =	date("D", strtotime($dat));
		  		  
          if ($thisDay=="b") 
		  {
			if ($dave<>'Sat' and  $dave<>'Sun'){ echo "<td class='blank'></td>";}
          } 
		  else 
		  {
			if ($dave<>'Sat' and  $dave<>'Sun')
			{
				$sql1 = mysqli_query($con,"SELECT * FROM declarations WHERE testdate ='$dat' order by testdate") ;
				echo "<td>";
				echo '<table class="b">';
				echo "<tr><td colspan='2' style='border: none;width=120px;background-color: #ba3423;'>";
				echo "<b>".date('d-m-Y', strtotime($dat))."</b>";
				echo "</td></tr><tr>";
				while ($row = mysqli_fetch_array($sql1)) 
				{
					//echo "<td style='border:none;width=80px;'>".$row['class'].":"."</td>"."<td style='border: none;width=80px;'>".$row['subject']."</td></tr>";
					echo "<td style='border:none;width=120px;'>"."<a style='text-decoration:none' href=\"psyndtel.php?syndx=".$row['class']."\" onclick=\"pop_up(this.href);return false\">".$row['class'].'/'.$row['subject']."</a>"."</td></tr>";
				}
				//************** ekdilosi
				
				$q2 = mysqli_query($con,"SELECT perapo,permexri,date,code,onoma FROM ekdilosi WHERE date = '$dtd' ");
				$c2=mysqli_num_rows($q2);
				$row2=mysqli_fetch_array($q2);
				if ($c2 > 0 and $row2['permexri'] > 0)
				{
				  echo "<td>"."---------------"."</td>";
				  while(!empty($row2))
				  {
					$descr=$row2['onoma'];
					$code=$row2['code'];
					echo "<td style='border:none;width=80px;'>"."<a style='text-decoration:none' href=\"psyndtel.php?syndx=".$descr."\" onclick=\"pop_up(this.href);return false\">".$descr."</a>"."</td></tr>";
					$row2=mysqli_fetch_array($q2);
				  }
				}
				//***********************
				echo "</table>";
			
		    }
				
		  }
            if ($i!=$total && $i%7==0) { echo "</tr><tr>"; }
          
		}
      ?></tr>
    </table>
	<p></p>
    <?php 
	
	include "selectsubj1.php";
	$sub=array_unique($mathsubj);
	$_SESSION['sub']=$sub;
	$fromp=1;
	?>

	<form method='post' action='diagdilosi.php' target="_blank"> 
    ΗΜΕΡΟΜΗΝΙΑ ΔΙΑΓΩΝΙΣΜΑΤΟΣ: <input type="date"  name ='dtd' value= <?php echo $dt; ?> > 
    &nbsp;&nbsp;&nbsp
	
	<label><b>ΤΜΗΜΑ/ΜΑΘΗΜΑ : </b></label><select name='mathsubid' >
     
      <?php
	echo "<option value='$tm1'> $tm1</option>";
    foreach ($mathsubj as $m){ 		  
          echo "<option value='$m'>$m</option>";
      }
      ?>
     </select>
	 <input type="hidden" name="pmp1" value=<?php echo $pmp;?> >
	 <input type="hidden" name="fromprog" value=<?php echo $fromp;?> >
	 <input type="submit" name="submitb" value="Επιβεβαίωση Επιλογής" >

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