{% extends "layouts/base.html" %}
{% load i18n static %}

{% block title %}{% trans "Staff Activity Log" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .page-header-gradient {
        background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
        color: white;
        padding: 1.5rem 2rem;
        border-radius: 1rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
    }
    .section-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        transition: all 0.3s ease;
    }
    .section-card:hover {
        border-color: #005696;
        box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
    }
    .section-header {
        padding: 1rem 1.5rem;
        border-bottom: 2px solid #e2e8f0;
        background: linear-gradient(to right, #f8fafc, #f1f5f9);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }
    .section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }
</style>
{% endblock %}

{% block content %}
<div class="mb-6">
    <a href="javascript:history.back()" class="inline-flex items-center gap-2 text-slate hover:text-navy transition text-sm font-semibold">
        <i data-lucide="arrow-left" class="w-4 h-4"></i> {% trans "Back" %}
    </a>
</div>

<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <i data-lucide="activity" class="w-7 h-7"></i>
                {% trans "Staff Activity Log" %}
            </h1>
            <p class="text-sm opacity-90 mt-1">{% trans "Track and monitor staff actions across the system" %}</p>
        </div>
        <div class="bg-white/20 rounded-xl px-5 py-3 text-center">
            <p class="text-3xl font-black">{{ page_obj.paginator.count }}</p>
            <p class="text-xs font-semibold opacity-80">{% trans "Total Events" %}</p>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-slate-100">
            <i data-lucide="filter" class="w-5 h-5 text-slate-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Filters" %}</h3>
    </div>
    <div class="p-4">
        <form method="get" class="flex flex-wrap gap-3 items-end">
            <div class="flex-1 min-w-[200px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Search" %}</label>
                <input type="text" name="search" value="{{ filters.search|default:'' }}" placeholder="{% trans 'Search descriptions...' %}" class="w-full px-3 py-2.5 border-2 border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none">
            </div>
            <div class="min-w-[160px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Activity Type" %}</label>
                <select name="activity_type" class="w-full px-3 py-2.5 border-2 border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none">
                    <option value="">{% trans "All Types" %}</option>
                    {% for val, label in activity_types %}
                    <option value="{{ val }}" {% if filters.activity_type == val %}selected{% endif %}>{{ label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="min-w-[140px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "Module" %}</label>
                <select name="module" class="w-full px-3 py-2.5 border-2 border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none">
                    <option value="">{% trans "All Modules" %}</option>
                    {% for m in modules %}
                    <option value="{{ m }}" {% if filters.module == m %}selected{% endif %}>{{ m|title }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="min-w-[140px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "From" %}</label>
                <input type="date" name="date_from" value="{{ filters.date_from|default:'' }}" class="w-full px-3 py-2.5 border-2 border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none">
            </div>
            <div class="min-w-[140px]">
                <label class="block text-xs font-bold text-slate uppercase tracking-wider mb-1.5">{% trans "To" %}</label>
                <input type="date" name="date_to" value="{{ filters.date_to|default:'' }}" class="w-full px-3 py-2.5 border-2 border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none">
            </div>
            <button type="submit" class="px-5 py-2.5 bg-navy text-white rounded-xl text-sm font-bold hover:bg-blue transition flex items-center gap-2">
                <i data-lucide="search" class="w-4 h-4"></i> {% trans "Search" %}
            </button>
            <a href="{% url 'accounts:staff-activity-log' %}" class="px-5 py-2.5 border-2 border-slate-200 rounded-xl text-sm font-bold text-slate hover:bg-slate-50 transition">{% trans "Clear" %}</a>
        </form>
    </div>
</div>

<!-- Activity List -->
{% if activities %}
<div class="section-card">
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead>
                <tr class="bg-slate-50 border-b-2 border-slate-200">
                    <th class="px-5 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Timestamp" %}</th>
                    <th class="px-5 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "User" %}</th>
                    <th class="px-5 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Activity" %}</th>
                    <th class="px-5 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Module" %}</th>
                    <th class="px-5 py-3 text-left text-xs font-bold text-slate uppercase tracking-wider">{% trans "Description" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-slate-100">
                {% for activity in activities %}
                <tr class="hover:bg-slate-50 transition">
                    <td class="px-5 py-4">
                        <div class="text-sm font-semibold text-navy">{{ activity.created_at|date:"Y-m-d" }}</div>
                        <div class="text-xs text-slate">{{ activity.created_at|time:"H:i:s" }}</div>
                    </td>
                    <td class="px-5 py-4">
                        {% if activity.user %}
                        <div class="text-sm font-semibold text-navy">{{ activity.user.get_full_name }}</div>
                        <div class="text-xs text-slate">{{ activity.user.email }}</div>
                        {% else %}
                        <span class="text-xs text-slate">{% trans "System" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        <span class="inline-flex items-center px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if activity.activity_type == 'login' or activity.activity_type == 'logout' %}bg-slate-100 text-slate-700
                            {% elif activity.activity_type == 'create' %}bg-green-100 text-green-700
                            {% elif activity.activity_type == 'update' %}bg-blue-100 text-blue-700
                            {% elif activity.activity_type == 'delete' %}bg-red-100 text-red-700
                            {% elif activity.activity_type == 'send' or activity.activity_type == 'approve' %}bg-emerald-100 text-emerald-700
                            {% elif activity.activity_type == 'reject' or activity.activity_type == 'reopen' %}bg-orange-100 text-orange-700
                            {% elif activity.activity_type == 'resolve' %}bg-teal-100 text-teal-700
                            {% elif activity.activity_type == 'transfer' or activity.activity_type == 'assign' %}bg-purple-100 text-purple-700
                            {% elif activity.activity_type == 'export' %}bg-indigo-100 text-indigo-700
                            {% elif activity.activity_type == 'analyze' %}bg-violet-100 text-violet-700
                            {% else %}bg-slate-100 text-slate-700{% endif %}">
                            {{ activity.get_activity_type_display }}
                        </span>
                    </td>
                    <td class="px-5 py-4">
                        {% if activity.module %}
                        <span class="text-xs font-bold text-slate uppercase">{{ activity.module }}</span>
                        {% else %}
                        <span class="text-xs text-slate">-</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        <div class="max-w-sm truncate text-sm text-slate-700" title="{{ activity.description }}">
                            {{ activity.description|default:"-" }}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>

<!-- Pagination -->
{% if page_obj.has_other_pages %}
<div class="flex items-center justify-center gap-2 mt-6">
    {% if page_obj.has_previous %}
    <a href="?page={{ page_obj.previous_page_number }}&search={{ filters.search }}&activity_type={{ filters.activity_type }}&module={{ filters.module }}&date_from={{ filters.date_from }}&date_to={{ filters.date_to }}" class="px-3 py-2 border-2 border-slate-200 rounded-xl text-sm font-bold text-slate hover:bg-slate-50 transition">
        <i data-lucide="chevron-left" class="w-4 h-4"></i>
    </a>
    {% endif %}
    <span class="px-4 py-2 text-sm font-bold text-slate">{% trans "Page" %} {{ page_obj.number }} {% trans "of" %} {{ page_obj.paginator.num_pages }}</span>
    {% if page_obj.has_next %}
    <a href="?page={{ page_obj.next_page_number }}&search={{ filters.search }}&activity_type={{ filters.activity_type }}&module={{ filters.module }}&date_from={{ filters.date_from }}&date_to={{ filters.date_to }}" class="px-3 py-2 border-2 border-slate-200 rounded-xl text-sm font-bold text-slate hover:bg-slate-50 transition">
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
    </a>
    {% endif %}
</div>
{% endif %}

{% else %}
<div class="section-card p-12 text-center">
    <div class="section-icon bg-slate-100 mx-auto mb-4" style="width:64px;height:64px;border-radius:1rem;">
        <i data-lucide="inbox" class="w-8 h-8 text-slate-400"></i>
    </div>
    <h3 class="text-lg font-bold text-navy mb-2">{% trans "No activity found" %}</h3>
    <p class="text-slate text-sm">{% trans "Try adjusting your filters." %}</p>
</div>
{% endif %}
{% endblock %}
