{% extends "base.html" %}

{% block title %}Audit Logs | BizTrack{% endblock %}

{% block content %}
<div class="space-y-4">
  <div class="flex flex-wrap items-center justify-between gap-3">
    <div>
      <h1 class="text-2xl font-bold">Audit Logs</h1>
      <p class="text-sm opacity-70">Track who changed what and when.</p>
    </div>
  </div>

  <form method="get" class="card bg-base-100 shadow-sm border border-base-200">
    <div class="card-body grid grid-cols-1 md:grid-cols-4 gap-3">
      <input
        type="text"
        name="q"
        value="{{ query }}"
        placeholder="Search actor / object / id"
        class="input input-bordered w-full"
      />
      <select name="action" class="select select-bordered w-full">
        <option value="">All actions</option>
        {% for value, label in actions %}
          <option value="{{ value }}" {% if current_action == value %}selected{% endif %}>
            {{ label }}
          </option>
        {% endfor %}
      </select>
      <input
        type="text"
        name="model"
        value="{{ current_model }}"
        placeholder="Model name (e.g. sale)"
        class="input input-bordered w-full"
      />
      <button type="submit" class="btn btn-primary">Filter</button>
    </div>
  </form>

  <div class="card bg-base-100 border border-base-200 shadow-sm">
    <div class="overflow-x-auto">
      <table class="table table-zebra">
        <thead>
          <tr>
            <th>When</th>
            <th>Actor</th>
            <th>Action</th>
            <th>Object</th>
            <th>Changes</th>
          </tr>
        </thead>
        <tbody>
          {% for log in logs %}
          <tr>
            <td class="whitespace-nowrap text-sm">{{ log.created_at|date:"Y-m-d H:i:s" }}</td>
            <td>{{ log.actor.username|default:"System" }}</td>
            <td><span class="badge badge-outline">{{ log.get_action_display }}</span></td>
            <td>
              <div class="font-medium">{{ log.app_label }}.{{ log.model_name }}</div>
              <div class="text-xs opacity-70">#{{ log.object_id }} — {{ log.object_repr }}</div>
            </td>
            <td class="text-xs">
              {% if log.changes %}
                <pre class="whitespace-pre-wrap">{{ log.changes }}</pre>
              {% else %}
                <span class="opacity-70">No field-level diff</span>
              {% endif %}
            </td>
          </tr>
          {% empty %}
          <tr>
            <td colspan="5" class="text-center opacity-70 py-8">No audit logs found.</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>

  {% include "partials/pagination.html" with page_obj=logs %}
</div>
{% endblock %}
