<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
  <meta charset="UTF-8">
  <title>Dashboard</title>
  <link href="/static/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-dark text-light">
  <div class="container py-4">
    <h1>🧠 Admin Dashboard</h1>
    <ul class="nav nav-tabs">
      <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#uploads">Uploads</a></li>
      <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#downloads">Downloads</a></li>
      <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#chart">Chart</a></li>
    </ul>
    <div class="tab-content mt-3">
      <div class="tab-pane fade show active" id="uploads">
        <table class="table table-dark table-bordered">
          <thead><tr><th>IP</th><th>File</th><th>Time</th></tr></thead>
          <tbody>
            {{range .Events}} {{if eq .Action "upload"}}
            <tr><td>{{.IP}}</td><td>{{.File}}</td><td>{{.Timestamp.Format "2006-01-02 15:04:05"}}</td></tr>
            {{end}} {{end}}
          </tbody>
        </table>
      </div>
      <div class="tab-pane fade" id="downloads">
        <table class="table table-dark table-bordered">
          <thead><tr><th>IP</th><th>File</th><th>Time</th></tr></thead>
          <tbody>
            {{range .Events}} {{if eq .Action "download"}}
            <tr><td>{{.IP}}</td><td>{{.File}}</td><td>{{.Timestamp.Format "2006-01-02 15:04:05"}}</td></tr>
            {{end}} {{end}}
          </tbody>
        </table>
      </div>
      <div class="tab-pane fade" id="chart">
        <div class="bg-light p-3 rounded text-dark">
          <canvas id="downloadChart" height="120"></canvas>
        </div>
      </div>
    </div>
  </div>
  <script src="/static/bootstrap.bundle.min.js"></script>
  <script src="/static/chart.min.js"></script>
  <script>
    fetch("/dashboard-data").then(r => r.json()).then(data => {
      const ctx = document.getElementById('downloadChart').getContext('2d')
      const labels = data.map(e => e.ip + ": " + e.file)
      const values = data.map(e => e.count)
      new Chart(ctx, {
        type: 'bar',
        data: {
          labels: labels,
          datasets: [{ label: "Downloads", data: values, backgroundColor: '#00d1b2' }]
        },
        options: {
          scales: {
            x: { ticks: { color: "white" } },
            y: { ticks: { color: "white" }, beginAtZero: true }
          }
        }
      })
    })
  </script>
</body>
</html>
