{% extends "layout.html" %}

{% block content %}
<div class="container">
    <div class="header-section">
        <h1>👥 System Admin - All Users</h1>
        <p class="subtitle">Manage users across all companies</p>
        <a href="{{ url_for('system_admin_dashboard') }}" class="btn btn-md btn-secondary">← Back to Dashboard</a>
    </div>

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

    <!-- Users Table -->
    {% if users.items %}
    <div class="table-section">
        <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 %}
                {% set user = user_data[0] %}
                {% set company_name = user_data[1] %}
                <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('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('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>

    <!-- Pagination -->
    {% if users.pages > 1 %}
    <div class="pagination-section">
        <div class="pagination">
            {% if users.has_prev %}
                <a href="{{ url_for('system_admin_users', page=users.prev_num, filter=current_filter) }}" class="page-link">← Previous</a>
            {% endif %}
            
            {% for page_num in users.iter_pages() %}
                {% if page_num %}
                    {% if page_num != users.page %}
                        <a href="{{ url_for('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('system_admin_users', page=users.next_num, filter=current_filter) }}" class="page-link">Next →</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>
.header-section {
    margin-bottom: 2rem;
}

.subtitle {
    color: #6c757d;
    margin-bottom: 1rem;
}

.filter-section {
    background: #f8f9fa;
    padding: 1.5rem;
    border-radius: 8px;
    margin-bottom: 2rem;
}

.filter-section h3 {
    margin-top: 0;
    margin-bottom: 1rem;
}

.filter-buttons {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.btn-filter {
    padding: 0.5rem 1rem;
    border: 1px solid #dee2e6;
    background: white;
    color: #495057;
    text-decoration: none;
    border-radius: 4px;
    transition: all 0.2s;
}

.btn-filter:hover {
    background: #e9ecef;
}

.btn-filter.active {
    background: #007bff;
    color: white;
    border-color: #007bff;
}

.table-section {
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

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

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

.table th {
    background: #f8f9fa;
    font-weight: 600;
    color: #495057;
}

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

.badge {
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
}

.badge-self {
    background: #d1ecf1;
    color: #0c5460;
}

.badge-personal {
    background: #fff3cd;
    color: #856404;
}

.badge-company {
    background: #d1ecf1;
    color: #0c5460;
}

.badge-freelancer {
    background: #d4edda;
    color: #155724;
}

.role-badge {
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
}

.role-team_member {
    background: #e2e3e5;
    color: #495057;
}

.role-team_leader {
    background: #d4edda;
    color: #155724;
}

.role-supervisor {
    background: #d1ecf1;
    color: #0c5460;
}

.role-admin {
    background: #fff3cd;
    color: #856404;
}

.role-system_admin {
    background: #f1c0e8;
    color: #6a1b99;
}

.status-badge {
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
}

.status-active {
    background: #d4edda;
    color: #155724;
}

.status-blocked {
    background: #f8d7da;
    color: #721c24;
}

.status-unverified {
    background: #fff3cd;
    color: #856404;
}

.action-buttons {
    display: flex;
    gap: 0.5rem;
}

.btn {
    display: inline-block;
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 4px;
    text-decoration: none;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s;
}

/* Button styles now centralized in main style.css */

.pagination-section {
    margin-top: 2rem;
    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 0.75rem;
    border: 1px solid #dee2e6;
    color: #007bff;
    text-decoration: none;
    border-radius: 4px;
}

.page-link:hover {
    background: #e9ecef;
}

.page-link.current {
    background: #007bff;
    color: white;
    border-color: #007bff;
}

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

.empty-state {
    text-align: center;
    padding: 3rem;
    color: #6c757d;
}

.company-name {
    font-weight: 500;
}
</style>
{% endblock %}