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

$user = check_user();
if (!is_admin_user()) {
    die('Zugriff verweigert. Nur Chefs dürfen den Kalender sehen.');
}

include 'header.php';
?>

<div class="container">
    <h2>Team Urlaubskalender</h2>
    <div id="calendar"></div>
    <br>
    <div>
        <span class="badge badge-success">genehmigt</span>
        <span class="badge badge-warning">beantragt</span>
        <span class="badge badge-primary">Betriebsurlaub</span>
    </div>
    <br>
    <div id="eventDetails" style="display:none;">
        <h4>Details</h4>
        <div id="detailsContent"></div>
    </div>
</div>

<link href='https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.css' rel='stylesheet' />
<script src='https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.js'></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');

    var calendar = new FullCalendar.Calendar(calendarEl, {
        initialView: 'dayGridMonth',
        firstDay: 1,
        height: 650,
        events: function(info, successCallback, failureCallback) {
            var url = 'api/vacations.php?start=' + info.startStr + '&end=' + info.endStr;
            fetch(url).then(function(res){ return res.json(); }).then(function(data){ successCallback(data); }).catch(function(err){ failureCallback(err); });
        },
        eventClick: function(info) {
            var ev = info.event;
            var props = ev.extendedProps;
            var html = '<strong>' + ev.title + '</strong><br>' + ev.start.toLocaleDateString() + ' - ' + (new Date(ev.end).toLocaleDateString()) + '<br>';
            if (props.type === 'user') {
                html += 'Status: ' + (props.status || '') + '<br>';
                html += 'Kommentar: ' + (props.comment || '') + '<br>';
            } else if (props.type === 'company') {
                html += 'Beschreibung: ' + (props.description || '') + '<br>';
            }
            document.getElementById('detailsContent').innerHTML = html;
            document.getElementById('eventDetails').style.display = 'block';
        }
    });

    calendar.render();
});
</script>

<?php include 'footer.php'; ?>
