<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('سجل الإشعارات') }}</h1>
    </div>

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 mb-4">
        <div class="grid grid-cols-1 sm:grid-cols-5 gap-4">
            <div class="sm:col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بنوع الحدث أو الموضوع...') }}"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
            </div>
            <select wire:model.live="channelFilter" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل القنوات') }}</option>
                @foreach($channels as $channel)
                <option value="{{ $channel->value }}">{{ $channel->label() }}</option>
                @endforeach
            </select>
            <select wire:model.live="statusFilter" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل الحالات') }}</option>
                @foreach($statuses as $status)
                <option value="{{ $status->value }}">{{ $status->label() }}</option>
                @endforeach
            </select>
            <div class="flex gap-2">
                <input type="date" wire:model.live="dateFrom"
                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm" dir="ltr">
            </div>
        </div>
        <div class="mt-3 flex gap-2">
            <div class="flex items-center gap-2 text-sm text-gray-500">
                <span>{{ __('من') }}:</span>
                <input type="date" wire:model.live="dateFrom"
                       class="px-3 py-1.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm" dir="ltr">
                <span>{{ __('إلى') }}:</span>
                <input type="date" wire:model.live="dateTo"
                       class="px-3 py-1.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm" dir="ltr">
            </div>
        </div>
    </div>

    {{-- Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div 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-center 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-center font-medium text-gray-600">{{ __('الحالة') }}</th>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('الموضوع') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($logs as $log)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 text-xs text-gray-500 whitespace-nowrap" dir="ltr">
                            {{ $log->created_at->format('Y-m-d H:i') }}
                        </td>
                        <td class="px-4 py-3">
                            <span class="text-sm text-gray-800">{{ $log->event_type }}</span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $channelColors = [
                                    'in_app' => 'bg-blue-100 text-blue-700',
                                    'email' => 'bg-green-100 text-green-700',
                                    'sms' => 'bg-amber-100 text-amber-700',
                                ];
                                $color = $channelColors[$log->channel->value] ?? 'bg-gray-100 text-gray-700';
                            @endphp
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium {{ $color }}">
                                {{ $log->channel->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-600">
                            <span class="text-xs text-gray-400">{{ $log->recipient_type }}:</span>
                            {{ $log->recipient_id }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusColors = [
                                    'sent' => 'bg-green-100 text-green-700',
                                    'failed' => 'bg-red-100 text-red-700',
                                    'queued' => 'bg-gray-100 text-gray-600',
                                    'read' => 'bg-blue-100 text-blue-700',
                                ];
                                $sColor = $statusColors[$log->status->value] ?? 'bg-gray-100 text-gray-700';
                            @endphp
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium {{ $sColor }}">
                                {{ $log->status->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-600 max-w-xs truncate">
                            {{ $log->subject ?? \Illuminate\Support\Str::limit($log->body, 40) }}
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" 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="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد سجلات إشعارات') }}</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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