<%- include("partials/header", { title: "Entries" , user }) %>

  <h1>Entries</h1>
  <div class="card" style="margin-top:12px;">
    <form method="get" action="/entries" class="row" style="gap:12px; align-items:flex-end;">
      <div style="flex:1;">
        <label>View operation</label>
        <select name="preset">
          <option value="all" <%=(selectedId==="all" ) ? "selected" : "" %>>View All (All-time)</option>

          <% if (!activeOps || activeOps.length===0) { %>
            <option value="">No active operations</option>
            <% } else { %>
              <% activeOps.forEach(p=> { %>
                <option value="<%= p.id %>" <%=(selectedId !=="all" && Number(selectedId)===Number(p.id)) ? "selected"
                  : "" %>>
                  <%= p.title %> — <%= p.topic %>
                </option>
                <% }) %>
                  <% } %>
        </select>
      </div>

      <button type="submit">Apply</button>
      <a class="btn" href="/entries">Reset</a>
    </form>
  </div>
  <% if (preset) { %>
    <form method="post" action="/entries/clear-preset" style="margin:12px 0;">
      <input type="hidden" name="preset_id" value="<%= preset.id %>">
      <button type="submit" onclick="return confirm('Delete ALL entries for this operation? This cannot be undone.');">
        Delete all entries for this operation
      </button>
    </form>
    <% } %>


      <% if (preset) { %>
        <p style="opacity:.85;"><b>Showing:</b>
          <%= preset.title %> (<%= fmtMelbourne(preset.start_at) %> → <%= fmtMelbourne(preset.end_at) %>)
        </p>
        <% } else { %>
          <p style="opacity:.85;"><b>Showing:</b> All entries (all-time)</p>
          <% } %>


            <form method="post" action="/entries/clear" style="margin-bottom:12px;"
              onsubmit="return confirm('DELETE ALL ENTRIES? This cannot be undone.')">
              <button type="submit">Delete All Entries</button>
            </form>

            <table>
              <thead>
                <tr>
                  <th>ID</th>
                  <th>Time</th>
                  <th>Officer</th>
                  <th>Report #</th>
                  <th>Evidence</th>
                  <th>Suspect</th>
                  <th>Charges</th>
                  <th></th>
                </tr>
              </thead>
              <tbody>
                <% (entries || []).forEach(e=> { %>
                  <tr>
                    <td>
                      <%= e.id %>
                    </td>
                    <td>
                      <%= fmtMelbourne(e.created_at) %>
                    </td>

                    <td>
                      <%= e.officer || "" %>
                    </td>
                    <td>
                      <%= e.report_number || "" %>
                    </td>
                    <td>
                      <% if (e.evidence) { %>
                        <a href="<%= normalizeUrl(e.evidence) %>" target="_blank" rel="noopener noreferrer">
                          Open
                        </a>
                        <% } %>
                    </td>

                    <td>
                      <%= e.suspect || "" %>
                    </td>
                    <td>
                      <%= e.charges || "" %>
                    </td>
                    <td style="white-space:nowrap;">
                      <a class="btn" href="/entries/<%= e.id %>/edit">Edit</a>
                      <form method="post" action="/entries/<%= e.id %>/delete" style="display:inline;"
                        onsubmit="return confirm('Delete this entry?')">
                        <button type="submit">Delete</button>
                      </form>
                    </td>
                  </tr>
                  <% }) %>
              </tbody>
            </table>

            <%- include("partials/footer") %>