<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Suhu Ruangan Server Bulan <?php echo date('F Y', mktime(0, 0, 0, $month, 1, $year)); ?></title>
    <!-- Inter Font -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <!-- Optional: Custom CSS to override or add styles -->
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body class="bg-light-neumorphic">
    <div class="container-fluid py-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1 class="h3 text-gray-800 fw-bold">Laporan Suhu dan Kelembapan Ruangan Server</h1>
            <img src="https://via.placeholder.com/80x80?text=Logo" alt="Logo Perusahaan" class="img-fluid">
        </div>

        <div class="card shadow mb-4">
            <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-primary">Data Bulan: <?php echo date('F Y', mktime(0, 0, 0, $month, 1, $year)); ?></h6>
            </div>
            <div class="card-body">
                <div class="row mb-3">
                    <div class="col-md-6">
                        <p class="fw-bold">Indikator Nasional Suhu Normal:</p>
                        <p><?= htmlspecialchars($nationalIndicator['normal_temperature_min']) ?>°C - <?= htmlspecialchars($nationalIndicator['normal_temperature_max']) ?>°C</p>
                    </div>
                    <div class="col-md-6">
                        <p class="fw-bold">Indikator Nasional Kelembapan Normal:</p>
                        <p><?= htmlspecialchars($nationalIndicator['normal_humidity_min']) ?>% - <?= htmlspecialchars($nationalIndicator['normal_humidity_max']) ?>%</p>
                    </div>
                </div>

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

                <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>";

                                    $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>
    <!-- Bootstrap JS Bundle -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>