<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-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-blue-500 focus:border-blue-500">
            </div>
            <select wire:model.live="actionFilter" 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>
                <option value="created">{{ __('إنشاء') }}</option>
                <option value="updated">{{ __('تعديل') }}</option>
                <option value="deleted">{{ __('حذف') }}</option>
                <option value="restored">{{ __('استعادة') }}</option>
            </select>
            <div class="flex items-center 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">
                <span class="text-gray-400 text-sm">-</span>
                <input type="date" wire:model.live="dateTo"
                       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>

    {{-- 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-start 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($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->user?->name_ar ?? $log->user?->name ?? __('نظام') }}</span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $actionColors = [
                                    'created' => 'bg-green-100 text-green-700',
                                    'updated' => 'bg-blue-100 text-blue-700',
                                    'deleted' => 'bg-red-100 text-red-700',
                                    'restored' => 'bg-purple-100 text-purple-700',
                                ];
                                $actionLabels = [
                                    'created' => 'إنشاء',
                                    'updated' => 'تعديل',
                                    'deleted' => 'حذف',
                                    'restored' => 'استعادة',
                                ];
                                $aColor = $actionColors[$log->action] ?? 'bg-gray-100 text-gray-700';
                                $aLabel = $actionLabels[$log->action] ?? $log->action;
                            @endphp
                            <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium {{ $aColor }}">
                                {{ __($aLabel) }}
                            </span>
                        </td>
                        <td class="px-4 py-3">
                            @php
                                $shortType = class_basename($log->auditable_type ?? '');
                            @endphp
                            <span class="text-sm text-gray-700">{{ $shortType }}</span>
                            <span class="text-xs text-gray-400 ms-1" dir="ltr">#{{ $log->auditable_id }}</span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-600 max-w-xs">
                            @if($log->action === 'created' && $log->new_values)
                                @php $keys = array_slice(array_keys($log->new_values), 0, 3); @endphp
                                <span class="text-xs">{{ implode(', ', $keys) }}{{ count($log->new_values) > 3 ? ' +' . (count($log->new_values) - 3) : '' }}</span>
                            @elseif($log->action === 'updated' && $log->new_values)
                                @php $keys = array_slice(array_keys($log->new_values), 0, 3); @endphp
                                <span class="text-xs">{{ implode(', ', $keys) }}{{ count($log->new_values) > 3 ? ' +' . (count($log->new_values) - 3) : '' }}</span>
                            @elseif($log->action === 'deleted')
                                <span class="text-xs text-red-500">{{ __('حذف') }}</span>
                            @else
                                <span class="text-xs text-gray-400">—</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            <button wire:click="toggleExpand({{ $log->id }})"
                                    class="text-blue-600 hover:text-blue-800 text-xs font-medium">
                                {{ $expandedRow === $log->id ? __('إخفاء') : __('عرض') }}
                            </button>
                        </td>
                    </tr>

                    {{-- Expanded Details Row --}}
                    @if($expandedRow === $log->id)
                    <tr class="bg-gray-50">
                        <td colspan="6" class="px-4 py-4">
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4" x-data="{ activeTab: 'changes' }">
                                {{-- Tabs --}}
                                <div class="md:col-span-2 flex gap-2 mb-2">
                                    <button @click="activeTab = 'changes'"
                                            :class="activeTab === 'changes' ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-600'"
                                            class="px-3 py-1 rounded text-xs font-medium">
                                        {{ __('التغييرات') }}
                                    </button>
                                    <button @click="activeTab = 'meta'"
                                            :class="activeTab === 'meta' ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-600'"
                                            class="px-3 py-1 rounded text-xs font-medium">
                                        {{ __('بيانات وصفية') }}
                                    </button>
                                </div>

                                {{-- Changes Tab --}}
                                <div x-show="activeTab === 'changes'" class="md:col-span-2">
                                    @if($log->action === 'updated' && $log->old_values && $log->new_values)
                                    <div class="overflow-x-auto">
                                        <table class="w-full text-xs border border-gray-200 rounded">
                                            <thead class="bg-gray-100">
                                                <tr>
                                                    <th class="px-3 py-2 text-start text-gray-600">{{ __('الحقل') }}</th>
                                                    <th class="px-3 py-2 text-start text-gray-600">{{ __('القيمة القديمة') }}</th>
                                                    <th class="px-3 py-2 text-start text-gray-600">{{ __('القيمة الجديدة') }}</th>
                                                </tr>
                                            </thead>
                                            <tbody class="divide-y divide-gray-100">
                                                @foreach($log->new_values as $field => $newVal)
                                                <tr>
                                                    <td class="px-3 py-2 font-medium text-gray-700">{{ $field }}</td>
                                                    <td class="px-3 py-2 text-red-600 bg-red-50">{{ is_array($log->old_values[$field] ?? null) ? json_encode($log->old_values[$field], JSON_UNESCAPED_UNICODE) : ($log->old_values[$field] ?? '—') }}</td>
                                                    <td class="px-3 py-2 text-green-600 bg-green-50">{{ is_array($newVal) ? json_encode($newVal, JSON_UNESCAPED_UNICODE) : $newVal }}</td>
                                                </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                    @elseif($log->action === 'created' && $log->new_values)
                                    <div class="overflow-x-auto">
                                        <table class="w-full text-xs border border-gray-200 rounded">
                                            <thead class="bg-gray-100">
                                                <tr>
                                                    <th class="px-3 py-2 text-start text-gray-600">{{ __('الحقل') }}</th>
                                                    <th class="px-3 py-2 text-start text-gray-600">{{ __('القيمة') }}</th>
                                                </tr>
                                            </thead>
                                            <tbody class="divide-y divide-gray-100">
                                                @foreach($log->new_values as $field => $val)
                                                <tr>
                                                    <td class="px-3 py-2 font-medium text-gray-700">{{ $field }}</td>
                                                    <td class="px-3 py-2 text-green-600 bg-green-50">{{ is_array($val) ? json_encode($val, JSON_UNESCAPED_UNICODE) : $val }}</td>
                                                </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                    @elseif($log->action === 'deleted' && $log->old_values)
                                    <div class="overflow-x-auto">
                                        <table class="w-full text-xs border border-gray-200 rounded">
                                            <thead class="bg-gray-100">
                                                <tr>
                                                    <th class="px-3 py-2 text-start text-gray-600">{{ __('الحقل') }}</th>
                                                    <th class="px-3 py-2 text-start text-gray-600">{{ __('القيمة المحذوفة') }}</th>
                                                </tr>
                                            </thead>
                                            <tbody class="divide-y divide-gray-100">
                                                @foreach($log->old_values as $field => $val)
                                                <tr>
                                                    <td class="px-3 py-2 font-medium text-gray-700">{{ $field }}</td>
                                                    <td class="px-3 py-2 text-red-600 bg-red-50">{{ is_array($val) ? json_encode($val, JSON_UNESCAPED_UNICODE) : $val }}</td>
                                                </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                    @else
                                    <p class="text-xs text-gray-400">{{ __('لا توجد بيانات تغييرات') }}</p>
                                    @endif
                                </div>

                                {{-- Meta Tab --}}
                                <div x-show="activeTab === 'meta'" class="md:col-span-2">
                                    <div class="grid grid-cols-2 md:grid-cols-4 gap-3 text-xs">
                                        <div>
                                            <span class="text-gray-500">{{ __('عنوان IP') }}:</span>
                                            <p class="font-mono text-gray-700 mt-0.5" dir="ltr">{{ $log->ip_address ?? '—' }}</p>
                                        </div>
                                        <div>
                                            <span class="text-gray-500">{{ __('الرابط') }}:</span>
                                            <p class="font-mono text-gray-700 mt-0.5 truncate" dir="ltr">{{ $log->url ?? '—' }}</p>
                                        </div>
                                        <div>
                                            <span class="text-gray-500">{{ __('النوع الكامل') }}:</span>
                                            <p class="font-mono text-gray-700 mt-0.5 truncate" dir="ltr">{{ $log->auditable_type }}</p>
                                        </div>
                                        <div>
                                            <span class="text-gray-500">{{ __('مالي') }}:</span>
                                            <p class="text-gray-700 mt-0.5">{{ $log->is_financial ? __('نعم') : __('لا') }}</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                    @endif
                    @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="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </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>
