<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    exit("unauthorized");
}

require_once "../database.php";

$user_id = $_GET["user_id"];

$eventArray = array();

/* PERMESSI */
$sqlQuery = "SELECT * FROM permissions WHERE user_id = $user_id AND status_id = 2";
$result = mysqli_query($con, $sqlQuery);
while ($row = mysqli_fetch_assoc($result)) {
    $sql_justification        = "SELECT * FROM justifications WHERE id = " . $row['justification_id'] . "";
    $result_justification     = mysqli_query($con, $sql_justification);
    $justification = mysqli_fetch_assoc($result_justification);

    if ($row["full_day"] == 1) {
        $full_day = true;
        $ending_date = date('Y-m-d', strtotime('+1 day', strtotime($row['end_date'])));
        $end = $ending_date . "T" . $row['end_time'];
    } else {
        $full_day = false;
        $end = $row['end_date'] . "T" . $row['end_time'];
    }

    $start = $row['start_date'] . "T" . $row['start_time'];
    $color = '#ff9800';
    if ($row["full_day"] == 1) {
        $title = $justification['name'];
    } else {
        $title = " - " . $justification['name'];
    }

    $array = array(
        'id' => $row['id'],
        'title' => $title,
        'start' => $start,
        'end' => $end,
        'allDay' => $full_day,
        'backgroundColor' => $color,
        'groupId' => 'permission'
    );

    array_push($eventArray, $array);
}


/* TIMBRATURE */
$sqlQuery = "SELECT * FROM stamps WHERE user_id = $user_id ORDER BY date";
$result = mysqli_query($con, $sqlQuery);
while ($row = mysqli_fetch_assoc($result)) {
    $start = $row['date'] . "T" . $row['time'];

    $title = " - " . $row['type'];
    if ($row['type'] == "Entrata") {
        $color = '#2196f3';
    } else {
        $color = '#009688';
    }

    $array = array(
        'id' => $row['id'],
        'title' => $title,
        'start' => $start,
        'allDay' => false,
        'backgroundColor' => $color,
        'groupId' => 'stamp'
    );

    array_push($eventArray, $array);
}

/* ERRORI */
$sql_problems_range    = "SELECT * FROM problems_date_range WHERE user_id = 0";
$result_problems_range = mysqli_query($con, $sql_problems_range);
$last_update_problems = mysqli_fetch_assoc($result_problems_range);

$sql_problems_range_user    = "SELECT * FROM problems_date_range WHERE user_id = " . $_SESSION["id"] . "";
$result_problems_range_user = mysqli_query($con, $sql_problems_range_user);
$last_update_problems_user = mysqli_fetch_assoc($result_problems_range_user);

if ($last_update_problems["datetime"] >= $last_update_problems_user["datetime"]) {
    $sqlQuery = "SELECT * FROM problems WHERE run_user_id = 0 AND user_id = $user_id ORDER BY date DESC";
} else {
    $sqlQuery = "SELECT * FROM problems WHERE run_user_id = " . $_SESSION["id"] . " AND user_id = $user_id ORDER BY date DESC";
}
$result = mysqli_query($con, $sqlQuery);
while ($row = mysqli_fetch_assoc($result)) {
    $start = $row['date'];

    $title = substr($row['suggestion'], 0, 25) . "...";
    $color = '#f44336';

    $array = array(
        'id' => $row['id'],
        'title' => $title,
        'start' => $start,
        'allDay' => true,
        'backgroundColor' => $color,
        'groupId' => 'problem'
    );

    array_push($eventArray, $array);
}


mysqli_free_result($result);

mysqli_close($con);
echo json_encode($eventArray);
