<?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);
	
	$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  = isset($_GET['allDay']) ? $_GET['allDay'] : 'false';
	$type  = isset($_GET['type']) ? $_GET['type'] : '';
	$color  = isset($_GET['color']) ? $_GET['color'] : '';
	$descr  = isset($_GET['desc']) ? $_GET['desc'] : '';
	
	// 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>';
	// echo "END: " . parseDateTime(substr($end,0,23))->format('Y-m-d H:i:s') ;
	// echo '<br>';
	// echo "ALL DAY: ".$allDay;
	// exit(0);
	
	$data = array();

	$data['result'] = 0;
	if ($allDay == 'true') {
		$start = parseDateTime(substr($start,0,23))->format('Y-m-d');
		$sql    = "insert into test (title,start,type,color,descr,allday) values (?,?,?,?,?,1) ";
		$type = (int)$type;
		if($stmt   = $db->prepare($sql)) { // assuming $mysqli is the connection
			$stmt->bind_param('ssiss', $title, $start, $type, $color, $descr);
			$stmt->execute();
			$data['result'] = 1;
		} else {
			$error = $db->errno . ' ' . $db->error;
			// echo '<br>';
			// echo $error; // 1054 Unknown column 'foo' in 'field list'
			$data['error'] = 0;
		}
	}
	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');
		$sql    = "insert into test (title,start,end,type,color,descr,allday) values (?,?,?,?,?,?,0)  ";
		$type = (int)$type;
		if($stmt   = $db->prepare($sql)) { // assuming $mysqli is the connection
			$stmt->bind_param('sssiss', $title, $start, $end, $type, $color, $descr);
			$stmt->execute();
			$data['result'] = 1;
			// echo "ID = " . $db -> insert_id;
			$data['id'] = $db -> insert_id;
		} else {
			$error = $db->errno . ' ' . $db->error;
			// echo '<br>';
			// echo $error; // 1054 Unknown column 'foo' in 'field list'
			$data['error'] = 0;
		}				
	}
	// echo "<br>RES: ".$result;
	$json = json_encode($data);
	echo $json;
