<div>
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('فاتورة') }} #{{ $invoice->number }}</h1>
        <div class="flex items-center gap-3">
            <a href="{{ route('invoices.print', $invoice) }}" target="_blank"
               class="inline-flex items-center gap-1.5 text-sm font-medium text-green-600 hover:text-green-800 active:text-green-900 transition-colors">
                <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="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"/></svg>
                {{ __('طباعة') }}
            </a>
            <a href="{{ route('invoices.list') }}" wire:navigate
               class="text-gray-600 hover:text-gray-800 text-sm font-medium">
                {{ __('العودة للفواتير') }}
            </a>
        </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

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-4 sm:gap-6">
        {{-- Main Content --}}
        <div class="lg:col-span-2 space-y-6">
            {{-- Invoice Header --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
                <div class="flex flex-col sm:flex-row sm:items-start justify-between gap-3 mb-4">
                    <div>
                        <p class="text-sm text-gray-500">{{ __('رقم الفاتورة') }}</p>
                        <p class="text-lg font-bold text-gray-800" dir="ltr">{{ $invoice->number }}</p>
                    </div>
                    @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-3 py-1 text-sm font-medium bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                        {{ $invoice->status->label() }}
                    </span>
                </div>

                <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 text-sm">
                    <div>
                        <p class="text-gray-500">{{ __('العميل') }}</p>
                        @php
                            $clientName = $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">{{ $clientName }}</a>
                        @else
                        <p class="font-medium text-gray-800">{{ $clientName }}</p>
                        @endif
                        @if($invoice->contact_phone || $invoice->billable?->person?->phone)
                        <p class="text-gray-500" dir="ltr">{{ $invoice->contact_phone ?? $invoice->billable?->person?->phone }}</p>
                        @endif
                    </div>
                    <div>
                        <p class="text-gray-500">{{ __('تاريخ الإصدار') }}</p>
                        <p class="font-medium text-gray-800" dir="ltr">{{ $invoice->issue_date?->format('Y-m-d') }}</p>
                    </div>
                    <div>
                        <p class="text-gray-500">{{ __('تاريخ الاستحقاق') }}</p>
                        <p class="font-medium text-gray-800" dir="ltr">{{ $invoice->due_date?->format('Y-m-d') }}</p>
                    </div>
                    <div>
                        <p class="text-gray-500">{{ __('أنشأها') }}</p>
                        <p class="font-medium text-gray-800">{{ $invoice->creator?->name_ar ?? $invoice->creator?->name ?? '—' }}</p>
                    </div>
                </div>

                @if($invoice->notes)
                <div class="mt-4 pt-4 border-t border-gray-100">
                    <p class="text-sm text-gray-500">{{ __('ملاحظات') }}</p>
                    <p class="text-sm text-gray-700 mt-1">{{ $invoice->notes }}</p>
                </div>
                @endif
            </div>

            {{-- Items Table --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden overflow-x-auto">
                <div class="px-4 sm:px-6 py-3 sm:py-4 border-b border-gray-200">
                    <h2 class="text-lg font-semibold text-gray-800">{{ __('عناصر الفاتورة') }}</h2>
                </div>
                <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-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-end font-medium text-gray-600">{{ __('الإجمالي') }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        @foreach($invoice->items as $item)
                        <tr>
                            <td class="px-4 py-3 text-gray-800">{{ $item->description }}</td>
                            <td class="px-4 py-3 text-center text-gray-600" dir="ltr">{{ $item->quantity }}</td>
                            <td class="px-4 py-3 text-center text-gray-600" dir="ltr">{{ number_format($item->unit_price / 100, 2) }}</td>
                            <td class="px-4 py-3 text-center text-gray-600" dir="ltr">{{ number_format($item->discount_amount / 100, 2) }}</td>
                            <td class="px-4 py-3 text-end font-medium text-gray-800" dir="ltr">{{ number_format($item->total_amount / 100, 2) }} {{ __('ج.م') }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                    <tfoot class="bg-gray-50 border-t border-gray-200">
                        <tr>
                            <td colspan="4" class="px-4 py-2 text-end text-sm text-gray-600">{{ __('المجموع الفرعي') }}</td>
                            <td class="px-4 py-2 text-end font-medium" dir="ltr">{{ number_format($invoice->subtotal_amount / 100, 2) }} {{ __('ج.م') }}</td>
                        </tr>
                        @if($invoice->discount_amount > 0)
                        <tr>
                            <td colspan="4" class="px-4 py-2 text-end text-sm text-red-600">{{ __('الخصم') }}</td>
                            <td class="px-4 py-2 text-end font-medium text-red-600" dir="ltr">-{{ number_format($invoice->discount_amount / 100, 2) }} {{ __('ج.م') }}</td>
                        </tr>
                        @endif
                        @if($invoice->tax_amount > 0)
                        <tr>
                            <td colspan="4" class="px-4 py-2 text-end text-sm text-gray-600">{{ __('الضريبة') }}</td>
                            <td class="px-4 py-2 text-end font-medium" dir="ltr">{{ number_format($invoice->tax_amount / 100, 2) }} {{ __('ج.م') }}</td>
                        </tr>
                        @endif
                        @if(($invoice->service_fee_amount ?? 0) > 0)
                        <tr>
                            <td colspan="4" class="px-4 py-2 text-end text-sm text-gray-600">{{ __('مصاريف خدمة') }}</td>
                            <td class="px-4 py-2 text-end font-medium" dir="ltr">{{ number_format($invoice->service_fee_amount / 100, 2) }} {{ __('ج.م') }}</td>
                        </tr>
                        @endif
                        <tr class="border-t border-gray-300">
                            <td colspan="4" class="px-4 py-3 text-end text-base font-bold text-gray-800">{{ __('الإجمالي') }}</td>
                            <td class="px-4 py-3 text-end text-base font-bold text-blue-600" dir="ltr">{{ number_format($invoice->total_amount / 100, 2) }} {{ __('ج.م') }}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>

            {{-- Payments History --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden overflow-x-auto">
                <div class="px-4 sm:px-6 py-3 sm:py-4 border-b border-gray-200">
                    <h2 class="text-lg font-semibold text-gray-800">{{ __('سجل المدفوعات') }}</h2>
                </div>
                @if($invoice->payments->count() > 0)
                <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-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">
                        @foreach($invoice->payments as $payment)
                        @php
                            $methodColors = [
                                'cash' => 'green',
                                'card' => 'blue',
                                'bank_transfer' => 'indigo',
                                'wallet' => 'purple',
                                'online' => 'cyan',
                                'cheque' => 'amber',
                                'other' => 'gray',
                            ];
                            $methodValue = $payment->method->value ?? $payment->method;
                            $mColor = $methodColors[$methodValue] ?? 'gray';
                            $pStatusColors = [
                                'confirmed' => 'green',
                                'pending' => 'yellow',
                                'failed' => 'red',
                                'cancelled' => 'gray',
                                'refunded' => 'purple',
                            ];
                            $pStatusValue = $payment->status->value ?? $payment->status;
                            $pColor = $pStatusColors[$pStatusValue] ?? 'gray';
                            $isEditing = $editingPaymentId === $payment->uuid;
                        @endphp
                        <tr class="{{ $isEditing ? 'bg-amber-50' : '' }}">
                            <td class="px-4 py-3 font-mono text-gray-600 text-xs" dir="ltr">{{ $payment->reference }}</td>
                            <td class="px-4 py-3 text-center">
                                @if($isEditing)
                                    <select wire:model="editingPaymentMethod"
                                            class="text-xs border border-amber-300 rounded-md px-2 py-1 focus:ring-2 focus:ring-amber-400 focus:border-amber-400 bg-white">
                                        @foreach($paymentMethods as $val => $lbl)
                                            <option value="{{ $val }}">{{ $lbl }}</option>
                                        @endforeach
                                    </select>
                                    @error('editingPaymentMethod') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                                @else
                                    <span class="px-2 py-0.5 text-xs bg-{{ $mColor }}-100 text-{{ $mColor }}-700 rounded-full">
                                        {{ $payment->method->label() }}
                                    </span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center font-medium" dir="ltr">
                                {{ number_format($payment->amount / 100, 2) }} {{ __('ج.م') }}
                            </td>
                            <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                                {{ $payment->payment_date?->format('Y-m-d') ?? $payment->created_at?->format('Y-m-d') }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span class="px-2 py-0.5 text-xs bg-{{ $pColor }}-100 text-{{ $pColor }}-700 rounded-full">
                                    {{ $payment->status->label() }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-center">
                                <div class="flex items-center justify-center gap-2">
                                    <x-ui.print-receipt-button :route="route('receipts.payment.print', $payment->uuid)" size="sm" />
                                    @if($pStatusValue === 'confirmed' && $payment->direction === 'inbound')
                                        @can('refunds.initiate')
                                        <button type="button"
                                                wire:click="refundPayment('{{ $payment->uuid }}')"
                                                wire:confirm="{{ __('هل أنت متأكد من استرداد هذه الدفعة؟ لا يمكن التراجع عن هذا الإجراء.') }}"
                                                wire:loading.attr="disabled"
                                                wire:target="refundPayment('{{ $payment->uuid }}')"
                                                title="{{ __('استرداد') }}"
                                                class="text-xs px-2 py-1 bg-purple-50 text-purple-600 border border-purple-200 rounded hover:bg-purple-100 transition-colors disabled:opacity-50">
                                            <span wire:loading.remove wire:target="refundPayment('{{ $payment->uuid }}')">{{ __('استرداد') }}</span>
                                            <span wire:loading wire:target="refundPayment('{{ $payment->uuid }}')">...</span>
                                        </button>
                                        @endcan
                                    @endif
                                    @if(auth()->user()?->is_super_admin)
                                        @if($isEditing)
                                            <button type="button" wire:click="savePaymentMethod"
                                                    wire:loading.attr="disabled" wire:target="savePaymentMethod"
                                                    class="text-xs px-2 py-1 bg-green-600 text-white rounded hover:bg-green-700 disabled:opacity-50 transition-colors">
                                                <span wire:loading.remove wire:target="savePaymentMethod">{{ __('حفظ') }}</span>
                                                <span wire:loading wire:target="savePaymentMethod">...</span>
                                            </button>
                                            <button type="button" wire:click="cancelEditPaymentMethod"
                                                    class="text-xs px-2 py-1 bg-gray-200 text-gray-700 rounded hover:bg-gray-300 transition-colors">
                                                {{ __('إلغاء') }}
                                            </button>
                                        @else
                                            <button type="button" wire:click="startEditPaymentMethod('{{ $payment->uuid }}')"
                                                    title="{{ __('تعديل طريقة الدفع') }}"
                                                    class="text-xs text-amber-600 hover:text-amber-800 transition-colors">
                                                <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="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                                                </svg>
                                            </button>
                                        @endif
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
                @else
                <div class="px-6 py-8 text-center">
                    <svg class="w-10 h-10 text-gray-300 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"/>
                    </svg>
                    <p class="text-gray-500 text-sm">{{ __('لا توجد مدفوعات مسجلة') }}</p>
                </div>
                @endif
            </div>
        </div>

        {{-- Sidebar --}}
        <div class="lg:col-span-1 space-y-6">
            {{-- Financial Summary --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4">{{ __('الملخص المالي') }}</h2>
                <div class="space-y-3">
                    <div class="flex justify-between text-sm">
                        <span class="text-gray-600">{{ __('الإجمالي') }}</span>
                        <span class="font-medium" dir="ltr">{{ number_format($invoice->total_amount / 100, 2) }} {{ __('ج.م') }}</span>
                    </div>
                    <div class="flex justify-between text-sm">
                        <span class="text-gray-600">{{ __('المدفوع') }}</span>
                        <span class="font-medium text-green-600" dir="ltr">{{ number_format($invoice->paid_amount / 100, 2) }} {{ __('ج.م') }}</span>
                    </div>
                    <div class="border-t border-gray-200 pt-3">
                        <div class="flex justify-between text-base font-bold">
                            <span class="text-gray-800">{{ __('المتبقي') }}</span>
                            <span class="{{ $invoice->due_amount > 0 ? 'text-red-600' : 'text-green-600' }}" dir="ltr">
                                {{ number_format($invoice->due_amount / 100, 2) }} {{ __('ج.م') }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            {{-- Record Payment Form --}}
            @if($this->canRecordPayment)
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6" x-data="{ showPayment: false }">
                <div class="flex items-center justify-between mb-4">
                    <h2 class="text-lg font-semibold text-gray-800">{{ __('تسجيل دفعة') }}</h2>
                    <button type="button" @click="showPayment = !showPayment"
                            class="text-sm text-blue-600 hover:text-blue-800">
                        <span x-show="!showPayment">{{ __('فتح') }}</span>
                        <span x-show="showPayment" x-cloak>{{ __('إغلاق') }}</span>
                    </button>
                </div>

                <div x-show="showPayment" x-cloak x-transition>
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('المبلغ (ج.م)') }} <span class="text-red-500">*</span></label>
                            <input type="number" wire:model="paymentAmount" min="0.01" step="0.01" dir="ltr"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                   placeholder="0.00">
                            @error('paymentAmount') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('طريقة الدفع') }} <span class="text-red-500">*</span></label>
                            <select wire:model="paymentMethod"
                                    class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                                @foreach($paymentMethods as $value => $label)
                                <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </select>
                            @error('paymentMethod') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('تاريخ الدفع') }} <span class="text-red-500">*</span></label>
                            <input type="date" wire:model="paymentDate" dir="ltr"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            @error('paymentDate') <p class="mt-1 text-xs text-red-600">{{ $message }}</p> @enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('ملاحظات') }}</label>
                            <textarea wire:model="paymentNotes" rows="2"
                                      class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                                      placeholder="{{ __('ملاحظات الدفعة...') }}"></textarea>
                        </div>

                        <button type="button" wire:click="recordPayment"
                                wire:loading.attr="disabled"
                                wire:target="recordPayment"
                                class="w-full flex items-center justify-center gap-2 px-4 py-2.5 bg-green-600 text-white rounded-lg hover:bg-green-700 active:bg-green-800 active:scale-[0.97] font-medium text-sm disabled:opacity-50 disabled:cursor-not-allowed transition-all">
                            <span wire:loading.remove wire:target="recordPayment">{{ __('تسجيل الدفعة') }}</span>
                            <span wire:loading wire:target="recordPayment">
                                <svg class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                            </span>
                            <span wire:loading wire:target="recordPayment">{{ __('جارٍ التسجيل...') }}</span>
                        </button>
                    </div>
                </div>
            </div>
            @endif

            {{-- Actions --}}
            @if($this->canCancel)
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
                <h2 class="text-lg font-semibold text-gray-800 mb-4">{{ __('إجراءات') }}</h2>
                <button type="button" wire:click="cancelInvoice"
                        wire:loading.attr="disabled"
                        wire:target="cancelInvoice"
                        wire:confirm="{{ __('هل أنت متأكد من إلغاء هذه الفاتورة؟') }}"
                        class="w-full flex items-center justify-center gap-2 px-4 py-2.5 bg-red-50 text-red-600 border border-red-200 rounded-lg hover:bg-red-100 active:bg-red-200 active:scale-[0.97] font-medium text-sm disabled:opacity-50 transition-all">
                    <span wire:loading.remove wire:target="cancelInvoice">{{ __('إلغاء الفاتورة') }}</span>
                    <span wire:loading wire:target="cancelInvoice">{{ __('جارٍ الإلغاء...') }}</span>
                </button>
            </div>
            @endif
        </div>
    </div>
</div>
