<div>
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('الفواتير') }}</h1>
        <div class="flex items-center gap-2">
            <a href="{{ route('export.invoices', array_filter(['status' => $status ?? '', 'search' => $search ?? ''])) }}"
               class="inline-flex items-center gap-2 px-3 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 text-sm font-medium">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
                {{ __('تصدير CSV') }}
            </a>
            @can('invoices.create')
            <a href="{{ route('invoices.wizard') }}" wire:navigate
               class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
                {{ __('إنشاء فاتورة') }}
            </a>
            @endcan
        </div>
    </div>

    {{-- Flash Messages --}}
    @if(session('success'))
    <div class="mb-4 p-4 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-4 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm">
        {{ session('error') }}
    </div>
    @endif

    {{-- 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-3 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="status" 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($statusOptions as $value => $label)
                <option value="{{ $value }}">{{ $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>
                        <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-center 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-center 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-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($invoices as $invoice)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-mono text-gray-600" dir="ltr">
                            {{ $invoice->number ?? '—' }}
                        </td>
                        <td class="px-4 py-3">
                            @php
                                $displayName = $invoice->contact_name
                                    ?? $invoice->billable?->person?->name_ar
                                    ?? $invoice->billable?->name_ar
                                    ?? '—';
                            @endphp
                            @if($invoice->billable_type === 'App\\Domain\\Participant\\Models\\Participant' && $invoice->billable)
                            <a href="{{ route('participants.show', $invoice->billable) }}" wire:navigate class="text-blue-600 hover:text-blue-800 hover:underline font-medium">{{ $displayName }}</a>
                            @else
                            <span class="font-medium text-gray-800">{{ $displayName }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center font-medium" dir="ltr">
                            {{ number_format($invoice->total_amount / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center text-green-600" dir="ltr">
                            {{ number_format($invoice->paid_amount / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center text-red-600" dir="ltr">
                            {{ number_format($invoice->due_amount / 100, 2) }} {{ __('ج.م') }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusValue = $invoice->status->value ?? $invoice->status;
                                $statusColors = [
                                    'draft' => 'gray',
                                    'pending' => 'yellow',
                                    'awaiting_approval' => 'amber',
                                    'sent' => 'blue',
                                    'partially_paid' => 'amber',
                                    'paid' => 'green',
                                    'overpaid' => 'emerald',
                                    'overdue' => 'red',
                                    'cancelled' => 'gray',
                                    'refunded' => 'purple',
                                    'partially_refunded' => 'violet',
                                    'written_off' => 'slate',
                                    'disputed' => 'orange',
                                ];
                                $color = $statusColors[$statusValue] ?? 'gray';
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                                {{ $invoice->status->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                            {{ $invoice->issue_date?->format('Y-m-d') }}
                        </td>
                        <td class="px-4 py-3 text-center space-x-2 space-x-reverse">
                            @can('invoices.view')
                            <a href="{{ route('invoices.show', $invoice) }}" wire:navigate
                               class="text-blue-600 hover:text-blue-800 text-sm">{{ __('عرض') }}</a>
                            @endcan
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" 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 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد فواتير') }}</p>
                                @can('invoices.create')
                                <a href="{{ route('invoices.wizard') }}" wire:navigate
                                   class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium">
                                    {{ __('إنشاء أول فاتورة') }}
                                </a>
                                @endcan
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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