<div class="container-fluid">
    <h1 class="h3 mb-4 text-gray-800">Riwayat Suhu dan Kelembapan Ruangan Server</h1>

    <div class="row">
        <div class="col-lg-12">
            <div class="card shadow mb-4">
                <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Riwayat Per Bulan</h6>
                </div>
                <div class="card-body">
                    <form action="" method="GET" class="form-inline mb-4">
                        <input type="hidden" name="page" value="room_temperature_history">
                        <label for="month" class="mr-2">Bulan:</label>
                        <select name="month" id="month" class="form-control mr-2">
                            <?php for ($m = 1; $m <= 12; $m++): ?>
                                <option value="<?= sprintf('%02d', $m) ?>" <?= (isset($month) && $month == sprintf('%02d', $m)) ? 'selected' : '' ?>>
                                    <?= date('F', mktime(0, 0, 0, $m, 10)) ?>
                                </option>
                            <?php endfor; ?>
                        </select>
                        <label for="year" class="mr-2">Tahun:</label>
                        <select name="year" id="year" class="form-control mr-2">
                            <?php for ($y = date('Y'); $y >= date('Y') - 5; $y--): ?>
                                <option value="<?= $y ?>" <?= (isset($year) && $y == $year) ? 'selected' : '' ?>>
                                    <?= $y ?>
                                </option>
                            <?php endfor; ?>
                        </select>
                        <button type="submit" class="btn btn-primary">Filter</button>
                    </form>

                    <?php
                    // Ensure $month and $year are set to default if not present
                    $month = $month ?? date('m');
                    $year = $year ?? date('Y');

                    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
                    $firstDayOfMonth = date('N', strtotime("{$year}-{$month}-01")); // 1 (for Monday) through 7 (for Sunday)

                    // Adjust first day to start from Sunday if needed, or simply offset for display
                    // Here, we'll just use 1-7 (Mon-Sun) and fill leading blanks
                    ?>

                    <div class="table-responsive">
                        <table class="table table-bordered table-sm" style="table-layout: fixed; width: 100%;">
                            <thead>
                                <tr class="text-center">
                                    <th>Senin</th>
                                    <th>Selasa</th>
                                    <th>Rabu</th>
                                    <th>Kamis</th>
                                    <th>Jumat</th>
                                    <th>Sabtu</th>
                                    <th>Minggu</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <?php
                                    // Fill leading blank days
                                    for ($i = 1; $i < $firstDayOfMonth; $i++) {
                                        echo '<td></td>';
                                    }

                                    $dayCount = $firstDayOfMonth;
                                    for ($d = 1; $d <= $daysInMonth; $d++) {
                                        if ($dayCount > 7) {
                                            echo '</tr><tr>';
                                            $dayCount = 1;
                                        }

                                        echo '<td class="text-center" style="vertical-align: top;">';
                                        echo "<strong class='text-primary'>{$d}</strong><br>";

                                        // Ensure $calendarData is set and contains the expected key
                                        $day_key = sprintf('%02d', $d); // Format day with leading zero for consistency

                                        if (isset($calendarData) && isset($calendarData[$day_key])) {
                                            foreach ($calendarData[$day_key] as $record) {
                                                echo "<small>";
                                                echo date('H:i', strtotime($record['recorded_at'])) . ": ";
                                                echo htmlspecialchars($record['temperature']) . "°C / ";
                                                echo htmlspecialchars($record['humidity']) . "%";
                                                echo "</small><br>";
                                            }
                                        } else {
                                            echo "<small class='text-muted'>No Data</small>";
                                        }
                                        echo '</td>';
                                        $dayCount++;
                                    }

                                    // Fill trailing blank days
                                    while ($dayCount <= 7) {
                                        echo '<td></td>';
                                        $dayCount++;
                                    }
                                    ?>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
