<?php
$pageTitle = 'Dispatch Calendar';
$first = new DateTime($start);
$daysInMonth = (int) $first->format('t');
$startWeekday = (int) $first->format('N');
$previousMonth = date('Y-m', strtotime($start . ' -1 month'));
$nextMonth = date('Y-m', strtotime($start . ' +1 month'));
?>
<div class="page-heading"><div><p class="eyebrow">Daily Planning</p><h2><?= e($first->format('F Y')) ?></h2><p>Pickup and delivery schedule for dispatch operations.</p></div><div class="button-row"><a class="btn btn-light" href="<?= url('/loads/calendar?month='.$previousMonth) ?>">Previous</a><a class="btn btn-light" href="<?= url('/loads/calendar?month='.date('Y-m')) ?>">Today</a><a class="btn btn-light" href="<?= url('/loads/calendar?month='.$nextMonth) ?>">Next</a></div></div>
<section class="card calendar-card">
<div class="calendar-grid calendar-head"><?php foreach (['Mon','Tue','Wed','Thu','Fri','Sat','Sun'] as $day): ?><div><?= e($day) ?></div><?php endforeach; ?></div>
<div class="calendar-grid">
<?php for ($blank=1; $blank<$startWeekday; $blank++): ?><div class="calendar-day is-empty"></div><?php endfor; ?>
<?php for ($day=1; $day<=$daysInMonth; $day++): $date=$month.'-'.str_pad((string)$day,2,'0',STR_PAD_LEFT); ?>
<div class="calendar-day <?= $date===date('Y-m-d')?'is-today':'' ?>"><div class="calendar-date"><?= $day ?></div><?php foreach ($events[$date] ?? [] as $event): $load=$event['load']; ?><a class="calendar-event event-<?= e($event['type']) ?>" href="<?= url('/loads/edit?id='.$load['id']) ?>"><strong><?= e(ucfirst($event['type']).' · '.$load['load_number']) ?></strong><span><?= e(date('g:i A', strtotime($event['type']==='pickup'?$load['pickup_datetime']:$load['delivery_datetime']))) ?></span><small><?= e($load['driver_name'] ?: 'Unassigned') ?><?= $load['unit_number'] ? ' · '.$load['unit_number'] : '' ?></small></a><?php endforeach; ?></div>
<?php endfor; ?>
</div></section>
