{% 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"><i class="ti ti-users"></i></span>
                    System Admin - All Users
                </h1>
                <p class="page-subtitle">Manage users across all companies</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('system_admin.system_admin_dashboard') }}" class="btn btn-secondary">
                    <i class="ti ti-arrow-left"></i>
                    Back to Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- Filter Options -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title">
                <span class="icon"><i class="ti ti-filter"></i></span>
                Filter Users
            </h2>
        </div>
        <div class="card-body">
        <div class="filter-buttons">
            <a href="{{ url_for('users.system_admin_users') }}" 
               class="btn btn-filter {% if not current_filter %}active{% endif %}">
                All Users ({{ users.total }})
            </a>
            <a href="{{ url_for('users.system_admin_users', filter='system_admins') }}" 
               class="btn btn-filter {% if current_filter == 'system_admins' %}active{% endif %}">
                System Admins
            </a>
            <a href="{{ url_for('users.system_admin_users', filter='admins') }}" 
               class="btn btn-filter {% if current_filter == 'admins' %}active{% endif %}">
                Company Admins
            </a>
            <a href="{{ url_for('users.system_admin_users', filter='blocked') }}" 
               class="btn btn-filter {% if current_filter == 'blocked' %}active{% endif %}">
                Blocked Users
            </a>
            <a href="{{ url_for('users.system_admin_users', filter='unverified') }}" 
               class="btn btn-filter {% if current_filter == 'unverified' %}active{% endif %}">
                Unverified
            </a>
            <a href="{{ url_for('users.system_admin_users', filter='freelancers') }}" 
               class="btn btn-filter {% if current_filter == 'freelancers' %}active{% endif %}">
                Freelancers
            </a>
        </div>
        </div>
    </div>

    <!-- Users Table -->
    {% if users.items %}
    <div class="card">
        <div class="card-body no-padding">
        <table class="table">
            <thead>
                <tr>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Company</th>
                    <th>Role</th>
                    <th>Account Type</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for user_data in users.items %}
                {% if user_data is sequence and user_data|length == 2 %}
                    {% set user = user_data[0] %}
                    {% set company_name = user_data[1] %}
                {% else %}
                    {# Fallback for when data structure is unexpected #}
                    {% set user = user_data %}
                    {% set company_name = user.company.name if user.company else 'Unknown' %}
                {% endif %}
                <tr class="{% if user.is_blocked %}blocked-user{% endif %}">
                    <td>
                        <strong>{{ user.username }}</strong>
                        {% if user.id == g.user.id %}
                            <span class="badge badge-self">You</span>
                        {% endif %}
                    </td>
                    <td>{{ user.email }}</td>
                    <td>
                        <span class="company-name">{{ company_name }}</span>
                        {% if user.company and user.company.is_personal %}
                            <span class="badge badge-personal">Personal</span>
                        {% endif %}
                    </td>
                    <td>
                        <span class="role-badge role-{{ user.role.name.lower() }}">
                            {{ user.role.value }}
                        </span>
                    </td>
                    <td>
                        <span class="badge {% if user.account_type == AccountType.FREELANCER %}badge-freelancer{% else %}badge-company{% endif %}">
                            {{ user.account_type.value }}
                        </span>
                    </td>
                    <td>
                        {% if user.is_blocked %}
                            <span class="status-badge status-blocked">Blocked</span>
                        {% elif not user.is_verified %}
                            <span class="status-badge status-unverified">Unverified</span>
                        {% else %}
                            <span class="status-badge status-active">Active</span>
                        {% endif %}
                    </td>
                    <td>{{ user.created_at.strftime('%Y-%m-%d') }}</td>
                    <td>
                        <div class="action-buttons">
                            <a href="{{ url_for('users.system_admin_edit_user', user_id=user.id) }}" 
                               class="btn btn-sm btn-primary">Edit</a>
                            
                            {% if user.id != g.user.id and not (user.role == Role.SYSTEM_ADMIN and user.id == g.user.id) %}
                            <form method="POST" action="{{ url_for('users.system_admin_delete_user', user_id=user.id) }}" 
                                  style="display: inline;" 
                                  onsubmit="return confirm('Are you sure you want to delete user \'{{ user.username }}\' from company \'{{ company_name }}\'? This action cannot be undone.')">
                                <button type="submit" class="btn btn-sm btn-danger">Delete</button>
                            </form>
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        </div>
    </div>

    <!-- Pagination -->
    {% if users.pages > 1 %}
    <div class="pagination-section">
        <div class="pagination">
            {% if users.has_prev %}
                <a href="{{ url_for('users.system_admin_users', page=users.prev_num, filter=current_filter) }}" class="page-link"><i class="ti ti-arrow-left"></i> Previous</a>
            {% endif %}
            
            {% for page_num in users.iter_pages() %}
                {% if page_num %}
                    {% if page_num != users.page %}
                        <a href="{{ url_for('users.system_admin_users', page=page_num, filter=current_filter) }}" class="page-link">{{ page_num }}</a>
                    {% else %}
                        <span class="page-link current">{{ page_num }}</span>
                    {% endif %}
                {% else %}
                    <span class="page-link">…</span>
                {% endif %}
            {% endfor %}
            
            {% if users.has_next %}
                <a href="{{ url_for('users.system_admin_users', page=users.next_num, filter=current_filter) }}" class="page-link">Next <i class="ti ti-arrow-right"></i></a>
            {% endif %}
        </div>
        
        <p class="pagination-info">
            Showing {{ users.per_page * (users.page - 1) + 1 }} - 
            {{ users.per_page * (users.page - 1) + users.items|length }} of {{ users.total }} users
        </p>
    </div>
    {% endif %}

    {% else %}
    <div class="empty-state">
        <h3>No users found</h3>
        <p>No users match the current filter criteria.</p>
    </div>
    {% endif %}
</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;
}

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

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

.card-header {
    background: #f8f9fa;
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: #1f2937;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-title .icon {
    font-size: 1.5rem;
}

.card-body {
    padding: 1.5rem;
}

.card-body.no-padding {
    padding: 0;
}

/* Filter Buttons */
.filter-buttons {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.btn-filter {
    padding: 0.5rem 1rem;
    border: 1px solid #e5e7eb;
    background: white;
    color: #6b7280;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.2s ease;
}

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

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

/* Table */
.table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
}

.table th,
.table td {
    padding: 1rem;
    text-align: left;
    border-bottom: 1px solid #e5e7eb;
}

.table th {
    background: #f8f9fa;
    font-weight: 600;
    color: #374151;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-size: 0.8rem;
}

.table tr:hover {
    background: #f8f9fa;
}

.blocked-user {
    background-color: #fef2f2 !important;
}

.company-name {
    font-weight: 600;
    color: #1f2937;
}

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

.badge-self {
    background: #dbeafe;
    color: #1e40af;
}

.badge-personal {
    background: #fef3c7;
    color: #92400e;
}

.badge-company {
    background: #dbeafe;
    color: #1e40af;
}

.badge-freelancer {
    background: #d1fae5;
    color: #065f46;
}

/* 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: #e5e7eb;
    color: #374151;
}

.role-team_leader {
    background: #d1fae5;
    color: #065f46;
}

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

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

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

/* 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: #065f46;
}

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

.status-unverified {
    background: #fef3c7;
    color: #92400e;
}

/* Action Buttons */
.action-buttons {
    display: flex;
    gap: 0.5rem;
}

/* Pagination */
.pagination-section {
    margin: 2rem 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.pagination {
    display: flex;
    gap: 0.25rem;
}

.page-link {
    padding: 0.5rem 1rem;
    border: 1px solid #e5e7eb;
    color: #667eea;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
}

.page-link:hover {
    background: #f3f4f6;
    border-color: #667eea;
}

.page-link.current {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-color: transparent;
}

.pagination-info {
    color: #6b7280;
    margin: 0;
    font-size: 0.9rem;
}

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

.empty-state h3 {
    font-size: 1.5rem;
    color: #1f2937;
    margin-bottom: 0.5rem;
}

.empty-state p {
    color: #6b7280;
    font-size: 1.1rem;
}

/* 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-secondary {
    background: white;
    color: #667eea;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.5);
}

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

.btn-danger:hover {
    background: #b91c1c;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(220, 38, 38, 0.3);
}

.btn-sm {
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    .users-admin-container {
        padding: 1rem;
    }
    
    .page-header {
        padding: 1.5rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .table {
        font-size: 0.8rem;
    }
    
    .table th,
    .table td {
        padding: 0.5rem;
    }
    
    .action-buttons {
        flex-direction: column;
        gap: 0.25rem;
    }
}

/* Animations */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: slideIn 0.3s ease-out;
    animation-fill-mode: both;
}

.card:nth-child(1) { animation-delay: 0.1s; }
.card:nth-child(2) { animation-delay: 0.2s; }
.card:nth-child(3) { animation-delay: 0.3s; }
</style>
{% endblock %}