<div>
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('المحافظ') }}</h1>
    </div>

    {{-- Flash Messages --}}
    @if(session('success'))
    <div class="mb-4 p-3 sm:p-4 bg-green-50 border border-green-200 rounded-lg text-green-700 text-sm">
        {{ session('success') }}
    </div>
    @endif

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3 sm:p-4 mb-4">
        <div class="grid grid-cols-2 md:grid-cols-3 gap-3">
            <div class="col-span-2 md:col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم...') }}"
                       class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <select wire:model.live="activeFilter" class="px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('الكل') }}</option>
                <option value="1">{{ __('نشطة') }}</option>
                <option value="0">{{ __('غير نشطة') }}</option>
            </select>
        </div>
    </div>

    {{-- Desktop Table --}}
    <div class="hidden md:block bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto" wire:loading.class="opacity-50 pointer-events-none">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 border-b border-gray-200">
                    <tr>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('صاحب المحفظة') }}</th>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('النوع') }}</th>
                        <th class="px-4 py-3 text-end font-medium text-gray-600">{{ __('الرصيد') }}</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('الحالة') }}</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($wallets as $wallet)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <span class="font-medium text-gray-800">
                                @if($wallet->owner && method_exists($wallet->owner, 'person') && $wallet->owner->person)
                                    {{ $wallet->owner->person->name_ar }}
                                @elseif($wallet->owner)
                                    {{ $wallet->owner->name_ar ?? $wallet->owner->name ?? '—' }}
                                @else
                                    —
                                @endif
                            </span>
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            @php
                                $typeLabels = [
                                    'App\\Domain\\Participant\\Models\\Participant' => 'مشترك',
                                ];
                                $ownerType = $wallet->owner_type;
                            @endphp
                            {{ $typeLabels[$ownerType] ?? class_basename($ownerType) }}
                        </td>
                        <td class="px-4 py-3 text-end font-mono {{ $wallet->balance > 0 ? 'text-green-700' : 'text-gray-600' }}" dir="ltr">
                            {{ number_format($wallet->balance / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @if($wallet->is_active)
                                <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">{{ __('نشطة') }}</span>
                            @else
                                <span class="px-2 py-0.5 text-xs bg-gray-100 text-gray-700 rounded-full">{{ __('غير نشطة') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @can('wallets.view')
                            <a href="{{ route('wallets.show', $wallet) }}" wire:navigate
                               class="text-blue-600 hover:text-blue-800 text-sm">{{ __('عرض') }}</a>
                            @endcan
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="px-4 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <svg class="w-12 h-12 text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد محافظ') }}</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($wallets->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $wallets->links() }}
        </div>
        @endif
    </div>

    {{-- Mobile Cards --}}
    <div class="md:hidden space-y-3" wire:loading.class="opacity-50 pointer-events-none">
        @forelse($wallets as $wallet)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <div class="flex items-start justify-between gap-3 mb-3">
                <div class="min-w-0 flex-1">
                    <p class="font-medium text-gray-800 text-sm truncate">
                        @if($wallet->owner && method_exists($wallet->owner, 'person') && $wallet->owner->person)
                            {{ $wallet->owner->person->name_ar }}
                        @elseif($wallet->owner)
                            {{ $wallet->owner->name_ar ?? $wallet->owner->name ?? '—' }}
                        @else
                            —
                        @endif
                    </p>
                    <p class="text-xs text-gray-500 mt-0.5">
                        @php
                            $typeLabels = [
                                'App\\Domain\\Participant\\Models\\Participant' => 'مشترك',
                            ];
                            $ownerType = $wallet->owner_type;
                        @endphp
                        {{ $typeLabels[$ownerType] ?? class_basename($ownerType) }}
                    </p>
                </div>
                @if($wallet->is_active)
                    <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full shrink-0">{{ __('نشطة') }}</span>
                @else
                    <span class="px-2 py-0.5 text-xs bg-gray-100 text-gray-700 rounded-full shrink-0">{{ __('غير نشطة') }}</span>
                @endif
            </div>

            <div class="flex items-center justify-between">
                <span class="font-mono text-sm {{ $wallet->balance > 0 ? 'text-green-700' : 'text-gray-600' }}" dir="ltr">
                    {{ number_format($wallet->balance / 100, 2) }} {{ __('ج.م') }}
                </span>
                @can('wallets.view')
                <a href="{{ route('wallets.show', $wallet) }}" wire:navigate
                   class="inline-flex items-center justify-center py-2 px-3 text-sm text-blue-600 hover:text-blue-800 hover:bg-blue-50 rounded-lg transition-colors">
                    {{ __('عرض') }}
                </a>
                @endcan
            </div>
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 px-4 py-12">
            <div class="flex flex-col items-center">
                <svg class="w-12 h-12 text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"/>
                </svg>
                <p class="text-gray-500 text-sm">{{ __('لا توجد محافظ') }}</p>
            </div>
        </div>
        @endforelse

        @if($wallets->hasPages())
        <div class="mt-4">
            {{ $wallets->links() }}
        </div>
        @endif
    </div>
</div>
