{% extends 'admin/base.html.twig' %}

{% block title %}Kullanicilar{% endblock %}
{% block page_title %}Kullanici Yonetimi{% endblock %}

{% block body %}
    {# Limit banner #}
    {% if userLimit is defined and userLimit.max > 0 %}
        <div class="mb-4 rounded-xl border p-3 {{ userLimit.percentUsed >= 100 ? 'bg-red-50 border-red-200' : (userLimit.percentUsed >= 80 ? 'bg-amber-50 border-amber-200' : 'bg-blue-50 border-blue-200') }}">
            <div class="flex items-center justify-between">
                <div class="flex items-center gap-2">
                    <i class="fas {{ userLimit.percentUsed >= 100 ? 'fa-exclamation-circle text-red-500' : (userLimit.percentUsed >= 80 ? 'fa-exclamation-triangle text-amber-500' : 'fa-users text-blue-500') }}"></i>
                    <span class="text-sm {{ userLimit.percentUsed >= 100 ? 'text-red-700' : (userLimit.percentUsed >= 80 ? 'text-amber-700' : 'text-blue-700') }}">
                        <strong>{{ userLimit.current }}/{{ userLimit.max }}</strong> kullanici kullanildi
                        {% if userLimit.percentUsed >= 100 %}
                            — Limit doldu!
                        {% endif %}
                    </span>
                </div>
                <div class="flex items-center gap-2">
                    <div class="w-32 bg-white/60 rounded-full h-2 overflow-hidden">
                        <div class="h-2 rounded-full {{ userLimit.percentUsed >= 100 ? 'bg-red-500' : (userLimit.percentUsed >= 80 ? 'bg-amber-500' : 'bg-blue-500') }}"
                             style="width: {{ userLimit.percentUsed > 100 ? 100 : userLimit.percentUsed }}%"></div>
                    </div>
                    <span class="text-xs font-medium {{ userLimit.percentUsed >= 100 ? 'text-red-600' : (userLimit.percentUsed >= 80 ? 'text-amber-600' : 'text-blue-600') }}">%{{ userLimit.percentUsed|round(0) }}</span>
                </div>
            </div>
        </div>
    {% endif %}

    {# Actions bar #}
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between mb-6 gap-4">
        <div class="flex items-center gap-3">
            {% if userLimit is defined and not userLimit.allowed %}
                <span class="btn-primary opacity-50 cursor-not-allowed" title="Kullanici limiti doldu">
                    <i class="fas fa-lock mr-2"></i> Limit Doldu
                </span>
            {% else %}
                <a href="{{ path('admin_user_create') }}" class="btn-primary">
                    <i class="fas fa-user-plus mr-2"></i> Yeni Kullanici
                </a>
            {% endif %}
        </div>
    </div>

    {# Users table #}
    <div class="card overflow-hidden">
        <div class="overflow-x-auto">
            <table class="table">
                <thead>
                    <tr>
                        <th class="w-12">Avatar</th>
                        <th>Ad Soyad</th>
                        <th>E-posta</th>
                        <th>Rol</th>
                        <th>Durum</th>
                        <th>Son Giris</th>
                        <th class="w-24">Islem</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200 bg-white">
                    {% for user in users %}
                        <tr>
                            <td>
                                {% if user.avatar %}
                                    <img src="{{ user.avatar }}" alt="{{ user.fullName }}"
                                         class="h-9 w-9 rounded-full object-cover">
                                {% else %}
                                    <span class="inline-flex h-9 w-9 items-center justify-center rounded-full bg-primary-100">
                                        <span class="text-sm font-medium text-primary-700">
                                            {{ user.fullName ? user.fullName|first|upper : user.username|first|upper }}
                                        </span>
                                    </span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{{ path('admin_user_edit', {id: user.id}) }}"
                                   class="font-medium text-gray-900 hover:text-primary-600">
                                    {{ user.fullName|default(user.username) }}
                                </a>
                                <span class="block text-xs text-gray-400 mt-0.5">{{ user.username }}</span>
                            </td>
                            <td class="text-gray-500 text-sm">
                                {{ user.email }}
                            </td>
                            <td>
                                {% if user.role == 'admin' %}
                                    <span class="inline-flex items-center rounded-full bg-red-50 px-2 py-0.5 text-xs font-medium text-red-700">
                                        <i class="fas fa-shield-alt mr-1"></i> Admin
                                    </span>
                                {% elseif user.role == 'editor' %}
                                    <span class="inline-flex items-center rounded-full bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-700">
                                        <i class="fas fa-edit mr-1"></i> Editor
                                    </span>
                                {% elseif user.role == 'author' %}
                                    <span class="inline-flex items-center rounded-full bg-green-50 px-2 py-0.5 text-xs font-medium text-green-700">
                                        <i class="fas fa-pen mr-1"></i> Yazar
                                    </span>
                                {% else %}
                                    <span class="inline-flex items-center rounded-full bg-gray-50 px-2 py-0.5 text-xs font-medium text-gray-700">
                                        <i class="fas fa-user mr-1"></i> Katilimci
                                    </span>
                                {% endif %}
                            </td>
                            <td>
                                {% if user.isActive %}
                                    <span class="badge-success">Aktif</span>
                                {% else %}
                                    <span class="badge-gray">Pasif</span>
                                {% endif %}
                            </td>
                            <td class="text-gray-500 text-sm">
                                {% if user.lastLoginAt %}
                                    {{ user.lastLoginAt|date('d.m.Y H:i') }}
                                {% else %}
                                    <span class="text-gray-400">-</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="flex items-center gap-2">
                                    <a href="{{ path('admin_user_edit', {id: user.id}) }}"
                                       class="text-primary-600 hover:text-primary-900" title="Duzenle">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <form method="post" action="{{ path('admin_user_delete', {id: user.id}) }}"
                                          onsubmit="return confirm('Bu kullaniciyi silmek istediginize emin misiniz?')">
                                        <input type="hidden" name="_token" value="{{ csrf_token('delete_user_' ~ user.id) }}">
                                        <button type="submit" class="text-red-600 hover:text-red-900" title="Sil">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    {% else %}
                        <tr>
                            <td colspan="7" class="text-center text-gray-500 py-12">
                                <i class="fas fa-users text-4xl text-gray-300 mb-3 block"></i>
                                Henuz kullanici eklenmemis.
                                <br>
                                <a href="{{ path('admin_user_create') }}" class="text-primary-600 hover:text-primary-500 mt-2 inline-block">
                                    Ilk kullaniciyi ekleyin
                                </a>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
