<div>
    <!-- Controles de columnas -->
    <div class="mb-4 flex items-center gap-4">
        <div class="relative" x-data="{ open: false }">
            <button @click="open = !open" class="bg-gray-100 px-4 py-2 rounded-lg hover:bg-gray-200">
                Columnas ▼
            </button>
            
            <div x-show="open" @click.outside="open = false" 
                    class="absolute bg-white shadow-lg rounded-lg p-4 mt-2 min-w-[200px] z-10">
                @foreach($available_columns as $key => $label)
                    <label class="flex items-center gap-2 mb-2">
                        <input type="checkbox" wire:model.live="columns.{{ $key }}" 
                                class="rounded border-gray-300">
                        {{ $label }}
                    </label>
                @endforeach
            </div>
        </div>
    </div>

    <!-- Tabla -->
    <div class="bg-white rounded-lg shadow overflow-x-auto">
        <table class="min-w-full">
            <thead class="bg-gray-50">
                <tr>
                    @foreach($available_columns as $key => $label)
                        @if($columns[$key])
                            @if($key !== 'is_active')
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase cursor-pointer" >
                                    <div class="flex items-center justify-between">
                                        <div class="flex flex-col">
                                            <span wire:click="sortBy('{{ $key }}')">{{ $label }}</span>
                                            <input type="text" 
                                                wire:model.live.debounce.300ms="filters.{{ $key }}" 
                                                class="mt-1 text-xs w-full border rounded px-2 py-1"
                                                placeholder="Filtrar...">
                                        </div>
                                        @if($sortField === $key)
                                            <span class="ml-2">
                                                @if($sortDirection === 'asc')
                                                    ↑
                                                @else
                                                    ↓
                                                @endif
                                            </span>
                                        @endif
                                    </div>
                                </th>
                            @else
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                                <div class="flex flex-col">
                                    <span>Estado</span>
                                    <select wire:model.live="filters.status" class="mt-1 text-xs w-full border rounded px-2 py-1">
                                        <option value="">Todos</option>
                                        <option value="active">Activo</option>
                                        <option value="inactive">Inactivo</option>
                                    </select>
                                </div>
                            </th>
                            @endif
                        @endif
                    @endforeach
                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Acciones</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                @foreach($users as $user)
                    <tr>
                        <!-- Iteramos dinámicamente sobre las columnas disponibles -->
                        @foreach($available_columns as $key => $label)
                            @if($columns[$key])
                                <td class="px-6 py-4 whitespace-nowrap">
                                    @switch($key)
                                        @case('full_name')
                                            <a href="{{ route('users.show', $user) }}" class="flex items-center hover:text-blue-600 hover:underline group">
                                                @if($user->profile_photo_path)
                                                    <div class="mr-3 flex-shrink-0">
                                                        <img src="{{ asset('storage/' . $user->profile_photo_path) }}" 
                                                            alt="{{ $user->full_name }}"
                                                            class="w-8 h-8 rounded-full object-cover transition-transform group-hover:scale-110">
                                                    </div>
                                                @else
                                                    <div class="w-8 h-8 rounded-full bg-gray-100 mr-3 flex items-center justify-center transition-colors group-hover:bg-blue-100">
                                                        <flux:icon.user variant="solid" class="size-4" />
                                                    </div>
                                                @endif
                                                
                                                <span class="group-hover:text-blue-600 transition-colors">
                                                    {{ $user->full_name }}
                                                    @if(!$user->is_active)
                                                        <span class="text-xs text-red-500 ml-2">(Inactivo)</span>
                                                    @endif
                                                </span>
                                            </a>
                                            @break

                                        @case('email')
                                            <div class="flex items-center gap-2">
                                                <svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
                                                </svg>
                                                {{ $user->email }}
                                            </div>
                                            @break

                                        @case('phone')
                                            <div class="flex items-center gap-2">
                                                <svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/>
                                                </svg>
                                                {{ $user->phone ?? 'N/A' }}
                                            </div>
                                            @break

                                        @case('access_start')
                                            {{ $user->access_start?->format('d/m/Y') ?? 'N/A' }}
                                            @break

                                        @case('created_at')
                                            {{ $user->created_at->format('d/m/Y H:i') }}
                                            @break

                                        @case('is_active')
                                            {{ $user->is_active ? 'Activo' : 'Inactivo' }}
                                            @break

                                        @default
                                            {{ $user->$key ?? 'N/A' }}
                                    @endswitch
                                </td>
                            @endif
                        @endforeach
                        
                        <td class="px-6 py-4 whitespace-nowrap">
                            <!-- Acciones -->
                            <div class="flex items-center gap-2">
                                <!-- Botón Editar -->
                                <a href="{{ route('users.edit', $user) }}" 
                                class="text-blue-600 hover:text-blue-900"
                                title="Editar usuario">
                                    <flux:icon.pencil class="size-5"/> 
                                </a>
                
                                <!-- Botón Eliminar -->
                                <button wire:click="confirmDelete({{ $user->id }})" 
                                        class="text-red-600 hover:text-red-900"
                                        title="Eliminar usuario">
                                    <flux:icon.trash class="size-5"/> 
                                </button>

                                <flux:button size="sm" icon="trash" variant="ghost" inset />
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>

    <!-- Paginación -->
    <div class="mt-4">
        {{ $users->links() }}
    </div>
</div>