<?php
$first = new DateTimeImmutable($month . '-01');
$daysInMonth = (int) $first->format('t');
$startOffset = (int) $first->format('w');
$entriesByDay = [];
foreach ($entries as $entry) {
    $day = (int) date('j', strtotime($entry['event_date']));
    $entriesByDay[$day][] = $entry;
}
?>
<section class="calendar-toolbar">
    <div><p class="eyebrow">Due dates and milestones</p><p class="muted">Bills are added automatically from budget targets.</p></div>
    <button class="button primary" type="button" data-dialog-open="event-dialog">+ Add event</button>
</section>
<article class="card calendar-card">
    <div class="calendar-weekdays"><?php foreach (['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] as $weekday): ?><span><?= h($weekday) ?></span><?php endforeach; ?></div>
    <div class="calendar-grid">
        <?php for ($blank = 0; $blank < $startOffset; $blank++): ?><div class="calendar-day blank"></div><?php endfor; ?>
        <?php for ($day = 1; $day <= $daysInMonth; $day++): ?>
            <?php $isToday = date('Y-m-d') === $month . '-' . str_pad((string) $day, 2, '0', STR_PAD_LEFT); ?>
            <div class="calendar-day <?= $isToday ? 'today' : '' ?>">
                <span class="day-number"><?= $day ?></span>
                <?php foreach ($entriesByDay[$day] ?? [] as $entry): ?>
                    <div class="calendar-event <?= h($entry['event_type']) ?>">
                        <span><?= $entry['event_type'] === 'bill' ? '●' : '◆' ?></span>
                        <div><strong><?= h($entry['title']) ?></strong><?php if ($entry['amount'] !== null): ?><small><?= h(Support::money($entry['amount'], $symbol)) ?></small><?php endif; ?></div>
                        <?php if ($entry['event_type'] !== 'bill'): ?>
                            <form method="post" data-confirm="Remove this calendar event?">
                                <?= csrf_field() ?><input type="hidden" name="action" value="event.delete"><input type="hidden" name="month" value="<?= h($month) ?>"><input type="hidden" name="id" value="<?= (int) $entry['id'] ?>">
                                <button type="submit" aria-label="Delete event">×</button>
                            </form>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endfor; ?>
    </div>
</article>

<dialog id="event-dialog">
    <form method="post" class="dialog-card">
        <?= csrf_field() ?><input type="hidden" name="action" value="event.add"><input type="hidden" name="month" value="<?= h($month) ?>">
        <div class="card-heading"><div><p class="eyebrow">Personal milestone</p><h2>Add calendar event</h2></div><button class="icon-button" type="button" data-dialog-close>×</button></div>
        <div class="form-grid">
            <label class="span-2">Title<input type="text" name="title" required placeholder="Payday, renewal, deadline…"></label>
            <label>Date<input type="date" name="event_date" value="<?= h($month . '-01') ?>" required></label>
            <label>Type<select name="event_type"><option value="note">Note</option><option value="payday">Payday</option><option value="deadline">Deadline</option></select></label>
            <label class="span-2">Notes<textarea name="notes" rows="2"></textarea></label>
        </div>
        <button class="button primary full" type="submit">Add to calendar</button>
    </form>
</dialog>
