<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Recent Connections</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      /* Use full width */
      .container-fluid {
        height: 100%;
        display: flex;
        flex-direction: column;
      }
      /* Header area takes as much room as needed; the table container fills the rest */
      .table-container {
        flex: 1;
        overflow-y: auto;
      }
    </style>
  </head>
  <body>
    <div class="container-fluid">
      <h1 class="mb-4">Downloads in den letzten 10 Minute</h1>
      <div class="mb-3">
        <a href="{{ url_for('index') }}" class="btn btn-primary mt-1">App</a>
        <a href="{{ url_for('mylinks') }}" class="btn btn-primary mt-1">Meine Links</a>
        <a href="{{ url_for('connections') }}" class="btn btn-primary mt-1">Verbindungen</a>
        <a href="{{ url_for('dashboard') }}" class="btn btn-primary mt-1">Auswertung</a>
      </div>
      <div class="table-responsive table-container">
        <table class="table table-hover">
          <thead class="table-info">
            <tr>
              <th>Timestamp</th>
              <th>IP Address</th>
              <th>User Agent</th>
              <th>File Path</th>
            </tr>
          </thead>
          <tbody id="connectionsTableBody">
            <!-- Rows will be dynamically inserted here -->
          </tbody>
        </table>
      </div>
    </div>

    <!-- Socket.IO client library -->
    <script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
    <script>
      const socket = io();

      // Request initial data immediately on connection
      socket.on('connect', () => {
        socket.emit('request_initial_data');
      });

      socket.on('recent_connections', function(data) {
        const tbody = document.getElementById('connectionsTableBody');
        tbody.innerHTML = ''; // Clear previous content

        data.forEach(record => {
          const row = document.createElement('tr');
          row.innerHTML = `
            <td>${record.timestamp}</td>
            <td>${record.ip_address}</td>
            <td>${record.user_agent}</td>
            <td>${record.full_path}</td>
          `;
          tbody.appendChild(row);
        });
      });
    </script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>
