<?php
// ============================================================
//  member/calendar.php — Calendar with SA holidays + events
// ============================================================
//
//  Month-view calendar showing:
//    - South African public holidays
//    - Events created by admins with show_to_members=1
//    - Weekends (tinted)
//
//  Members see events read-only (no click-through detail page
//  yet — title only).
// ============================================================

$page_title = 'Calendar';
require __DIR__ . '/_guard.php';
require_once __DIR__ . '/../includes/calendar.php';

$year  = (int)($_GET['y'] ?? date('Y'));
$month = (int)($_GET['m'] ?? date('n'));
if ($year < 2000 || $year > 2100) $year = (int)date('Y');
if ($month < 1 || $month > 12)    $month = (int)date('n');

// Compute month bounds (±7 days so leading/trailing grid cells are included)
$first_of_month = sprintf('%04d-%02d-01', $year, $month);
$range_start = date('Y-m-d', strtotime('-7 days', strtotime($first_of_month)));
$range_end   = date('Y-m-d', strtotime('+7 days', strtotime("last day of $first_of_month")));

// Member-visible events only
$event_rows = calendar_fetch_events($range_start, $range_end, true);
$events     = calendar_expand_events($event_rows, null);  // no URLs for members
?>

<style>
<?= calendar_css() ?>
</style>

<section class="section"><div class="container">

<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:1rem;flex-wrap:wrap;gap:1rem;">
    <h1 style="margin:0;">Calendar</h1>
    <p style="margin:0;color:var(--ink-muted);font-size:.85rem;">
        Events &amp; South African public holidays
    </p>
</div>

<?= calendar_render($year, $month, $events, ['base_url' => 'calendar.php']) ?>

<div style="margin-top:1rem;padding:.75rem 1rem;background:var(--surface-alt);border-radius:6px;font-size:.78rem;color:var(--ink-muted);display:flex;gap:1rem;flex-wrap:wrap;align-items:center;">
    <?php if (!empty($event_rows)): ?>
        <span style="display:inline-flex;align-items:center;gap:.4rem;">
            <span style="display:inline-block;width:14px;height:14px;border-radius:3px;background:#ede9fe;"></span>
            Event
        </span>
    <?php endif; ?>
    <span style="display:inline-flex;align-items:center;gap:.4rem;">
        <span style="display:inline-block;width:14px;height:14px;border-radius:3px;background:#fed7aa;"></span>
        SA public holiday
    </span>
    <span style="display:inline-flex;align-items:center;gap:.4rem;">
        <span style="display:inline-block;width:14px;height:14px;border-radius:3px;background:#f9f7f1;border:1px solid #e5e7eb;"></span>
        Weekend
    </span>
</div>

</div></section>

<?php require __DIR__ . '/_footer.php'; ?>