<x-app-layout>
    <x-slot name="header">
        <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
            <div>
                <p class="text-sm font-semibold uppercase tracking-wide text-blue-600 dark:text-blue-400">Admin Control</p>
                <h2 class="mt-1 text-2xl font-bold text-slate-950 dark:text-white">Users Management</h2>
            </div>
            <div class="flex flex-wrap gap-2">
                <a href="{{ route('admin.support-team.index') }}" class="rounded-lg border border-blue-100 px-4 py-2 text-sm font-semibold text-blue-700 transition hover:bg-blue-50 dark:border-blue-900/50 dark:text-blue-300 dark:hover:bg-blue-950/60">
                    Support Team ({{ $supportTeamCount }})
                </a>
                <a href="{{ route('admin.dashboard') }}" class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white transition hover:bg-blue-700 dark:bg-blue-500 dark:hover:bg-blue-400">
                    Admin Dashboard
                </a>
            </div>
        </div>
    </x-slot>

    <div class="space-y-6">
        @foreach (['success' => 'blue', 'error' => 'red'] as $key => $color)
            @if (session($key))
                <div class="rounded-lg border px-4 py-3 text-sm font-semibold {{ $color === 'blue' ? 'border-blue-100 bg-blue-50 text-blue-700 dark:border-blue-900/50 dark:bg-blue-950/40 dark:text-blue-300' : 'border-red-100 bg-red-50 text-red-700 dark:border-red-900/50 dark:bg-red-950/30 dark:text-red-300' }}">
                    {{ session($key) }}
                </div>
            @endif
        @endforeach

        <form method="GET" action="{{ route('admin.users.index') }}" class="rounded-lg border border-blue-100 bg-white p-4 shadow-sm dark:border-blue-900/50 dark:bg-slate-900">
            <div class="grid gap-3 lg:grid-cols-[1fr_14rem_auto_auto]">
                <input name="search" value="{{ $search }}" placeholder="Search by name or email" class="block w-full rounded-lg border-slate-300 bg-white text-slate-900 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-100">
                <select name="role" class="block w-full rounded-lg border-slate-300 bg-white text-slate-900 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:border-slate-700 dark:bg-slate-950 dark:text-slate-100">
                    <option value="">All Roles</option>
                    @foreach ($roles as $value => $label)
                        <option value="{{ $value }}" @selected($role === $value)>{{ $label }}</option>
                    @endforeach
                </select>
                <button type="submit" class="rounded-lg bg-blue-600 px-5 py-2 text-sm font-semibold text-white transition hover:bg-blue-700">
                    Filter
                </button>
                @if ($search || $role)
                    <a href="{{ route('admin.users.index') }}" class="rounded-lg border border-blue-100 px-5 py-2 text-center text-sm font-semibold text-blue-700 transition hover:bg-blue-50 dark:border-blue-900/50 dark:text-blue-300 dark:hover:bg-blue-950/60">
                        Clear
                    </a>
                @endif
            </div>
        </form>

        <div class="overflow-hidden rounded-lg border border-blue-100 bg-white shadow-sm vt-card-motion dark:border-blue-900/50 dark:bg-slate-900">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-blue-100 dark:divide-blue-900/50">
                    <thead class="bg-blue-50 dark:bg-blue-950/40">
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">User</th>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Role</th>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Status</th>
                            <th class="px-4 py-3 text-left text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Trades</th>
                            <th class="px-4 py-3 text-right text-xs font-bold uppercase tracking-wide text-blue-700 dark:text-blue-300">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-blue-50 bg-white dark:divide-blue-900/40 dark:bg-slate-900">
                        @forelse ($users as $user)
                            <tr>
                                <td class="px-4 py-4">
                                    <p class="text-sm font-semibold text-slate-900 dark:text-slate-100">{{ $user->name }}</p>
                                    <p class="mt-1 text-sm text-slate-500 dark:text-slate-400">{{ $user->email }}</p>
                                </td>
                                <td class="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ $roles[$user->role] ?? str($user->role)->headline() }}</td>
                                <td class="px-4 py-4 text-sm capitalize {{ $user->status === 'active' ? 'text-blue-600 dark:text-blue-400' : 'text-red-500 dark:text-red-400' }}">{{ $user->status }}</td>
                                <td class="px-4 py-4 text-sm text-slate-600 dark:text-slate-300">{{ $user->trades_count }}</td>
                                <td class="px-4 py-4 text-right text-sm">
                                    <div class="flex flex-wrap justify-end gap-2">
                                        <a href="{{ route('admin.users.show', $user) }}" class="font-semibold text-blue-700 hover:text-blue-900 dark:text-blue-300">View</a>
                                        <a href="{{ route('admin.users.edit', $user) }}" class="font-semibold text-slate-600 hover:text-slate-900 dark:text-slate-300 dark:hover:text-white">Edit</a>
                                        <form method="POST" action="{{ route('admin.users.toggle-status', $user) }}">
                                            @csrf
                                            @method('PATCH')
                                            <button type="submit" class="font-semibold {{ $user->status === 'active' ? 'text-red-600 dark:text-red-400' : 'text-blue-700 dark:text-blue-300' }}">
                                                {{ $user->status === 'active' ? 'Block' : 'Unblock' }}
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="px-4 py-10 text-center text-sm text-slate-500 dark:text-slate-400">No users found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if ($users->hasPages())
                <div class="border-t border-blue-100 px-4 py-3 dark:border-blue-900/50">
                    {{ $users->links() }}
                </div>
            @endif
        </div>
    </div>
</x-app-layout>
