{% extends 'base.html' %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="h3 mb-1">Logs</h1>
    <p class="text-muted mb-0">Errores capturados por acciones del sistema e integraciones externas.</p>
  </div>
</div>
<div class="card shadow-sm">
  <div class="card-body p-0">
    {% for item in logs %}
    <div class="audit-line px-3">
      <div class="d-flex justify-content-between gap-3 flex-wrap">
        <div><strong>{{ item.error_type }}</strong> · {{ item.function_name }}<br><span class="small text-muted">{{ item.created_at.strftime('%d/%m/%Y %H:%M:%S') }} · {{ item.route or 'sin ruta' }} · {{ item.actor_email or 'system' }}</span></div>
        <span class="badge text-bg-danger">Error</span>
      </div>
      <div class="mt-2">{{ item.message }}</div>
      {% if item.extra %}<div class="small text-muted mt-1">Contexto: {{ item.extra }}</div>{% endif %}
      {% if item.stacktrace %}<details class="mt-2"><summary class="small">Ver stacktrace</summary><pre class="code-box mt-2">{{ item.stacktrace }}</pre></details>{% endif %}
    </div>
    {% else %}
    <div class="p-4 text-center text-muted">No hay errores registrados.</div>
    {% endfor %}
  </div>
  {% if pagination.pages > 1 %}
  <div class="card-body border-top">
    <nav><ul class="pagination mb-0 justify-content-end">
      <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}"><a class="page-link" href="{{ url_for('admin_logs', page=pagination.prev_num) }}">Anterior</a></li>
      {% for page_num in pagination.iter_pages() %}
        {% if page_num %}<li class="page-item {% if page_num == pagination.page %}active{% endif %}"><a class="page-link" href="{{ url_for('admin_logs', page=page_num) }}">{{ page_num }}</a></li>{% else %}<li class="page-item disabled"><span class="page-link">…</span></li>{% endif %}
      {% endfor %}
      <li class="page-item {% if not pagination.has_next %}disabled{% endif %}"><a class="page-link" href="{{ url_for('admin_logs', page=pagination.next_num) }}">Siguiente</a></li>
    </ul></nav>
  </div>
  {% endif %}
</div>
{% endblock %}
