{% extends "admin_frontend/base.html" %}
{% load crispy_forms_tags breadcrumbs %}
{% block title %}Audit Logs{% endblock %}
{% block content %}
<div class="flex items-center justify-between mb-4">
  <h1 class="text-2xl font-semibold">Audit Logs</h1>
</div>

{% render_breadcrumbs breadcrumbs %}
<div class="bg-white rounded shadow p-4 mb-4">
  <form method="get" class="space-y-3 flex gap-2">
    {{ filter.form|crispy }}
    <div class="flex items-center gap-2">
      <button class="px-3 py-2 bg-blue-600 text-white rounded">Filter</button>
      <a href="{% url 'recycle_core:audit_logs_list' %}" class="btn-outline">Reset</a>
      <a href="{% url 'recycle_core:audit_logs_export_csv' %}?{{ request.GET.urlencode }}" class="btn-outline">Export CSV</a>
    </div>
  </form>
  {% if filter.qs.count != page_obj.paginator.count %}
    <p class="text-sm text-gray-500 mt-2">Filtered results: {{ page_obj.paginator.count }} items</p>
  {% endif %}
</div>

<div class="bg-white rounded shadow overflow-hidden">
  <table class="min-w-full divide-y divide-gray-200">
    <thead class="bg-gray-50">
      <tr>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">When</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Org</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">User</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Action</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Object</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Metadata</th>
      </tr>
    </thead>
    <tbody class="divide-y divide-gray-200">
      {% for log in logs %}
      <tr>
        <td class="px-4 py-2">{{ log.created_at }}</td>
        <td class="px-4 py-2">{{ log.organization.code }}</td>
        <td class="px-4 py-2">{{ log.user.username|default:"-" }}</td>
        <td class="px-4 py-2">{{ log.action }}</td>
        <td class="px-4 py-2">{{ log.content_type.model }} #{{ log.object_id }}</td>
        <td class="px-4 py-2"><code class="text-xs">{{ log.metadata|json_script:"_" }}</code></td>
      </tr>
      {% empty %}
      <tr><td class="px-4 py-4 text-center text-gray-500" colspan="6">No audit logs.</td></tr>
      {% endfor %}
    </tbody>
  </table>
  {% include 'admin_frontend/_pagination.html' %}
</div>
{% endblock %}
