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

  <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:16px">
    <div>
      <div class="page-title">Decisions</div>
      <div class="page-sub">Active bans, captchas, and manual decisions</div>
    </div>
    <button class="btn-primary" onclick="toggleAddForm()">Add Decision</button>
  </div>

  <div id="add-form" class="panel" style="display:none;margin-bottom:16px">
    <div class="panel-header">
      <span class="panel-title">New Decision</span>
      <button class="btn-ghost-sm" onclick="toggleAddForm()">Cancel</button>
    </div>
    <div class="panel-body">
      <form method="POST" action="/decisions/add">
        <input type="hidden" name="_csrf" value="{{.CSRFToken}}">
        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(170px,1fr));gap:16px;margin-bottom:16px">
          <div>
            <label class="field-label">Value</label>
            <input class="field-input" type="text" name="value" placeholder="1.2.3.4 or 1.2.3.0/24 or US" required autofocus>
          </div>
          <div>
            <label class="field-label">Scope</label>
            <select class="field-input" name="scope">
              <option value="Ip">IP</option>
              <option value="Range">Range / CIDR</option>
              <option value="Country">Country (2-letter)</option>
            </select>
          </div>
          <div>
            <label class="field-label">Type</label>
            <select class="field-input" name="type">
              <option value="ban">ban</option>
              <option value="captcha">captcha</option>
              <option value="throttle">throttle</option>
            </select>
          </div>
          <div>
            <label class="field-label">Duration</label>
            <input class="field-input" type="text" name="duration" placeholder="24h" value="24h" required>
            <div class="field-hint">Units: s m h d w</div>
          </div>
          <div>
            <label class="field-label">Scenario (optional)</label>
            <input class="field-input" type="text" name="scenario" placeholder="manual">
          </div>
        </div>
        <div style="display:flex;justify-content:flex-end">
          <button type="submit" class="btn-primary">Add Decision</button>
        </div>
      </form>
    </div>
  </div>

  <div class="panel" style="margin-bottom:16px">
    <div class="panel-body" style="padding:12px 18px">
      <form method="GET" action="/decisions" class="filter-bar">
        <select class="filter-select" name="type">
          <option value="">All types</option>
          <option value="ban"      {{if eq .Filter.Type "ban"}}selected{{end}}>ban</option>
          <option value="captcha"  {{if eq .Filter.Type "captcha"}}selected{{end}}>captcha</option>
          <option value="throttle" {{if eq .Filter.Type "throttle"}}selected{{end}}>throttle</option>
        </select>
        <select class="filter-select" name="scope">
          <option value="">All scopes</option>
          <option value="Ip"      {{if eq .Filter.Scope "Ip"}}selected{{end}}>IP</option>
          <option value="Range"   {{if eq .Filter.Scope "Range"}}selected{{end}}>Range</option>
          <option value="Country" {{if eq .Filter.Scope "Country"}}selected{{end}}>Country</option>
        </select>
        <input class="filter-input" type="text" name="value" placeholder="Search value..." value="{{.Filter.Value}}">
        <button type="submit" class="btn-secondary">Filter</button>
        {{if or .Filter.Type .Filter.Scope .Filter.Value}}<a href="/decisions" class="btn-ghost">Clear</a>{{end}}
      </form>
    </div>
  </div>

  <div class="panel">
    <form method="POST" action="/decisions/delete" id="bulk-form">
      <input type="hidden" name="_csrf" value="{{.CSRFToken}}">
      <div class="panel-header">
        <span class="panel-title">
          Page {{.Page}}{{if .HasNext}} (more available){{end}}
        </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 .Decisions}}
      <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>Value</th>
              <th>Type</th>
              <th>Scope</th>
              <th>Origin</th>
              <th>Scenario</th>
              <th>Duration</th>
              <th>Expires</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            {{range .Decisions}}
            <tr>
              <td><input type="checkbox" name="id" value="{{.ID}}" class="row-chk"></td>
              <td style="font-family:'JetBrains Mono',monospace;font-size:12px">{{.Value}}</td>
              <td><span class="badge {{decisionBadgeClass .Type}}">{{.Type}}</span></td>
              <td><span class="badge badge-gray">{{.Scope}}</span></td>
              <td><span class="badge {{originBadgeClass .Origin}}">{{.Origin}}</span></td>
              <td style="font-size:12px;color:var(--muted)">{{truncate .Scenario 24}}</td>
              <td style="font-family:'JetBrains Mono',monospace;font-size:12px">{{.Duration}}</td>
              <td style="font-size:12px;color:var(--muted)">{{truncate .Until 16}}</td>
              <td>
                <form method="POST" action="/decisions/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 this decision?')">Delete</button>
                </form>
              </td>
            </tr>
            {{end}}
          </tbody>
        </table>
      </div>
      {{else}}
      <div class="empty-state">
        <div class="empty-text">No decisions found</div>
        <div class="empty-sub">No active decisions match the current filters</div>
      </div>
      {{end}}
    </form>

    {{if or (gt .Page 1) .HasNext}}
    <div class="pagination">
      {{if gt .Page 1}}
        <a href="/decisions?page={{dec .Page}}&type={{.Filter.Type}}&scope={{.Filter.Scope}}&value={{.Filter.Value}}" class="btn-ghost-sm">Prev</a>
      {{end}}
      <span style="font-family:'JetBrains Mono',monospace;font-size:12px;color:var(--muted)">Page {{.Page}}</span>
      {{if .HasNext}}
        <a href="/decisions?page={{inc .Page}}&type={{.Filter.Type}}&scope={{.Filter.Scope}}&value={{.Filter.Value}}" class="btn-ghost-sm">Next</a>
      {{end}}
    </div>
    {{end}}
  </div>
</div>
{{end}}

{{define "scripts"}}
<script>
function toggleAddForm() {
  var f = document.getElementById('add-form');
  f.style.display = f.style.display === 'none' ? 'block' : 'none';
}
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 decision.'); return false; }
  return confirm('Delete ' + n + ' decision(s)?');
}
</script>
{{end}}
