{% extends 'base.html' %}
{% load static %}

{% block title %}Access Logs{% endblock %}

{% block content %}
<div class="space-y-6">
    <!-- Header -->
    <div class="bg-white shadow rounded-lg">
        <div class="px-4 py-5 sm:p-6">
            <div class="flex justify-between items-center">
                <div>
                    <h3 class="text-lg leading-6 font-medium text-gray-900">
                        <i class="fas fa-history text-primary mr-2"></i>
                        Access Logs
                    </h3>
                    <p class="mt-1 max-w-2xl text-sm text-gray-500">
                        View detailed audit trail of user activities and system access
                    </p>
                </div>
                <a href="{% url 'users:admin_list' %}" class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md shadow-sm text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                    <span>←</span>
                    Back to Staff Management
                </a>
            </div>
        </div>
    </div>

    <!-- Stats -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
        <div class="bg-white overflow-hidden shadow rounded-lg">
            <div class="p-5">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <span>📋</span>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 truncate">Total Logs</dt>
                            <dd class="text-lg font-medium text-gray-900">{{ total_logs }}</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
        <div class="bg-white overflow-hidden shadow rounded-lg">
            <div class="p-5">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <span>👥</span>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 truncate">Active Users</dt>
                            <dd class="text-lg font-medium text-gray-900">{{ active_users }}</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
        <div class="bg-white overflow-hidden shadow rounded-lg">
            <div class="p-5">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <span>⚠️</span>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 truncate">Access Denied</dt>
                            <dd class="text-lg font-medium text-gray-900">{{ access_denied }}</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
        <div class="bg-white overflow-hidden shadow rounded-lg">
            <div class="p-5">
                <div class="flex items-center">
                    <div class="flex-shrink-0">
                        <span>⏰</span>
                    </div>
                    <div class="ml-5 w-0 flex-1">
                        <dl>
                            <dt class="text-sm font-medium text-gray-500 truncate">Today's Activity</dt>
                            <dd class="text-lg font-medium text-gray-900">{{ today_activity }}</dd>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Search and Filter -->
    <div class="bg-white shadow rounded-lg">
        <div class="px-4 py-5 sm:p-6">
            <form method="get" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label for="user_filter" class="block text-sm font-medium text-gray-700 mb-1">User</label>
                    <select name="user" id="user_filter" class="w-full rounded-md border-gray-300 focus:border-primary focus:ring-primary">
                        <option value="">All Users</option>
                        {% for user in users %}
                            <option value="{{ user.id }}" {% if selected_user == user.id %}selected{% endif %}>
                                {{ user.get_full_name }} ({{ user.get_role_display }})
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div>
                    <label for="action_filter" class="block text-sm font-medium text-gray-700 mb-1">Action</label>
                    <select name="action" id="action_filter" class="w-full rounded-md border-gray-300 focus:border-primary focus:ring-primary">
                        <option value="">All Actions</option>
                        <option value="view" {% if selected_action == 'view' %}selected{% endif %}>View</option>
                        <option value="create" {% if selected_action == 'create' %}selected{% endif %}>Create</option>
                        <option value="edit" {% if selected_action == 'edit' %}selected{% endif %}>Edit</option>
                        <option value="delete" {% if selected_action == 'delete' %}selected{% endif %}>Delete</option>
                        <option value="access_denied" {% if selected_action == 'access_denied' %}selected{% endif %}>Access Denied</option>
                    </select>
                </div>
                <div>
                    <label for="module_filter" class="block text-sm font-medium text-gray-700 mb-1">Module</label>
                    <select name="module" id="module_filter" class="w-full rounded-md border-gray-300 focus:border-primary focus:ring-primary">
                        <option value="">All Modules</option>
                        <option value="users" {% if selected_module == 'users' %}selected{% endif %}>User Management</option>
                        <option value="loans" {% if selected_module == 'loans' %}selected{% endif %}>Loan Management</option>
                        <option value="reports" {% if selected_module == 'reports' %}selected{% endif %}>Reports</option>
                        <option value="settings" {% if selected_module == 'settings' %}selected{% endif %}>Settings</option>
                    </select>
                </div>
                <div>
                    <label for="date_filter" class="block text-sm font-medium text-gray-700 mb-1">Date Range</label>
                    <select name="date_range" id="date_filter" class="w-full rounded-md border-gray-300 focus:border-primary focus:ring-primary">
                        <option value="all" {% if date_range == 'all' %}selected{% endif %}>All Time</option>
                        <option value="today" {% if date_range == 'today' %}selected{% endif %}>Today</option>
                        <option value="week" {% if date_range == 'week' %}selected{% endif %}>This Week</option>
                        <option value="month" {% if date_range == 'month' %}selected{% endif %}>This Month</option>
                    </select>
                </div>
                <div class="md:col-span-4 flex justify-end space-x-3">
                    <button type="submit" class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-primary hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                        <span>🔍</span>
                        Filter Logs
                    </button>
                    <a href="{% url 'users:access_logs' %}" class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md shadow-sm text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                        <span>✕</span>
                        Clear
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Logs Table -->
    <div class="bg-white shadow overflow-hidden sm:rounded-md">
        <div class="px-4 py-5 sm:px-6">
            <h3 class="text-lg leading-6 font-medium text-gray-900">Access Log Entries</h3>
        </div>
        
        {% if logs %}
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">User</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Action</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Module</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Description</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">IP Address</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Timestamp</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        {% for log in logs %}
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <div class="flex-shrink-0 h-8 w-8">
                                        {% if log.user.selfie %}
                                            <img class="h-8 w-8 rounded-full" src="{{ log.user.selfie.url }}" alt="">
                                        {% else %}
                                            <div class="h-8 w-8 rounded-full bg-gray-300 flex items-center justify-center">
                                                <span>👤</span>
                                            </div>
                                        {% endif %}
                                    </div>
                                    <div class="ml-4">
                                        <div class="text-sm font-medium text-gray-900">{{ log.user.get_full_name }}</div>
                                        <div class="text-sm text-gray-500">{{ log.user.get_role_display }}</div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                {% if log.action == 'access_denied' %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                        <i class="fas fa-ban mr-1"></i>Access Denied
                                    </span>
                                {% elif log.action == 'view' %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                        <i class="fas fa-eye mr-1"></i>View
                                    </span>
                                {% elif log.action == 'create' %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                        <span>+</span>Create
                                    </span>
                                {% elif log.action == 'edit' %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
                                        <span>✏️</span>Edit
                                    </span>
                                {% elif log.action == 'delete' %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                        <span>🗑️</span>Delete
                                    </span>
                                {% else %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
                                        {{ log.action|title }}
                                    </span>
                                {% endif %}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                <span class="capitalize">{{ log.module|title }}</span>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-900">
                                <div class="max-w-xs truncate" title="{{ log.description }}">
                                    {{ log.description }}
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                {{ log.ip_address|default:"-" }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                {{ log.accessed_at|date:"M d, Y H:i:s" }}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        {% else %}
            <div class="text-center py-12">
                <i class="fas fa-history text-gray-400 text-4xl mb-4"></i>
                <h3 class="text-lg font-medium text-gray-900 mb-2">No access logs found</h3>
                <p class="text-gray-500">No activity has been logged yet.</p>
            </div>
        {% endif %}
    </div>

    <!-- Pagination -->
    {% if logs.has_other_pages %}
    <div class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
        <div class="flex-1 flex justify-between sm:hidden">
            {% if logs.has_previous %}
                <a href="?page={{ logs.previous_page_number }}{% if selected_user %}&user={{ selected_user }}{% endif %}{% if selected_action %}&action={{ selected_action }}{% endif %}{% if selected_module %}&module={{ selected_module }}{% endif %}{% if date_range != 'all' %}&date_range={{ date_range }}{% endif %}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                    Previous
                </a>
            {% endif %}
            {% if logs.has_next %}
                <a href="?page={{ logs.next_page_number }}{% if selected_user %}&user={{ selected_user }}{% endif %}{% if selected_action %}&action={{ selected_action }}{% endif %}{% if selected_module %}&module={{ selected_module }}{% endif %}{% if date_range != 'all' %}&date_range={{ date_range }}{% endif %}" class="ml-3 relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                    Next
                </a>
            {% endif %}
        </div>
        <div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
            <div>
                <p class="text-sm text-gray-700">
                    Showing <span class="font-medium">{{ logs.start_index }}</span> to <span class="font-medium">{{ logs.end_index }}</span> of <span class="font-medium">{{ logs.paginator.count }}</span> results
                </p>
            </div>
            <div>
                <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
                    {% if logs.has_previous %}
                        <a href="?page=1{% if selected_user %}&user={{ selected_user }}{% endif %}{% if selected_action %}&action={{ selected_action }}{% endif %}{% if selected_module %}&module={{ selected_module }}{% endif %}{% if date_range != 'all' %}&date_range={{ date_range }}{% endif %}" class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <i class="fas fa-angle-double-left"></i>
                        </a>
                        <a href="?page={{ logs.previous_page_number }}{% if selected_user %}&user={{ selected_user }}{% endif %}{% if selected_action %}&action={{ selected_action }}{% endif %}{% if selected_module %}&module={{ selected_module }}{% endif %}{% if date_range != 'all' %}&date_range={{ date_range }}{% endif %}" class="relative inline-flex items-center px-2 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <i class="fas fa-angle-left"></i>
                        </a>
                    {% endif %}

                    {% for num in logs.paginator.page_range %}
                        {% if logs.number == num %}
                            <span class="relative inline-flex items-center px-4 py-2 border border-primary bg-primary text-sm font-medium text-white">{{ num }}</span>
                        {% elif num > logs.number|add:'-3' and num < logs.number|add:'3' %}
                            <a href="?page={{ num }}{% if selected_user %}&user={{ selected_user }}{% endif %}{% if selected_action %}&action={{ selected_action }}{% endif %}{% if selected_module %}&module={{ selected_module }}{% endif %}{% if date_range != 'all' %}&date_range={{ date_range }}{% endif %}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50">{{ num }}</a>
                        {% endif %}
                    {% endfor %}

                    {% if logs.has_next %}
                        <a href="?page={{ logs.next_page_number }}{% if selected_user %}&user={{ selected_user }}{% endif %}{% if selected_action %}&action={{ selected_action }}{% endif %}{% if selected_module %}&module={{ selected_module }}{% endif %}{% if date_range != 'all' %}&date_range={{ date_range }}{% endif %}" class="relative inline-flex items-center px-2 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <i class="fas fa-angle-right"></i>
                        </a>
                        <a href="?page={{ logs.paginator.num_pages }}{% if selected_user %}&user={{ selected_user }}{% endif %}{% if selected_action %}&action={{ selected_action }}{% endif %}{% if selected_module %}&module={{ selected_module }}{% endif %}{% if date_range != 'all' %}&date_range={{ date_range }}{% endif %}" class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <i class="fas fa-angle-double-right"></i>
                        </a>
                    {% endif %}
                </nav>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %} 