<?php

//--------------------------------------------------------------------------------------------------
// This script reads event data from a JSON file and outputs those events which are within the range
// supplied by the "start" and "end" GET parameters.
//
// An optional "timeZone" GET parameter will force all ISO8601 date stings to a given timeZone.
//
// Requires PHP 5.2.0 or higher.
//--------------------------------------------------------------------------------------------------

// Require our Event class and datetime utilities
require dirname(__FILE__) . '/utils.php';

    //database details
    $dbHost     = 'localhost';
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName     = 'prenotazioni';

    //create connection and select DB
    $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
    if($db->connect_error){
        die("Unable to connect database: " . $db->connect_error);
    }

	// print_r($_GET);
	// exit(0);
	
	$id  = $_GET['id'];
	$title  = $_GET['title'];
	// $start  = isset($_GET['start']) ? date('Y-m-d', strtotime(urldecode($_GET['start']))) : '';
	// $end  = isset($_GET['end']) ? date('Y-m-d', strtotime(urldecode($_GET['end']))) : '';
	$start  = isset($_GET['start']) ? urldecode($_GET['start']) : '';
	$end  = isset($_GET['end']) ? urldecode($_GET['end']) : '';
	$allDay  = $_GET['allDay'];
	$descr  = isset($_GET['desc']) ? $_GET['desc'] : '';
	$color  = isset($_GET['color']) ? $_GET['color'] : '';
	$type  = isset($_GET['type']) ? $_GET['type'] : '';
	
	// echo "ID: ".$id;
	// echo '<br>';
	// echo $start;
	// echo '<br>';
	// echo $end;
	// echo '<br>';
	// Fri Jan 07 2022 00:00:00 GMT+0100 (Ora standard dell’Europa centrale)
	// echo "START: " . parseDateTime(substr($start,0,23))->format('Y-m-d H:i:s') ;
	// echo '<br>';
	// if ($end != 'null') {
		// echo "END: " . parseDateTime(substr($end,0,23))->format('Y-m-d H:i:s') ;
		// echo '<br>';
	// }
	// echo "ALL DAY: ".$allDay;
	// echo '<br>';
	// echo "COLOR: ". $color;
	// exit(0);
	
	$data = array();

	$data['result'] = 0;
	if ($allDay == 'true') {
		$start = parseDateTime(substr($start,0,23))->format('Y-m-d');
		$sql    = "update test set title = ?, start = ?, allday=1 where id = ?";
		$stmt   = $db->prepare($sql);
		$stmt->bind_param('ssd', $title, $start, $id);
		if($stmt->execute()){
			$data['result'] = 1;
		}	
	}
	else {
		$start = parseDateTime(substr($start,0,23))->format('Y-m-d H:i:s');
		$end = parseDateTime(substr($end,0,23))->format('Y-m-d H:i:s');
		if ($type == '') {
			$sql    = "update test set title = ?, start = ?, end = ?, allday=0 where id = ? ";
			$stmt   = $db->prepare($sql);
			$stmt->bind_param('sssi', $title, $start, $end, $id);
			if($stmt->execute())
				$data['result'] = 2;		
		} else {
			$sql    = "update test set title = ?, start = ?, end = ?, descr = ?, color = ?, type = ?, allday=0 where id = ? ";
			$stmt   = $db->prepare($sql);
			$stmt->bind_param('ssssssi', $title, $start, $end, $descr, $color, $type, $id);
			if($stmt->execute())
				$data['result'] = 3;
		}		
	}
	// echo "<br>RES: ".$result;
	$json = json_encode($data);
	echo $json;
