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

{% block title %}Audit Logs - Administration - van Hees-Theyssen-Vogel'sche Stiftung{% endblock %}

{% block content %}

    <div class="row">
        <div class="col-12">
            <div class="d-sm-flex align-items-center justify-content-between mb-4">
                <h1 class="h3 mb-0 text-gray-800">
                    <i class="fas fa-history me-2"></i>Audit Logs
                </h1>
                <a href="{% url 'stiftung:administration' %}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-1"></i>Zurück zur Administration
                </a>
            </div>
        </div>
    </div>

    <!-- Filter -->
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-filter me-2"></i>Filter
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-2">
                    <label class="form-label">Benutzer</label>
                    <input type="text" class="form-control" name="user" value="{{ user_filter }}" placeholder="Benutzername...">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Aktion</label>
                    <select class="form-select" name="action">
                        <option value="">Alle Aktionen</option>
                        {% for value, display in action_choices %}
                        <option value="{{ value }}" {% if action_filter == value %}selected{% endif %}>{{ display }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Entitätstyp</label>
                    <select class="form-select" name="entity_type">
                        <option value="">Alle Typen</option>
                        {% for value, display in entity_choices %}
                        <option value="{{ value }}" {% if entity_filter == value %}selected{% endif %}>{{ display }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Von Datum</label>
                    <input type="date" class="form-control" name="date_from" value="{{ date_from }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Bis Datum</label>
                    <input type="date" class="form-control" name="date_to" value="{{ date_to }}">
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <div class="btn-group w-100">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search me-1"></i>Filtern
                        </button>
                        <a href="{% url 'stiftung:audit_log_list' %}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-1"></i>Reset
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Audit Logs -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">
                <i class="fas fa-list me-2"></i>Audit Log Einträge
                <span class="badge bg-primary ms-2">{{ page_obj.paginator.count|intcomma }}</span>
            </h6>
        </div>
        <div class="card-body">
            {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Zeitpunkt</th>
                                <th>Benutzer</th>
                                <th>Aktion</th>
                                <th>Entität</th>
                                <th>Beschreibung</th>
                                <th>IP</th>
                                <th>Details</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for log in page_obj %}
                            <tr>
                                <td>
                                    <small>{{ log.timestamp|date:"d.m.Y" }}</small><br>
                                    <small class="text-muted">{{ log.timestamp|date:"H:i:s" }}</small>
                                </td>
                                <td>
                                    <span class="badge bg-secondary">{{ log.username }}</span>
                                </td>
                                <td>
                                    <span class="badge bg-{% if log.action == 'create' %}success{% elif log.action == 'update' %}warning{% elif log.action == 'delete' %}danger{% elif log.action == 'login' %}info{% elif log.action == 'logout' %}secondary{% elif log.action == 'backup' %}primary{% elif log.action == 'restore' %}warning{% else %}light{% endif %}">
                                        {{ log.get_action_display }}
                                    </span>
                                </td>
                                <td>
                                    <div>
                                        <span class="badge bg-light text-dark">{{ log.get_entity_type_display }}</span>
                                        {% if log.entity_name %}
                                        <br><small class="text-muted">{{ log.entity_name|truncatechars:30 }}</small>
                                        {% endif %}
                                    </div>
                                </td>
                                <td>
                                    <small>{{ log.description|truncatechars:60 }}</small>
                                </td>
                                <td>
                                    {% if log.ip_address %}
                                    <small class="text-muted">{{ log.ip_address }}</small>
                                    {% else %}
                                    <small class="text-muted">-</small>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if log.changes %}
                                    <button class="btn btn-outline-info btn-sm" onclick="showDetails('{{ log.id }}')">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                    {% else %}
                                    <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                {% if page_obj.has_other_pages %}
                <nav aria-label="Audit Log Pagination">
                    <ul class="pagination justify-content-center">
                        {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.previous_page_number }}">Vorherige</a>
                        </li>
                        {% endif %}
                        
                        {% for num in page_obj.paginator.page_range %}
                        {% if page_obj.number == num %}
                        <li class="page-item active">
                            <span class="page-link">{{ num }}</span>
                        </li>
                        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ num }}">{{ num }}</a>
                        </li>
                        {% endif %}
                        {% endfor %}
                        
                        {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.next_page_number }}">Nächste</a>
                        </li>
                        {% endif %}
                    </ul>
                </nav>
                {% endif %}
            {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-history fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">Keine Audit Logs gefunden</h5>
                    <p class="text-muted">Passen Sie die Filter an oder überprüfen Sie die Suchkriterien.</p>
                </div>
            {% endif %}
        </div>
    </div>


<!-- Details Modal -->
<div class="modal fade" id="detailsModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Änderungsdetails</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div id="detailsContent">
                    <div class="text-center">
                        <div class="spinner-border" role="status">
                            <span class="visually-hidden">Lädt...</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


<script>
function showDetails(logId) {
    const modal = new bootstrap.Modal(document.getElementById('detailsModal'));
    const content = document.getElementById('detailsContent');
    
    // Show loading spinner
    content.innerHTML = `
        <div class="text-center">
            <div class="spinner-border" role="status">
                <span class="visually-hidden">Lädt...</span>
            </div>
        </div>
    `;
    
    modal.show();
    
    // Fetch details - for now just show that the feature is available
    // In a full implementation, you'd fetch the log details via AJAX
    setTimeout(() => {
        content.innerHTML = `
            <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i>
                Detailansicht für Log-ID: ${logId}<br>
                <small>Diese Funktion kann erweitert werden, um detaillierte Änderungsinformationen anzuzeigen.</small>
            </div>
        `;
    }, 500);
}
</script>
{% endblock %}
