<?php
session_start();
require_once(__DIR__ . "/../inc/config.inc.php");
require_once(__DIR__ . "/../inc/functions.inc.php");

//Überprüfe, dass der User eingeloggt ist
//Der Aufruf von check_user() muss in alle internen Seiten eingebaut sein
$user = check_admin_user();

include("templates/header.inc.php");

echo '<div class="container main-container">';

function build_calendar($month, $year) {
	$daysOfWeek = array('M','T','W','T','F','S','S');
	$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
	$numberDays = date('t',$firstDayOfMonth);
	$dateComponents = getdate($firstDayOfMonth);
	$monthName = $dateComponents['month'];
	$dayOfWeek = $dateComponents['wday'] -1;
	$calendar = "<table class='calendar table table-condensed table-bordered' >";
	$calendar .= "<caption><h3>$monthName $year</h3></caption>";
	$calendar .= "<tr>";
	foreach($daysOfWeek as $day) {
		$calendar .= "<th class='header'>$day</th>";
	}
	$currentDay = 1;
	$calendar .= "</tr><tr>";
	if ($dayOfWeek > 0) {
		$calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
	}
	$month = str_pad($month, 2, "0", STR_PAD_LEFT);
	while($currentDay <= $numberDays){
		if($dayOfWeek == 7){
			$dayOfWeek = 0;
			$calendar .= "</tr><tr>";
		}
		$currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
		$date = "$year-$month-$currentDayRel";
		
		// Is this today?
		if(date('Y-m-d') == $date) {
			$calendar .= "<td class='day success' rel='$date' onClick='submitForm('".$date ."')'><b>$currentDay</b>";

		} else {
			$calendar .= "<td class='day' rel='$date'  onClick='submitForm(\"".$date ."\")'>$currentDay";

		}
		$calendar .= "<form action='". $_SERVER['PHP_SELF'] . "' name='".$date ."' method=POST><input type=hidden name=searchdate value='". $date. "'><input type=hidden name=aktion value='4'></form>";
		$calendar .= AuswertungImpfungdailycalendar($date);
		$calendar .= "</td>";

		$currentDay++;
		$dayOfWeek++;
	}
	if($dayOfWeek != 7){
		$remainingDays = 7 - $dayOfWeek;
		$calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";
	}
	$calendar .= "</tr>";
	$calendar .= "</table>";
	return $calendar;
}
 
$calendar = build_calendar(7, 2021);

//$calendar = '<div style="width:200px">' . $calendar . '</div>';

$calendar .= '<style type="text/css">table tbody tr td, table tbody tr th { text-align: center; }</style>';


$calendar .= '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>';
$calendar .= "<script>function submitForm(sub) {
	result = $.ajax({
		type: 'POST',
		async: false, 
		url: 'impfadmin.php',
		data: ({
			aktion: 4,
			searchdate: sub
		})
	}).responseText;
	//window.location.reload(false); 
	document.body.innerHTML = result;
}</script>";

/*
$calendar .= "<script>function submitForm(sub) {
        // Form fields, see IDs above
        const params = {
            aktion: 4
        }

        const http = new XMLHttpRequest()
        http.open('POST', 'impfadmin.php')
        http.setRequestHeader('Content-type', 'application/json')
        http.send(JSON.stringify(params)) // Make sure to stringify
        http.onload = function() {
            // Do whatever with response
            //alert(sub)
			document.body.innerHTML = http.responseText;
        }
}</script>";
*/
print $calendar; 



    include_once('footer.php');

?>