<div>
    <div class="flex items-center justify-between mb-6">
        <div class="flex items-center gap-3">
            <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center">
                <svg class="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/></svg>
            </div>
            <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('سجل رسائل الواتساب') }}</h1>
        </div>
        <a href="{{ route('whatsapp.send') }}" wire:navigate
           class="inline-flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 text-sm font-medium">
            <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24"><path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/></svg>
            {{ __('رسالة جديدة') }}
        </a>
    </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-4 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-green-500 focus:border-green-500">
            </div>
            <select wire:model.live="status" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500">
                <option value="">{{ __('كل الحالات') }}</option>
                @foreach($statuses as $statusOption)
                    <option value="{{ $statusOption->value }}">{{ $statusOption->label() }}</option>
                @endforeach
            </select>
            <select wire:model.live="type" class="px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500">
                <option value="">{{ __('كل الأنواع') }}</option>
                @foreach($types as $typeOption)
                    <option value="{{ $typeOption->value }}">{{ $typeOption->label() }}</option>
                @endforeach
            </select>
        </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>
                        <x-ui.sort-header column="phone_number" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('الرقم') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">{{ __('المحتوى') }}</th>
                        <x-ui.sort-header column="type" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('النوع') }}</x-ui.sort-header>
                        <x-ui.sort-header column="status" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الحالة') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('المرسل') }}</th>
                        <x-ui.sort-header column="created_at" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('التاريخ') }}</x-ui.sort-header>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($messages as $msg)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-mono text-xs text-gray-700" dir="ltr">
                            {{ $msg->phone_number }}
                        </td>
                        <td class="px-4 py-3 text-gray-700 max-w-xs truncate">
                            {{ Str::limit($msg->content, 60) }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            <span class="px-2 py-0.5 text-xs rounded-full bg-gray-100 text-gray-600">
                                {{ $msg->type->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusColor = match($msg->status) {
                                    \App\Domain\WhatsApp\Enums\MessageStatus::Sent => 'bg-blue-100 text-blue-700',
                                    \App\Domain\WhatsApp\Enums\MessageStatus::Delivered => 'bg-indigo-100 text-indigo-700',
                                    \App\Domain\WhatsApp\Enums\MessageStatus::Read => 'bg-green-100 text-green-700',
                                    \App\Domain\WhatsApp\Enums\MessageStatus::Failed => 'bg-red-100 text-red-700',
                                    default => 'bg-gray-100 text-gray-700',
                                };
                            @endphp
                            <span class="px-2 py-0.5 text-xs rounded-full {{ $statusColor }}">
                                {{ $msg->status->label() }}
                            </span>
                            @if($msg->error_message)
                                <p class="text-xs text-red-500 mt-1 max-w-[150px] truncate" title="{{ $msg->error_message }}">
                                    {{ $msg->error_message }}
                                </p>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center text-gray-500 text-xs">
                            {{ $msg->sender?->name ?? '-' }}
                        </td>
                        <td class="px-4 py-3 text-center text-gray-500 text-xs" dir="ltr">
                            {{ $msg->created_at->format('Y-m-d H:i') }}
                        </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="currentColor" viewBox="0 0 24 24"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347z"/></svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد رسائل بعد') }}</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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