{% extends "base.html" %}
{% block title %}Logs{% endblock %}
{% block heading %}Logs de sistema e auditoria{% endblock %}
{% block content %}

<section class="panel">
  <form method="get" class="filters">
    <input type="text" name="username" value="{{ username }}" placeholder="Usuário" />
    <input type="text" name="action" value="{{ action }}" placeholder="Ação (ex: bloqueio_incluido)" />
    <label class="inline">De <input type="date" name="date_from" value="{{ date_from }}" /></label>
    <label class="inline">Até <input type="date" name="date_to" value="{{ date_to }}" /></label>
    <button class="btn" type="submit">Filtrar</button>
    <a class="btn btn-ghost" href="{{ url_for('logs.index') }}">Limpar</a>
    <a class="btn" href="{{ url_for('logs.export_csv', username=username, action=action, date_from=date_from, date_to=date_to) }}">Exportar CSV</a>
  </form>

  <p class="hint">
    {{ total }} registro(s) encontrado(s). A exportação em CSV respeita os filtros aplicados acima.
  </p>

  {% if rows %}
  <table class="table">
    <thead><tr><th>ID</th><th>Data/Hora (UTC)</th><th>Usuário</th><th>Ação</th><th>Detalhe</th><th>IP</th></tr></thead>
    <tbody>
      {% for row in rows %}
      <tr>
        <td>{{ row.id }}</td>
        <td>{{ row.created_at }}</td>
        <td>{{ row.username }}</td>
        <td><span class="tag">{{ row.action }}</span></td>
        <td class="wrap">{{ row.detail }}</td>
        <td>{{ row.ip_address }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>

  {% if pages > 1 %}
  <nav class="pager">
    {% if page > 1 %}<a class="btn" href="{{ url_for('logs.index', page=page-1, username=username, action=action, date_from=date_from, date_to=date_to) }}">Anterior</a>{% endif %}
    <span>Página {{ page }} de {{ pages }}</span>
    {% if page < pages %}<a class="btn" href="{{ url_for('logs.index', page=page+1, username=username, action=action, date_from=date_from, date_to=date_to) }}">Próxima</a>{% endif %}
  </nav>
  {% endif %}
  {% else %}
  <p class="empty">Nenhum registro para os filtros informados.</p>
  {% endif %}
</section>

{% endblock %}
