{% macro events_tab(events, csrf_token, users, total_pages=0, current_page=1) %}
<div class="card shadow-sm">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h5 class="card-title mb-0">Event Log</h5>
            <div class="d-flex gap-2">
                <select id="eventTypeFilter" class="form-select form-select-sm">
                    <option value="">All Event Types</option>
                    <option value="user_login">User Login</option>
                    <option value="user_logout">User Logout</option>
                    <option value="user_create">User Create</option>
                    <option value="user_delete">User Delete</option>
                    <option value="user_update">User Update</option>
                    <option value="file_upload">File Upload</option>
                    <option value="file_delete">File Delete</option>
                    <option value="file_download">File Download</option>
                    <option value="file_preview">File Preview</option>
                    <option value="file_restore">File Restore</option>
                    <option value="file_move">File Move</option>
                    <option value="file_rename">File Rename</option>
                    <option value="file_star">File Star</option>
                    <option value="file_unstar">File Unstar</option>
                    <option value="file_delete_permanent">File Delete Permanent</option>
                    <option value="folder_create">Folder Create</option>
                    <option value="room_create">Room Create</option>
                    <option value="room_delete">Room Delete</option>
                    <option value="room_update">Room Update</option>
                    <option value="room_open">Room Open</option>
                    <option value="room_member_add">Room Member Add</option>
                    <option value="room_member_remove">Room Member Remove</option>
                    <option value="room_member_permissions_update">Room Member Permissions Update</option>
                    <option value="room_permission_update">Room Permission Update</option>
                    <option value="conversation_create">Conversation Create</option>
                    <option value="conversation_update">Conversation Update</option>
                    <option value="conversation_delete">Conversation Delete</option>
                    <option value="conversation_open">Conversation Open</option>
                    <option value="message_create">Message Create</option>
                    <option value="attachment_download">Attachment Download</option>
                </select>
                <select id="dateRangeFilter" class="form-select form-select-sm">
                    <option value="24h">Last 24 Hours</option>
                    <option value="7d">Last 7 Days</option>
                    <option value="30d">Last 30 Days</option>
                    <option value="all">All Time</option>
                </select>
                <select id="userFilter" class="form-select form-select-sm">
                    <option value="">All Users</option>
                    {% for user in users %}
                    <option value="{{ user.id }}">{{ user.username }} {{ user.last_name }}</option>
                    {% endfor %}
                </select>
                <button id="clearFilters" class="btn btn-secondary btn-sm">
                    <i class="fas fa-times me-1"></i>Clear Filters
                </button>
                <button id="downloadEvents" class="btn btn-primary btn-sm">
                    <i class="fas fa-download me-1"></i>Download CSV
                </button>
            </div>
        </div>
        
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Timestamp</th>
                        <th>Event Type</th>
                        <th>User</th>
                        <th>Details</th>
                        <th>IP Address</th>
                    </tr>
                </thead>
                <tbody id="eventsTableBody">
                    {% if events %}
                        {% for event in events %}
                        <tr>
                            <td>{{ event.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            <td>
                                {% if event.event_type == 'user_login' %}
                                    <span class="badge bg-info">User Login</span>
                                {% elif event.event_type == 'user_logout' %}
                                    <span class="badge bg-info">User Logout</span>
                                {% elif event.event_type == 'user_create' %}
                                    <span class="badge bg-success">User Create</span>
                                {% elif event.event_type == 'user_delete' %}
                                    <span class="badge bg-danger">User Delete</span>
                                {% elif event.event_type == 'user_update' %}
                                    <span class="badge bg-warning">User Update</span>
                                {% elif event.event_type == 'file_upload' %}
                                    <span class="badge bg-success">File Upload</span>
                                {% elif event.event_type == 'file_delete' %}
                                    <span class="badge bg-danger">File Delete</span>
                                {% elif event.event_type == 'file_download' %}
                                    <span class="badge bg-info">File Download</span>
                                {% elif event.event_type == 'file_preview' %}
                                    <span class="badge bg-info">File Preview</span>
                                {% elif event.event_type == 'file_restore' %}
                                    <span class="badge bg-warning">File Restore</span>
                                {% elif event.event_type == 'file_move' %}
                                    <span class="badge bg-warning">File Move</span>
                                {% elif event.event_type == 'file_rename' %}
                                    <span class="badge bg-warning">File Rename</span>
                                {% elif event.event_type == 'file_star' %}
                                    <span class="badge bg-warning">File Star</span>
                                {% elif event.event_type == 'file_unstar' %}
                                    <span class="badge bg-warning">File Unstar</span>
                                {% elif event.event_type == 'file_delete_permanent' %}
                                    <span class="badge bg-danger">File Delete Permanent</span>
                                {% elif event.event_type == 'folder_create' %}
                                    <span class="badge bg-success">Folder Create</span>
                                {% elif event.event_type == 'room_create' %}
                                    <span class="badge bg-success">Room Create</span>
                                {% elif event.event_type == 'room_delete' %}
                                    <span class="badge bg-danger">Room Delete</span>
                                {% elif event.event_type == 'room_update' %}
                                    <span class="badge bg-warning">Room Update</span>
                                {% elif event.event_type == 'room_open' %}
                                    <span class="badge bg-info">Room Open</span>
                                {% elif event.event_type == 'room_member_add' %}
                                    <span class="badge bg-success">Room Member Add</span>
                                {% elif event.event_type == 'room_member_remove' %}
                                    <span class="badge bg-danger">Room Member Remove</span>
                                {% elif event.event_type == 'room_member_permissions_update' %}
                                    <span class="badge bg-warning">Room Member Permissions Update</span>
                                {% elif event.event_type == 'room_permission_update' %}
                                    <span class="badge bg-warning">Room Permission Update</span>
                                {% elif event.event_type == 'conversation_create' %}
                                    <span class="badge bg-success">Conversation Create</span>
                                {% elif event.event_type == 'conversation_update' %}
                                    <span class="badge bg-warning">Conversation Update</span>
                                {% elif event.event_type == 'conversation_delete' %}
                                    <span class="badge bg-danger">Conversation Delete</span>
                                {% elif event.event_type == 'conversation_open' %}
                                    <span class="badge bg-info">Conversation Open</span>
                                {% elif event.event_type == 'message_create' %}
                                    <span class="badge bg-success">Message Create</span>
                                {% elif event.event_type == 'attachment_download' %}
                                    <span class="badge bg-info">Attachment Download</span>
                                {% else %}
                                    <span class="badge bg-secondary">{{ event.event_type }}</span>
                                {% endif %}
                            </td>
                            <td>{{ event.user.username }} {{ event.user.last_name if event.user else 'Unknown' }}</td>
                            <td>
                                <button class="btn btn-sm btn-outline-secondary" 
                                        data-bs-toggle="modal" 
                                        data-bs-target="#eventDetailsModal"
                                        data-event-id="{{ event.id }}">
                                    <i class="fas fa-info-circle"></i> View Details
                                </button>
                            </td>
                            <td>{{ event.ip_address or '-' }}</td>
                        </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="5" class="text-center">No events found</td>
                        </tr>
                    {% endif %}
                </tbody>
            </table>
        </div>
        
        <div class="d-flex justify-content-between align-items-center mt-3">
            <div>
                <button id="prevPage" class="btn btn-outline-primary btn-sm" 
                        style="border-color:var(--primary-color); color:var(--primary-color);" 
                        onmouseover="this.style.backgroundColor='var(--primary-color)'; this.style.color='white'" 
                        onmouseout="this.style.backgroundColor='transparent'; this.style.color='var(--primary-color)'"
                        {% if current_page == 1 %}disabled{% endif %}>
                    <i class="fas fa-chevron-left me-1"></i>Previous
                </button>
                <span class="mx-2">Page <span id="currentPage">{{ current_page }}</span> of <span id="totalPages">{{ total_pages }}</span></span>
                <button id="nextPage" class="btn btn-outline-primary btn-sm"
                        style="border-color:var(--primary-color); color:var(--primary-color);" 
                        onmouseover="this.style.backgroundColor='var(--primary-color)'; this.style.color='white'" 
                        onmouseout="this.style.backgroundColor='transparent'; this.style.color='var(--primary-color)'"
                        {% if current_page == total_pages %}disabled{% endif %}>
                    Next<i class="fas fa-chevron-right ms-1"></i>
                </button>
            </div>
        </div>
    </div>
</div>

<!-- Event Details Modal -->
<div class="modal fade" id="eventDetailsModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Event Details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <pre id="eventDetailsContent" class="bg-light p-3 rounded"></pre>
            </div>
        </div>
    </div>
</div>
{% endmacro %}

{% block content %}
{{ events_tab(events, csrf_token, users) }}
{% endblock %} 