{{template "base" .}}
{{define "content"}}
<div style="max-width:1400px">

  <div style="margin-bottom:16px">
    <div class="page-title">Alerts</div>
    <div class="page-sub">Security events detected by CrowdSec agents</div>
  </div>

  <div class="panel" style="margin-bottom:16px">
    <div class="panel-body" style="padding:12px 18px">
      <form method="GET" action="/alerts" class="filter-bar">
        <input class="filter-input" type="text" name="scenario" placeholder="Scenario filter..." value="{{.Filter.Scenario}}">
        <input class="filter-input" type="text" name="ip" placeholder="Source IP..." value="{{.Filter.IP}}">
        <select class="filter-select" name="since">
          <option value="">All time</option>
          <option value="1h"  {{if eq .Filter.Since "1h"}}selected{{end}}>Last 1h</option>
          <option value="24h" {{if eq .Filter.Since "24h"}}selected{{end}}>Last 24h</option>
          <option value="7d"  {{if eq .Filter.Since "7d"}}selected{{end}}>Last 7d</option>
        </select>
        <button type="submit" class="btn-secondary">Filter</button>
        {{if or .Filter.Scenario .Filter.IP .Filter.Since}}<a href="/alerts" class="btn-ghost">Clear</a>{{end}}
      </form>
    </div>
  </div>

  <div class="panel">
    <form method="POST" action="/alerts/delete" id="bulk-form">
      <input type="hidden" name="_csrf" value="{{.CSRFToken}}">
      <div class="panel-header">
        <span class="panel-title">Alerts ({{len .Alerts}} shown)</span>
        <div style="display:flex;gap:8px;align-items:center">
          <button type="button" class="btn-ghost-sm" onclick="toggleAll()">Select all</button>
          <button type="submit" class="btn-danger-sm" onclick="return confirmBulkDelete()">Delete selected</button>
        </div>
      </div>

      {{if .Alerts}}
      <div style="overflow-x:auto">
        <table class="data-table">
          <thead>
            <tr>
              <th style="width:36px"><input type="checkbox" id="chk-all" onchange="selectAll(this)"></th>
              <th>ID</th>
              <th>Scenario</th>
              <th>Source</th>
              <th>Country</th>
              <th>Events</th>
              <th>Decisions</th>
              <th>Date</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            {{range .Alerts}}
            <tr>
              <td><input type="checkbox" name="id" value="{{.ID}}" class="row-chk"></td>
              <td style="font-family:'JetBrains Mono',monospace;font-size:12px">{{.ID}}</td>
              <td style="font-size:12px" title="{{.Scenario}}">{{truncate .Scenario 36}}</td>
              <td style="font-family:'JetBrains Mono',monospace;font-size:12px">{{.Source.Value}}</td>
              <td style="font-size:12px;color:var(--muted)">{{.Source.CN}}</td>
              <td style="font-family:'JetBrains Mono',monospace">{{.EventsCount}}</td>
              <td style="font-family:'JetBrains Mono',monospace">{{len .Decisions}}</td>
              <td style="font-size:12px;color:var(--muted)">{{truncate .StartAt 16}}</td>
              <td>
                <form method="POST" action="/alerts/delete" style="display:inline">
                  <input type="hidden" name="_csrf" value="{{$.CSRFToken}}">
                  <input type="hidden" name="id" value="{{.ID}}">
                  <button type="submit" class="btn-danger-sm" onclick="return confirm('Delete alert #{{.ID}}?')">Delete</button>
                </form>
              </td>
            </tr>
            {{end}}
          </tbody>
        </table>
      </div>
      {{else}}
      <div class="empty-state">
        <div class="empty-text">No alerts found</div>
        <div class="empty-sub">No security events match the current filters</div>
      </div>
      {{end}}
    </form>
  </div>
</div>
{{end}}

{{define "scripts"}}
<script>
function selectAll(cb) {
  document.querySelectorAll('.row-chk').forEach(function(c) { c.checked = cb.checked; });
}
function toggleAll() {
  var cb = document.getElementById('chk-all');
  cb.checked = !cb.checked;
  selectAll(cb);
}
function confirmBulkDelete() {
  var n = document.querySelectorAll('.row-chk:checked').length;
  if (n === 0) { alert('Select at least one alert.'); return false; }
  return confirm('Delete ' + n + ' alert(s)?');
}
</script>
{{end}}
