<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$monthNames = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
$today = date('Y-m-d');
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Leave Calendar',
    'workspaceTitle'      => 'Leave Calendar Workspace',
    'workspaceDesc'       => 'Monthly view of approved leave and public holidays across the organization.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head d-flex justify-content-between align-items-center flex-wrap gap-2">
        <div>
            <h2 class="enterprise-data-panel__title mb-0"><?= esc($monthNames[$month]) ?> <?= esc($year) ?></h2>
            <p class="enterprise-data-panel__subtitle mb-0">Approved leave and holidays for the selected month.</p>
        </div>
        <div class="d-flex gap-2">
            <a href="<?= esc($prevUrl) ?>" class="btn btn-sm btn-outline-secondary"><i class="fa-solid fa-chevron-left"></i> Prev</a>
            <a href="<?= base_url('leave/calendar') ?>" class="btn btn-sm btn-outline-secondary">Today</a>
            <a href="<?= esc($nextUrl) ?>" class="btn btn-sm btn-outline-secondary">Next <i class="fa-solid fa-chevron-right"></i></a>
        </div>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table text-center" style="table-layout:fixed;">
                <thead>
                    <tr>
                        <?php foreach (['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] as $dow): ?>
                            <th class="small text-muted"><?= $dow ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $day = 1;
                    $cell = 0;
                    echo '<tr>';
                    for ($i = 1; $i < $firstDow; $i++) {
                        echo '<td class="bg-light"></td>';
                        $cell++;
                    }
                    while ($day <= $daysInMonth) {
                        if ($cell > 0 && $cell % 7 === 0) {
                            echo '</tr><tr>';
                        }
                        $dateKey = sprintf('%04d-%02d-%02d', $year, $month, $day);
                        $isToday = $dateKey === $today;
                        $events  = $eventsByDay[$dateKey] ?? [];
                        ?>
                        <td class="align-top p-2<?= $isToday ? ' bg-primary bg-opacity-10' : '' ?>" style="min-height:100px;vertical-align:top;">
                            <div class="fw-semibold small mb-1"><?= $day ?></div>
                            <?php foreach ($events as $ev): ?>
                                <?php if ($ev['type'] === 'holiday'): ?>
                                    <div class="badge bg-danger mb-1 w-100 text-truncate" title="<?= esc($ev['label']) ?>"><?= esc($ev['label']) ?></div>
                                <?php else: ?>
                                    <div class="badge bg-info text-dark mb-1 w-100 text-truncate" title="<?= esc($ev['label']) ?>"><?= esc($ev['label']) ?></div>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </td>
                        <?php
                        $day++;
                        $cell++;
                    }
                    while ($cell % 7 !== 0) {
                        echo '<td class="bg-light"></td>';
                        $cell++;
                    }
                    echo '</tr>';
                    ?>
                </tbody>
            </table>
        </div>
        <div class="mt-3 d-flex gap-3 small text-muted">
            <span><span class="badge bg-info text-dark">■</span> Approved Leave</span>
            <span><span class="badge bg-danger">■</span> Holiday</span>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
