<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Monitoring Request ESP8266</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f4f4f4;
        }
    </style>
</head>
<body>
    <h1>Monitoring Request ESP8266</h1>
    <p>Hanya mencatat request yang dikirim oleh ESP8266.</p>
    <button id="refresh">Refresh Log</button>
    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Waktu Request</th>
            </tr>
        </thead>
        <tbody id="request-log">
            <!-- Data akan dimuat di sini -->
        </tbody>
    </table>

    <script>
        async function fetchLogs() {
            try {
                const response = await fetch('https://iot2.ruangbelajar.xyz/2024-EWS/get_esp_logs.php');
                const logs = await response.json();

                const tableBody = document.getElementById('request-log');
                tableBody.innerHTML = '';

                logs.forEach((log, index) => {
                    const newRow = document.createElement('tr');
                    newRow.innerHTML = `
                        <td>${index + 1}</td>
                        <td>${log.timestamp}</td>
                    `;
                    tableBody.appendChild(newRow);
                });
            } catch (error) {
                console.error('Error fetching logs:', error);
            }
        }

        // Refresh manual
        document.getElementById('refresh').addEventListener('click', fetchLogs);

        // Refresh otomatis setiap 10 detik
        setInterval(fetchLogs, 10000);

        // Panggil pertama kali
        fetchLogs();
    </script>
</body>
</html>
