<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>
        <a href="{{ route('participants.list') }}" wire:navigate class="text-sm text-blue-600 hover:text-blue-800">{{ __('العودة') }}</a>
    </div>

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

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6 mb-4">
        <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-3 sm:gap-4 items-end">
            <div>
                <label class="block text-sm text-gray-600 mb-1">{{ __('الحالة الحالية') }}</label>
                <select wire:model.live="currentFilter" class="w-full rounded-lg border-gray-300 text-sm py-2.5">
                    <option value="active">{{ __('active') }}</option>
                    <option value="frozen">{{ __('frozen') }}</option>
                    <option value="suspended">{{ __('suspended') }}</option>
                    <option value="inactive">{{ __('inactive') }}</option>
                </select>
            </div>
            <div>
                <label class="block text-sm text-gray-600 mb-1">{{ __('التغيير إلى') }}</label>
                <select wire:model="targetStatus" class="w-full rounded-lg border-gray-300 text-sm py-2.5">
                    <option value="">{{ __('اختر') }}</option>
                    @foreach($availableTargets as $target)
                    <option value="{{ $target }}">{{ __($target) }}</option>
                    @endforeach
                </select>
            </div>
            <div class="flex gap-2 col-span-2 md:col-span-1">
                <button wire:click="selectAll" class="flex-1 md:flex-none px-3 py-2.5 text-xs bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200">
                    {{ __('تحديد الكل') }}
                </button>
                <button wire:click="deselectAll" class="flex-1 md:flex-none px-3 py-2.5 text-xs bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200">
                    {{ __('إلغاء التحديد') }}
                </button>
            </div>
            <div class="col-span-2 md:col-span-3 lg:col-span-2">
                <button wire:click="openConfirm" @disabled(empty($selectedIds) || empty($targetStatus))
                        class="w-full sm:w-auto px-4 py-2.5 text-sm bg-red-600 text-white rounded-lg hover:bg-red-700 disabled:opacity-50">
                    {{ __('تنفيذ') }} ({{ count($selectedIds) }})
                </button>
            </div>
        </div>
    </div>

    {{-- Desktop Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden hidden md:block">
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500 w-10"></th>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('الاسم') }}</th>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('الحالة') }}</th>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('تاريخ التسجيل') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($participants as $p)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <input type="checkbox" wire:model="selectedIds" value="{{ $p->id }}"
                                   class="w-4 h-4 text-blue-600 rounded border-gray-300">
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-800">{{ $p->full_name }}</td>
                        <td class="px-4 py-3">
                            <span class="px-2 py-0.5 text-xs rounded-full bg-gray-100 text-gray-600">{{ __($p->status) }}</span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-500" dir="ltr">{{ $p->created_at?->format('Y-m-d') }}</td>
                    </tr>
                    @empty
                    <tr><td colspan="4" class="px-4 py-8 text-center text-sm text-gray-500">{{ __('لا يوجد مشتركين') }}</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- Mobile Cards --}}
    <div class="md:hidden space-y-3">
        @forelse($participants as $p)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <div class="flex items-center gap-3">
                <input type="checkbox" wire:model="selectedIds" value="{{ $p->id }}"
                       class="w-4 h-4 text-blue-600 rounded border-gray-300 shrink-0">
                <div class="flex-1 min-w-0">
                    <p class="text-sm font-medium text-gray-800 truncate">{{ $p->full_name }}</p>
                    <div class="flex items-center gap-2 mt-1">
                        <span class="px-2 py-0.5 text-xs rounded-full bg-gray-100 text-gray-600">{{ __($p->status) }}</span>
                        <span class="text-xs text-gray-500" dir="ltr">{{ $p->created_at?->format('Y-m-d') }}</span>
                    </div>
                </div>
            </div>
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-8 text-center text-sm text-gray-500">
            {{ __('لا يوجد مشتركين') }}
        </div>
        @endforelse
    </div>

    @if($confirmModal)
    <div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4">
        <div class="bg-white rounded-xl shadow-xl w-full max-w-md p-4 sm:p-6">
            <h3 class="text-base sm:text-lg font-bold text-gray-800 mb-2">{{ __('تأكيد التغيير') }}</h3>
            <p class="text-sm text-gray-600 mb-4">
                {{ __('سيتم تغيير حالة :count مشترك من :from إلى :to', [
                    'count' => count($selectedIds),
                    'from' => __($currentFilter),
                    'to' => __($targetStatus),
                ]) }}
            </p>
            <div class="mb-4">
                <label class="block text-sm text-gray-600 mb-1">{{ __('السبب') }} *</label>
                <textarea wire:model="reason" rows="2" class="w-full rounded-lg border-gray-300 text-sm py-2.5"></textarea>
                @error('reason') <p class="text-xs text-red-500 mt-1">{{ $message }}</p> @enderror
            </div>
            <div class="flex flex-col-reverse sm:flex-row gap-3 sm:justify-end">
                <button wire:click="$set('confirmModal', false)" class="w-full sm:w-auto px-4 py-2.5 text-sm text-gray-600 hover:text-gray-800">{{ __('إلغاء') }}</button>
                <button wire:click="execute" class="w-full sm:w-auto px-4 py-2.5 text-sm text-white bg-red-600 rounded-lg hover:bg-red-700">{{ __('تأكيد') }}</button>
            </div>
        </div>
    </div>
    @endif
</div>
