<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">{{ __('سجل المصروفات') }}</h1>
        @can('expenses.create')
        <a href="{{ route('expenses.create') }}" wire:navigate
           class="inline-flex items-center gap-2 px-4 py-2 bg-emerald-600 text-white rounded-lg hover:bg-emerald-700 transition text-sm">
            <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>

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

    {{-- Summary Card --}}
    <div class="mb-4 p-4 bg-gradient-to-l from-red-50 to-white border border-red-200 rounded-xl">
        <div class="flex items-center gap-3">
            <div class="w-10 h-10 bg-red-100 rounded-lg flex items-center justify-center">
                <svg class="w-5 h-5 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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>
            </div>
            <div>
                <p class="text-sm text-gray-500">{{ __('إجمالي المصروفات') }}</p>
                <p class="text-lg font-bold text-red-700" dir="ltr">{{ format_money($totalAmount) }}</p>
            </div>
        </div>
    </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-3">
            <input type="text" wire:model.live.debounce.300ms="search"
                   class="w-full rounded-lg border-gray-300 text-sm py-2" placeholder="{{ __('بحث بالوصف أو المستلم...') }}">
            <select wire:model.live="category_filter" class="w-full rounded-lg border-gray-300 text-sm py-2">
                <option value="">{{ __('كل الفئات') }}</option>
                @foreach($categories as $cat)
                <option value="{{ $cat->value }}">{{ $cat->label() }}</option>
                @endforeach
            </select>
            <input type="date" wire:model.live="from_date" dir="ltr"
                   class="w-full rounded-lg border-gray-300 text-sm py-2" placeholder="{{ __('من') }}">
            <input type="date" wire:model.live="to_date" dir="ltr"
                   class="w-full rounded-lg border-gray-300 text-sm py-2" placeholder="{{ __('إلى') }}">
        </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" class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 border-b">
                    <tr>
                        <x-ui.sort-header column="expense_date" :sortBy="$sortBy" :sortDir="$sortDir" class="text-xs text-gray-500">{{ __('التاريخ') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('الفئة') }}</th>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('الوصف') }}</th>
                        <x-ui.sort-header column="amount" :sortBy="$sortBy" :sortDir="$sortDir" class="text-xs text-gray-500">{{ __('المبلغ') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('المستلم') }}</th>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('طريقة الدفع') }}</th>
                        <x-ui.sort-header column="status" :sortBy="$sortBy" :sortDir="$sortDir" class="text-xs text-gray-500">{{ __('الحالة') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('بواسطة') }}</th>
                        <th class="px-4 py-3 text-center text-xs font-medium text-gray-500">{{ __('إجراءات') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($expenses as $expense)
                    <tr wire:key="expense-{{ $expense->uuid }}"
                        x-on:click="Livewire.navigate(@js(route('expenses.show', $expense->uuid)))"
                        class="hover:bg-gray-50 cursor-pointer {{ $expense->status === 'cancelled' ? 'opacity-60' : '' }}">
                        <td class="px-4 py-3 text-gray-600" dir="ltr">{{ $expense->expense_date?->format('Y-m-d') }}</td>
                        <td class="px-4 py-3">
                            <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-700">
                                {{ $expense->category->label() }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-gray-800 max-w-xs">
                            <div class="flex items-center gap-1.5">
                                <span class="truncate">{{ $expense->description }}</span>
                                @if($expense->hasAttachment())
                                <svg class="w-3.5 h-3.5 shrink-0 text-gray-400" title="{{ __('يوجد مرفق') }}" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"/></svg>
                                @endif
                            </div>
                        </td>
                        <td class="px-4 py-3 font-semibold text-red-700" dir="ltr">{{ format_money($expense->amount) }}</td>
                        <td class="px-4 py-3 text-gray-600">{{ $expense->recipient_name ?? '—' }}</td>
                        <td class="px-4 py-3 text-gray-600">{{ $expense->payment_method?->label() ?? '—' }}</td>
                        <td class="px-4 py-3">
                            @if($expense->status === 'cancelled')
                            <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-700" title="{{ $expense->cancellation_reason }}">{{ __('ملغى') }}</span>
                            @else
                            <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-700">{{ __('نشط') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-gray-500 text-xs">{{ $expense->creator?->name }}</td>
                        <td class="px-4 py-3 text-center" x-on:click.stop>
                            <div class="flex items-center justify-center gap-3">
                                <a href="{{ route('expenses.show', $expense->uuid) }}" wire:navigate
                                   class="inline-flex items-center gap-1 text-emerald-700 hover:text-emerald-900 text-xs font-medium">
                                    <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                                    {{ __('عرض') }}
                                </a>
                                @if($expense->status !== 'cancelled')
                                <button wire:click="openCancelModal('{{ $expense->uuid }}')"
                                        class="text-red-600 hover:text-red-800 text-xs font-medium">
                                    {{ __('إلغاء') }}
                                </button>
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="9" class="px-4 py-8 text-center text-gray-400">{{ __('لا توجد مصروفات مسجلة') }}</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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

    {{-- Cancel Modal --}}
    @if($showCancelModal)
    <div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50" wire:click.self="$set('showCancelModal', false)">
        <div class="bg-white rounded-xl shadow-xl w-full max-w-md mx-4 p-6">
            <h3 class="text-lg font-bold text-gray-800 mb-4">{{ __('إلغاء المصروف') }}</h3>
            <p class="text-sm text-gray-600 mb-4">{{ __('سيتم عكس القيد المالي لهذا المصروف. المصروف سيبقى مرئياً بحالة "ملغى".') }}</p>
            <div class="mb-4">
                <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('سبب الإلغاء') }} <span class="text-red-500">*</span></label>
                <textarea wire:model="cancellationReason" rows="3"
                          class="w-full rounded-lg border-gray-300 text-sm focus:ring-red-500 focus:border-red-500"
                          placeholder="{{ __('مثال: تم تسجيل المصروف بالغلط في فرع آخر') }}"></textarea>
                @error('cancellationReason')
                <p class="text-xs text-red-600 mt-1">{{ $message }}</p>
                @enderror
            </div>
            <div class="flex gap-3 justify-end">
                <button wire:click="$set('showCancelModal', false)"
                        class="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 rounded-lg border border-gray-300">
                    {{ __('تراجع') }}
                </button>
                <button wire:click="confirmCancel"
                        wire:loading.attr="disabled"
                        class="px-4 py-2 text-sm bg-red-600 text-white rounded-lg hover:bg-red-700 disabled:opacity-50">
                    <span wire:loading.remove wire:target="confirmCancel">{{ __('تأكيد الإلغاء') }}</span>
                    <span wire:loading wire:target="confirmCancel">{{ __('جارٍ الإلغاء...') }}</span>
                </button>
            </div>
        </div>
    </div>
    @endif
</div>
