{% extends "layout/app.twig" %}
{% block page_title_block %}{{ __(page_title_key) }}{% endblock %}

{% block content %}
    <div class="flex justify-between items-center mb-6">
        {# O título H1 é gerenciado pelo Topbar via 'page_title_key' passado pelo controller #}
        <a href="{{ BASE_URL }}{{ routeParser.urlFor('admin.users.create') }}" class="btn-primary inline-flex items-center">
            <i class="fas fa-plus mr-2"></i> {{ __('button.new_user') }}
        </a>
    </div>

    {% include 'partials/flash_messages.twig' %}

    <div class="bg-white shadow-md rounded-lg overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="table-th">{{ __('table_header.name') }}</th>
                        <th class="table-th">{{ __('label.email') }}</th>
                        <th class="table-th">{{ __('table_header.role') }}</th>
                        <th class="table-th">{{ __('table_header.status') }}</th>
                        <th class="table-th">{{ __('table_header.last_login') }}</th>
                        <th class="table-th text-right">{{ __('table_header.actions') }}</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    {% for user in users %}
                    <tr>
                        <td class="table-td font-medium">{{ user.name }}</td>
                        <td class="table-td">{{ user.email }}</td>
                        <td class="table-td"><span class="role-badge role-{{ user.role_name | lower | replace({' ': '-'}) }}">{{ user.role_name | default('N/A') }}</span></td>
                        <td class="table-td">{% if user.is_active %}<span class="status-badge status-active">{{ __('status.active') }}</span>{% else %}<span class="status-badge status-inactive">{{ __('status.inactive') }}</span>{% endif %}</td>
                        <td class="table-td">{{ user.last_login ? (user.last_login | date(__( "format.datetime_short"))) : __('text.never') }}</td>
                        <td class="table-td text-right">
                            <a href="{{ BASE_URL }}{{ routeParser.urlFor('admin.users.edit', {'id': user.id}) }}" class="text-indigo-600 hover:text-indigo-900 mr-3" title="{{ __('button.edit') }}"><i class="fas fa-edit"></i></a>
                            {% if session.user_id != user.id %}
                            <form action="{{ BASE_URL }}{{ routeParser.urlFor('admin.users.delete', {'id': user.id}) }}" method="POST" class="inline-block" onsubmit="return confirm('{{ __('confirm_delete_message') }}');">
                                <input type="hidden" name="_METHOD" value="DELETE"> 
                                <button type="submit" class="text-red-600 hover:text-red-900" title="{{ __('button.delete') }}"><i class="fas fa-trash-alt"></i></button>
                            </form>
                            {% endif %}
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="6" class="table-td text-center">{{ __('text.no_users_found') }}</td></tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        {% if totalPages > 1 %}
            <div class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
                 <div class="text-sm text-gray-700">
                    {{ __('pagination.displaying_items', {'shown': users|length, 'total': totalItems, 'item_type': __('item_type.user_plural'), 'current': currentPage, 'pages': totalPages}) }}
                </div>
                {% include 'partials/pagination.twig' with {'totalPages': totalPages, 'currentPage': currentPage, 'baseUrl': baseUrl, 'queryParams': queryParams } %}
            </div>
        {% elseif totalItems > 0 %}
             <div class="bg-white px-4 py-3 text-sm text-gray-700 border-t border-gray-200 sm:px-6">
                {{ __('pagination.total_items_of_type', {'total': totalItems, 'item_type': __('item_type.user_plural')}) }}
            </div>
        {% endif %}
    </div>
    <style>.table-th{padding:0.75rem 1.5rem; text-align:left; font-size:0.75rem; font-weight:500; color:#6B7280; text-transform:uppercase; letter-spacing:0.05em;} .table-td{padding:0.75rem 1.5rem; font-size:0.875rem; color:#374151;} .role-badge{padding: 0.25rem 0.5rem; font-size: 0.75rem; font-weight: 600; border-radius: 9999px;} .role-administrador{background-color: #F3E8FF; color: #8B5CF6;} .role-advogado{background-color: #DBEAFE; color: #3B82F6;} .role-assistente{background-color: #FEF3C7; color: #D97706;} .status-badge{padding: 0.25rem 0.5rem; font-size: 0.75rem; font-weight: 600; border-radius: 9999px;} .status-active{background-color: #D1FAE5; color: #059669;} .status-inactive{background-color: #FEE2E2; color: #DC2626;}</style>
{% endblock %}