<?php
/*
$events = array();
$events[] = [
    "title"=> "Repair Event",
    "start"=> "2018-02-02",
    "rendering" => "background",
    "className" => 'estimate-repair-block'
];

echo json_encode($events);*/

include_once( dirname(dirname(__FILE__)) . '/../classes/check.class.php');
include_once __DIR__ . '/../classes/functions.php';
protect( implode(',', [Permission::USER_LEVEL_1, Permission::USER_LEVEL_3, Permission::USER_LEVEL_4]));

$_GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);

$start = $_GET['start'];
$end   = $_GET['end'];

$db = DB::getInstance();

$appointments = $db->query('SELECT * FROM appointment WHERE date BETWEEN ? AND ? GROUP BY date, type ORDER BY date ASC', [ $start, $end ])->toArray();

$combined = array();

foreach ( $appointments as $appointment ) {
    $combined[$appointment['date']][] = $appointment;
}

$events = array();

foreach ( $combined as $date => $appointmentData ) {
    if ( count( $appointmentData ) == 1 && $appointmentData[0]['type'] == 'repair' ) {
        $events[] = array(
            "title"=> "Estimate Event",
            "start"=> $date,
            "rendering" => "background",
            "className" => 'estimate-repair-block'
        );
    }
}

echo json_encode($events);
