<?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 die Urlaubsübersicht sehen.');
}

include 'header.php';

// Jahr für Auswertung
$year = date('Y');

// Lade alle Mitarbeiter
$stmt = $pdo->prepare("SELECT id, vorname, nachname, email, urlaubstage FROM users ORDER BY nachname, vorname");
$stmt->execute();
$users = $stmt->fetchAll();

?>

<div class="container">
    <h2>Urlaubsübersicht (<?php echo $year; ?>)</h2>

    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>Mitarbeiter</th>
                <th>Email</th>
                <th>Anspruch</th>
                <th>Genutzt (<?php echo $year; ?>)</th>
                <th>Verbleibend</th>
                <th>Ausstehend</th>
                <th>Bevorstehende Urlaube</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($users as $u):
            $uid = $u['id'];

            // Genutzte Tage (genehmigt) im Jahr
            $s = $pdo->prepare("SELECT IFNULL(SUM(days),0) AS used FROM vacations WHERE user_id = ? AND status = 'genehmigt' AND YEAR(start_date) = ?");
            $s->execute([$uid, $year]);
            $used = (int)$s->fetchColumn();

            // Ausstehende Anträge
            $p = $pdo->prepare("SELECT COUNT(*) FROM vacations WHERE user_id = ? AND status = 'beantragt'");
            $p->execute([$uid]);
            $pending = (int)$p->fetchColumn();

            // Bevorstehende Urlaube (nächste 5)
            $n = $pdo->prepare("SELECT start_date, end_date, days, status FROM vacations WHERE user_id = ? AND end_date >= CURDATE() ORDER BY start_date LIMIT 5");
            $n->execute([$uid]);
            $upcoming = $n->fetchAll();

            $entitlement = isset($u['urlaubstage']) ? (int)$u['urlaubstage'] : 0;
            $remaining = $entitlement - $used;
        ?>
            <tr>
                <td><?php echo htmlspecialchars($u['vorname'] . ' ' . $u['nachname']); ?></td>
                <td><?php echo htmlspecialchars($u['email']); ?></td>
                <td><?php echo $entitlement; ?></td>
                <td><?php echo $used; ?></td>
                <td><?php echo $remaining; ?></td>
                <td><?php echo $pending; ?></td>
                <td>
                    <?php if (count($upcoming) == 0) { echo '-'; } else {
                        foreach ($upcoming as $up) {
                            echo htmlspecialchars($up['start_date'] . ' → ' . $up['end_date'] . ' (' . $up['days'] . 'd) ' . ' [' . $up['status'] . ']');
                            echo '<br>';
                        }
                    } ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

    <p>Hinweis: Ansprüche werden aus dem Feld <strong>users.urlaubstage</strong> gelesen. Falls dieses Feld leer ist, bitte in der Nutzerverwaltung pflegen.</p>

</div>

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