{% extends "layout.html" %}

{% block content %}
<div class="users-admin-container">
    <!-- Header Section -->
    <div class="page-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="page-title">
                    <span class="page-icon">👤</span>
                    User Management
                </h1>
                <p class="page-subtitle">Manage user accounts and permissions across your organization</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('users.create_user') }}" class="btn btn-primary">
                    <span class="icon">+</span>
                    Create New User
                </a>
            </div>
        </div>
    </div>

    <!-- User Statistics -->
    {% if users %}
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-value">{{ users|length }}</div>
            <div class="stat-label">Total Users</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ users|selectattr('is_blocked', 'equalto', false)|list|length }}</div>
            <div class="stat-label">Active Users</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ users|selectattr('role.name', 'equalto', 'ADMIN')|list|length + users|selectattr('role.name', 'equalto', 'SYSTEM_ADMIN')|list|length }}</div>
            <div class="stat-label">Administrators</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ users|selectattr('team_id', 'none')|list|length }}</div>
            <div class="stat-label">Unassigned</div>
        </div>
    </div>
    {% endif %}

    <!-- Main Content -->
    <div class="users-content">
        {% if users %}
            <!-- View Controls -->
            <div class="view-controls">
                <div class="search-container">
                    <span class="search-icon">🔍</span>
                    <input type="text" 
                           class="search-input" 
                           id="userSearch" 
                           placeholder="Search users by name, email, or role...">
                </div>
                <div class="view-toggle">
                    <button class="toggle-btn active" data-view="grid" title="Grid View">
                        <span class="icon">⊞</span>
                    </button>
                    <button class="toggle-btn" data-view="list" title="List View">
                        <span class="icon">☰</span>
                    </button>
                </div>
            </div>

            <!-- Grid View -->
            <div class="view-container grid-view active" id="gridView">
                <div class="users-grid" id="usersGrid">
                    {% for user in users %}
                        <div class="user-card" 
                             data-username="{{ user.username.lower() }}" 
                             data-email="{{ user.email.lower() if user.email else '' }}"
                             data-role="{{ user.role.value.lower() if user.role else 'team member' }}">
                            <div class="user-header">
                                <img src="{{ user.get_avatar_url(80) }}" alt="{{ user.username }}" class="user-avatar">
                                <div class="user-status">
                                    <span class="status-badge {% if user.is_blocked %}status-blocked{% else %}status-active{% endif %}">
                                        {% if user.is_blocked %}Blocked{% else %}Active{% endif %}
                                    </span>
                                </div>
                            </div>
                            
                            <div class="user-body">
                                <h3 class="user-name">{{ user.username }}</h3>
                                <p class="user-email">{{ user.email if user.email else 'No email' }}</p>
                                
                                <div class="user-info">
                                    <div class="info-row">
                                        <span class="info-label">Role:</span>
                                        <span class="role-badge role-{{ user.role.name.lower() if user.role else 'team_member' }}">
                                            {{ user.role.value if user.role else 'Team Member' }}
                                        </span>
                                    </div>
                                    <div class="info-row">
                                        <span class="info-label">Team:</span>
                                        <span class="info-value">{{ user.team.name if user.team else 'Unassigned' }}</span>
                                    </div>
                                    <div class="info-row">
                                        <span class="info-label">Joined:</span>
                                        <span class="info-value">{{ user.created_at.strftime('%b %d, %Y') }}</span>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="user-actions">
                                <a href="{{ url_for('users.edit_user', user_id=user.id) }}" class="btn btn-edit" title="Edit User">
                                    <span class="icon">✏️</span>
                                    Edit
                                </a>
                                {% if user.id != g.user.id %}
                                    <form method="POST" action="{{ url_for('users.toggle_user_status', user_id=user.id) }}" class="status-form">
                                        {% if user.is_blocked %}
                                            <button type="submit" class="btn btn-unblock" title="Unblock User">
                                                <span class="icon">🔓</span>
                                            </button>
                                        {% else %}
                                            <button type="submit" class="btn btn-block" title="Block User">
                                                <span class="icon">🔒</span>
                                            </button>
                                        {% endif %}
                                    </form>
                                    <button class="btn btn-delete" onclick="confirmDelete({{ user.id }}, '{{ user.username }}')" title="Delete User">
                                        <span class="icon">🗑️</span>
                                    </button>
                                {% endif %}
                            </div>
                        </div>
                    {% endfor %}
                </div>
            </div>

            <!-- List View -->
            <div class="view-container list-view" id="listView">
                <div class="table-container">
                    <table class="users-table">
                        <thead>
                            <tr>
                                <th>User</th>
                                <th>Email</th>
                                <th>Role</th>
                                <th>Team</th>
                                <th>Status</th>
                                <th>Joined</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody id="usersTableBody">
                            {% for user in users %}
                                <tr class="user-row"
                                    data-username="{{ user.username.lower() }}" 
                                    data-email="{{ user.email.lower() if user.email else '' }}"
                                    data-role="{{ user.role.value.lower() if user.role else 'team member' }}">
                                    <td>
                                        <div class="user-cell">
                                            <img src="{{ user.get_avatar_url(40) }}" alt="{{ user.username }}" class="table-avatar">
                                            <span class="table-username">{{ user.username }}</span>
                                        </div>
                                    </td>
                                    <td>{{ user.email if user.email else '-' }}</td>
                                    <td>
                                        <span class="role-badge role-{{ user.role.name.lower() if user.role else 'team_member' }}">
                                            {{ user.role.value if user.role else 'Team Member' }}
                                        </span>
                                    </td>
                                    <td>{{ user.team.name if user.team else 'Unassigned' }}</td>
                                    <td>
                                        <span class="status-badge {% if user.is_blocked %}status-blocked{% else %}status-active{% endif %}">
                                            {% if user.is_blocked %}Blocked{% else %}Active{% endif %}
                                        </span>
                                    </td>
                                    <td>{{ user.created_at.strftime('%Y-%m-%d') }}</td>
                                    <td>
                                        <div class="table-actions">
                                            <a href="{{ url_for('users.edit_user', user_id=user.id) }}" class="btn-action btn-edit" title="Edit">
                                                <span class="icon">✏️</span>
                                            </a>
                                            {% if user.id != g.user.id %}
                                                <form method="POST" action="{{ url_for('users.toggle_user_status', user_id=user.id) }}" class="inline-form">
                                                    {% if user.is_blocked %}
                                                        <button type="submit" class="btn-action btn-unblock" title="Unblock">
                                                            <span class="icon">🔓</span>
                                                        </button>
                                                    {% else %}
                                                        <button type="submit" class="btn-action btn-block" title="Block">
                                                            <span class="icon">🔒</span>
                                                        </button>
                                                    {% endif %}
                                                </form>
                                                <button class="btn-action btn-delete" onclick="confirmDelete({{ user.id }}, '{{ user.username }}')" title="Delete">
                                                    <span class="icon">🗑️</span>
                                                </button>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
            
            <!-- No Results Message -->
            <div class="no-results" id="noResults" style="display: none;">
                <div class="empty-icon">🔍</div>
                <p class="empty-message">No users found matching your search</p>
                <p class="empty-hint">Try searching with different keywords</p>
            </div>
        {% else %}
            <!-- Empty State -->
            <div class="empty-state">
                <div class="empty-icon">👤</div>
                <h2 class="empty-title">No Users Yet</h2>
                <p class="empty-message">Create your first user to get started</p>
                <a href="{{ url_for('users.create_user') }}" class="btn btn-primary btn-lg">
                    <span class="icon">+</span>
                    Create First User
                </a>
            </div>
        {% endif %}
    </div>
    
    <!-- Delete Confirmation Modal -->
    <div id="delete-modal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2 class="modal-title">Confirm Deletion</h2>
                <span class="modal-close">&times;</span>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete user <strong id="delete-username"></strong>?</p>
                <p class="warning-text">⚠️ This action cannot be undone.</p>
            </div>
            <div class="modal-footer">
                <form id="delete-form" method="POST">
                    <button type="button" id="cancel-delete" class="btn btn-secondary">Cancel</button>
                    <button type="submit" class="btn btn-danger">
                        <span class="icon">🗑️</span>
                        Delete User
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
/* Container */
.users-admin-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 2rem;
}

.page-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.page-icon {
    font-size: 2.5rem;
    display: inline-block;
    animation: float 3s ease-in-out infinite;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.page-subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
    margin: 0.5rem 0 0 0;
}

/* Stats Section */
.stats-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: white;
    padding: 1.5rem;
    border-radius: 12px;
    text-align: center;
    border: 1px solid #e5e7eb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    color: #667eea;
}

.stat-label {
    font-size: 0.9rem;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

/* View Controls */
.view-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin-bottom: 2rem;
}

.search-container {
    position: relative;
    flex: 1;
    max-width: 500px;
}

.search-icon {
    position: absolute;
    left: 1rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.25rem;
    opacity: 0.6;
}

.search-input {
    width: 100%;
    padding: 1rem 1rem 1rem 3rem;
    border: 2px solid #e5e7eb;
    border-radius: 12px;
    font-size: 1rem;
    transition: all 0.3s ease;
    background: white;
}

.search-input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* View Toggle */
.view-toggle {
    display: flex;
    gap: 0.5rem;
    background: #f3f4f6;
    padding: 0.25rem;
    border-radius: 8px;
}

.toggle-btn {
    padding: 0.5rem 1rem;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.25rem;
    color: #6b7280;
    transition: all 0.2s ease;
}

.toggle-btn:hover {
    background: rgba(102, 126, 234, 0.1);
    color: #667eea;
}

.toggle-btn.active {
    background: white;
    color: #667eea;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* View Containers */
.view-container {
    display: none;
}

.view-container.active {
    display: block;
}

/* Grid View */
.users-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.user-card {
    background: white;
    border-radius: 12px;
    border: 1px solid #e5e7eb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
    overflow: hidden;
}

.user-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.user-header {
    background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
    padding: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.user-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: 4px solid white;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.user-body {
    padding: 1.5rem;
}

.user-name {
    font-size: 1.25rem;
    font-weight: 700;
    color: #1f2937;
    margin: 0 0 0.25rem 0;
}

.user-email {
    color: #6b7280;
    font-size: 0.875rem;
    margin-bottom: 1rem;
}

.user-info {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.info-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.info-label {
    color: #6b7280;
    font-size: 0.875rem;
    font-weight: 600;
}

.info-value {
    color: #1f2937;
    font-size: 0.875rem;
}

.user-actions {
    padding: 1rem 1.5rem;
    background: #f8f9fa;
    border-top: 1px solid #e5e7eb;
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.status-form,
.inline-form {
    margin: 0;
}

/* List View */
.table-container {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    overflow: hidden;
}

.users-table {
    width: 100%;
    border-collapse: collapse;
}

.users-table thead {
    background: #f8f9fa;
}

.users-table th {
    padding: 1rem;
    text-align: left;
    font-weight: 600;
    color: #374151;
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 2px solid #e5e7eb;
}

.users-table td {
    padding: 1rem;
    border-bottom: 1px solid #e5e7eb;
}

.users-table tbody tr:hover {
    background: #f9fafb;
}

.user-cell {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.table-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.table-username {
    font-weight: 600;
    color: #1f2937;
}

.table-actions {
    display: flex;
    gap: 0.5rem;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.btn-edit {
    background: white;
    color: #667eea;
    border: 2px solid #e5e7eb;
    flex: 1;
    justify-content: center;
}

.btn-edit:hover {
    background: #f3f4f6;
    border-color: #667eea;
}

.btn-block,
.btn-unblock,
.btn-delete {
    padding: 0.75rem;
    min-width: auto;
}

.btn-block {
    background: #fee2e2;
    color: #dc2626;
}

.btn-block:hover {
    background: #dc2626;
    color: white;
}

.btn-unblock {
    background: #d1fae5;
    color: #059669;
}

.btn-unblock:hover {
    background: #059669;
    color: white;
}

.btn-delete {
    background: #fee2e2;
    color: #dc2626;
}

.btn-delete:hover {
    background: #dc2626;
    color: white;
}

.btn-secondary {
    background: #e5e7eb;
    color: #374151;
}

.btn-secondary:hover {
    background: #d1d5db;
}

.btn-danger {
    background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
    color: white;
}

.btn-danger:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

.btn-action {
    padding: 0.5rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    background: transparent;
    color: #6b7280;
}

.btn-action:hover {
    background: #f3f4f6;
}

.btn-action.btn-edit:hover {
    color: #667eea;
    background: rgba(102, 126, 234, 0.1);
}

.btn-action.btn-block:hover {
    color: #dc2626;
    background: rgba(220, 38, 38, 0.1);
}

.btn-action.btn-unblock:hover {
    color: #059669;
    background: rgba(5, 150, 105, 0.1);
}

.btn-action.btn-delete:hover {
    color: #dc2626;
    background: rgba(220, 38, 38, 0.1);
}

.btn-lg {
    padding: 1rem 2rem;
    font-size: 1.1rem;
}

/* Status Badges */
.status-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
}

.status-active {
    background: #d1fae5;
    color: #059669;
}

.status-blocked {
    background: #fee2e2;
    color: #dc2626;
}

/* Role Badges */
.role-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
}

.role-team_member {
    background: #dbeafe;
    color: #1e40af;
}

.role-team_leader {
    background: #fef3c7;
    color: #92400e;
}

.role-supervisor {
    background: #ede9fe;
    color: #5b21b6;
}

.role-admin {
    background: #fee2e2;
    color: #991b1b;
}

.role-system_admin {
    background: #fce7f3;
    color: #be185d;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background-color: white;
    margin: 10% auto;
    padding: 0;
    border-radius: 12px;
    width: 90%;
    max-width: 500px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        transform: translateY(-50px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 1.5rem;
    border-radius: 12px 12px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-title {
    margin: 0;
    font-size: 1.5rem;
}

.modal-close {
    font-size: 2rem;
    cursor: pointer;
    opacity: 0.8;
    transition: opacity 0.2s;
}

.modal-close:hover {
    opacity: 1;
}

.modal-body {
    padding: 2rem;
}

.modal-body p {
    margin-bottom: 1rem;
}

.warning-text {
    color: #dc2626;
    font-weight: 600;
}

.modal-footer {
    padding: 1.5rem;
    background: #f8f9fa;
    border-radius: 0 0 12px 12px;
    border-top: 1px solid #e5e7eb;
}

.modal-footer form {
    display: flex;
    gap: 1rem;
    justify-content: flex-end;
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    background: white;
    border-radius: 12px;
    border: 2px dashed #e5e7eb;
}

.empty-icon {
    font-size: 4rem;
    margin-bottom: 1.5rem;
    opacity: 0.3;
}

.empty-title {
    font-size: 1.75rem;
    font-weight: 700;
    color: #1f2937;
    margin-bottom: 0.5rem;
}

.empty-message {
    font-size: 1.1rem;
    color: #6b7280;
    margin-bottom: 2rem;
}

.empty-hint {
    color: #9ca3af;
    font-size: 0.875rem;
}

/* No Results */
.no-results {
    text-align: center;
    padding: 3rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    .users-admin-container {
        padding: 1rem;
    }
    
    .page-header {
        padding: 1.5rem;
    }
    
    .page-title {
        font-size: 1.75rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .view-controls {
        flex-direction: column;
    }
    
    .search-container {
        max-width: 100%;
    }
    
    .users-grid {
        grid-template-columns: 1fr;
    }
    
    .users-table {
        font-size: 0.875rem;
    }
    
    .users-table th,
    .users-table td {
        padding: 0.75rem;
    }
    
    .table-actions {
        flex-direction: column;
    }
    
    .user-actions {
        flex-wrap: wrap;
    }
    
    .btn-edit {
        width: 100%;
    }
}

/* Animations */
.user-card,
.user-row {
    animation: slideIn 0.4s ease-out;
    animation-fill-mode: both;
}

.user-card:nth-child(1),
.user-row:nth-child(1) { animation-delay: 0.05s; }
.user-card:nth-child(2),
.user-row:nth-child(2) { animation-delay: 0.1s; }
.user-card:nth-child(3),
.user-row:nth-child(3) { animation-delay: 0.15s; }
.user-card:nth-child(4),
.user-row:nth-child(4) { animation-delay: 0.2s; }
.user-card:nth-child(5),
.user-row:nth-child(5) { animation-delay: 0.25s; }
.user-card:nth-child(6),
.user-row:nth-child(6) { animation-delay: 0.3s; }
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // View Toggle
    const toggleBtns = document.querySelectorAll('.toggle-btn');
    const gridView = document.getElementById('gridView');
    const listView = document.getElementById('listView');
    
    toggleBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            const view = this.getAttribute('data-view');
            
            // Update button states
            toggleBtns.forEach(b => b.classList.remove('active'));
            this.classList.add('active');
            
            // Show/hide views
            if (view === 'grid') {
                gridView.classList.add('active');
                listView.classList.remove('active');
            } else {
                gridView.classList.remove('active');
                listView.classList.add('active');
            }
        });
    });
    
    // Search functionality
    const searchInput = document.getElementById('userSearch');
    const usersGrid = document.getElementById('usersGrid');
    const usersTableBody = document.getElementById('usersTableBody');
    const noResults = document.getElementById('noResults');
    
    if (searchInput) {
        searchInput.addEventListener('input', function() {
            const searchTerm = this.value.toLowerCase().trim();
            
            // Search in grid view
            if (usersGrid) {
                const userCards = usersGrid.querySelectorAll('.user-card');
                let visibleCount = 0;
                
                userCards.forEach(card => {
                    const username = card.getAttribute('data-username');
                    const email = card.getAttribute('data-email');
                    const role = card.getAttribute('data-role');
                    
                    if (username.includes(searchTerm) || 
                        email.includes(searchTerm) || 
                        role.includes(searchTerm)) {
                        card.style.display = '';
                        visibleCount++;
                    } else {
                        card.style.display = 'none';
                    }
                });
                
                // Show/hide no results in grid view
                if (gridView.classList.contains('active') && noResults) {
                    noResults.style.display = visibleCount === 0 ? 'block' : 'none';
                }
            }
            
            // Search in list view
            if (usersTableBody) {
                const userRows = usersTableBody.querySelectorAll('.user-row');
                let visibleCount = 0;
                
                userRows.forEach(row => {
                    const username = row.getAttribute('data-username');
                    const email = row.getAttribute('data-email');
                    const role = row.getAttribute('data-role');
                    
                    if (username.includes(searchTerm) || 
                        email.includes(searchTerm) || 
                        role.includes(searchTerm)) {
                        row.style.display = '';
                        visibleCount++;
                    } else {
                        row.style.display = 'none';
                    }
                });
                
                // Show/hide no results in list view
                if (listView.classList.contains('active') && noResults) {
                    noResults.style.display = visibleCount === 0 ? 'block' : 'none';
                }
            }
        });
    }
});

function confirmDelete(userId, username) {
    document.getElementById('delete-username').textContent = username;
    document.getElementById('delete-form').action = "{{ url_for('users.delete_user', user_id=0) }}".replace('0', userId);
    document.getElementById('delete-modal').style.display = 'block';
}

// Modal controls
document.querySelector('.modal-close').addEventListener('click', function() {
    document.getElementById('delete-modal').style.display = 'none';
});

document.getElementById('cancel-delete').addEventListener('click', function() {
    document.getElementById('delete-modal').style.display = 'none';
});

window.addEventListener('click', function(event) {
    if (event.target == document.getElementById('delete-modal')) {
        document.getElementById('delete-modal').style.display = 'none';
    }
});
</script>

{% endblock %}