<?= $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' => 'Calendar',
    'workspaceTitle'        => 'Scheduling Calendar',
    'workspaceDesc'         => 'Monthly view of employees scheduled on active contracts.',
]) ?>

<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">Employee contract coverage 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('scheduling/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 (array_slice($events, 0, 4) as $ev): ?>
                                <div class="badge bg-info text-dark mb-1 w-100 text-truncate" title="<?= esc($ev['label'] . ($ev['company'] ? ' @ ' . $ev['company'] : '')) ?>">
                                    <?= esc($ev['label']) ?>
                                </div>
                            <?php endforeach; ?>
                            <?php if (count($events) > 4): ?>
                                <div class="small text-muted">+<?= count($events) - 4 ?> more</div>
                            <?php endif; ?>
                        </td>
                        <?php
                        $day++;
                        $cell++;
                    }
                    while ($cell % 7 !== 0) {
                        echo '<td class="bg-light"></td>';
                        $cell++;
                    }
                    echo '</tr>';
                    ?>
                </tbody>
            </table>
        </div>
        <div class="mt-3 small text-muted">
            <span class="badge bg-info text-dark">■</span> Employee on contract
        </div>
    </div>
</div>

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