<div class="space-y-5">
    <header class="flex flex-wrap items-center justify-between gap-3">
        <div>
            <h1 class="text-xl font-bold text-gray-900">{{ __('طلبات الأعضاء') }}</h1>
            <p class="mt-0.5 text-sm text-gray-500">
                {{ __('التجميد والنقل والإلغاء والأعذار في قائمة واحدة. الموافقة تنفّذ الطلب فعلياً، ولا تغيّر حالته فقط.') }}
            </p>
        </div>
        @if($pendingCount > 0)
            <span class="rounded-full border border-amber-200 bg-amber-50 px-3 py-1.5 text-sm font-bold text-amber-700">
                {{ $pendingCount }} {{ __('قيد المراجعة') }}
            </span>
        @endif
    </header>

    <div class="flex flex-wrap gap-3">
        <select wire:model.live="status" class="rounded-lg border-gray-300 text-sm">
            <option value="pending">{{ __('قيد المراجعة') }}</option>
            <option value="approved">{{ __('تمت الموافقة') }}</option>
            <option value="rejected">{{ __('مرفوض') }}</option>
            <option value="">{{ __('كل الحالات') }}</option>
        </select>
        <select wire:model.live="type" class="rounded-lg border-gray-300 text-sm">
            <option value="">{{ __('كل الأنواع') }}</option>
            @foreach($types as $t)
                <option value="{{ $t }}">{{ $t }}</option>
            @endforeach
        </select>
    </div>

    @if($handling)
        <section class="rounded-xl border-2 border-blue-200 bg-blue-50/40 p-4 sm:p-5">
            <h2 class="font-bold text-gray-900">{{ $handling->typeLabel() }}</h2>
            <dl class="mt-3 grid grid-cols-2 gap-3 text-sm sm:grid-cols-4">
                <div>
                    <dt class="text-xs text-gray-500">{{ __('العضو') }}</dt>
                    <dd class="font-semibold">{{ $handling->participant?->person?->name_ar ?? '—' }}</dd>
                </div>
                @if($handling->trainingSession)
                    <div>
                        <dt class="text-xs text-gray-500">{{ __('الحصة') }}</dt>
                        <dd class="font-semibold">
                            {{ $handling->trainingSession->session_date?->translatedFormat('j M') }} —
                            {{ $handling->trainingSession->group?->name_ar }}
                        </dd>
                    </div>
                @endif
                <div>
                    <dt class="text-xs text-gray-500">{{ __('أُرسل') }}</dt>
                    <dd class="text-xs">{{ $handling->created_at?->diffForHumans() }}</dd>
                </div>
                @if($handling->attachment_path)
                    <div>
                        <dt class="text-xs text-gray-500">{{ __('المرفق') }}</dt>
                        <dd>
                            <a href="{{ route('requests.attachment', $handling->uuid) }}"
                               class="text-xs font-semibold text-blue-700 hover:underline">{{ __('تنزيل') }}</a>
                        </dd>
                    </div>
                @endif
            </dl>

            @if($handling->reason)
                <p class="mt-3 rounded-lg border border-gray-200 bg-white px-3 py-2 text-sm text-gray-700">
                    {{ $handling->reason }}
                </p>
            @endif

            <div class="mt-4">
                <label for="notes" class="block text-sm font-semibold text-gray-900">{{ __('ملاحظات للعضو') }}</label>
                <textarea id="notes" rows="2" wire:model="notes"
                          class="mt-1 w-full rounded-lg border-gray-300 text-sm"></textarea>
                @error('notes') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
            </div>

            <div class="mt-4 flex flex-wrap gap-3">
                <button type="button" wire:click="approve" wire:loading.attr="disabled" wire:target="approve"
                        class="rounded-xl bg-emerald-600 px-5 py-2.5 text-sm font-bold text-white hover:bg-emerald-700 disabled:opacity-60">
                    <span wire:loading.remove wire:target="approve">{{ __('موافقة وتنفيذ') }}</span>
                    <span wire:loading wire:target="approve">{{ __('جارٍ التنفيذ...') }}</span>
                </button>
                <button type="button" wire:click="reject" wire:loading.attr="disabled" wire:target="reject"
                        class="rounded-xl border border-red-300 bg-white px-4 py-2.5 text-sm font-bold text-red-700 hover:bg-red-50">
                    {{ __('رفض') }}
                </button>
                <button type="button" wire:click="cancelHandling"
                        class="rounded-xl px-4 py-2.5 text-sm font-medium text-gray-500">{{ __('إغلاق') }}</button>
            </div>
        </section>
    @endif

    <div class="overflow-hidden rounded-xl border border-gray-200 bg-white">
        <table class="min-w-full divide-y divide-gray-200 text-sm">
            <thead class="bg-gray-50 text-xs text-gray-500">
                <tr>
                    <th class="px-4 py-3 text-start font-medium">{{ __('النوع') }}</th>
                    <th class="px-4 py-3 text-start font-medium">{{ __('العضو') }}</th>
                    <th class="px-4 py-3 text-start font-medium">{{ __('السبب') }}</th>
                    <th class="px-4 py-3 text-start font-medium">{{ __('الحالة') }}</th>
                    <th class="px-4 py-3 text-end font-medium">{{ __('إجراء') }}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                @forelse($requests as $request)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-medium text-gray-900">{{ $request->typeLabel() }}</td>
                        <td class="px-4 py-3">{{ $request->participant?->person?->name_ar ?? '—' }}</td>
                        <td class="max-w-xs truncate px-4 py-3 text-gray-600">{{ $request->reason }}</td>
                        <td class="px-4 py-3">
                            <span class="text-xs">{{ $request->statusLabel() }}</span>
                            @if($request->effect_error)
                                <span class="block text-[11px] text-red-600">{{ $request->effect_error }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-end">
                            @if($request->isOpen())
                                <button type="button" wire:click="handle({{ $request->id }})"
                                        class="rounded-lg bg-blue-600 px-3 py-1.5 text-xs font-bold text-white hover:bg-blue-700">
                                    {{ __('معالجة') }}
                                </button>
                            @else
                                <span class="text-xs text-gray-400">{{ $request->handled_at?->diffForHumans() }}</span>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="px-4 py-10 text-center text-gray-500">{{ __('لا توجد طلبات') }}</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div>{{ $requests->links() }}</div>
</div>
