<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

    {{-- 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>
                        <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>
                        <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>
                        <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>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('بواسطة') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($expenses as $expense)
                    <tr class="hover:bg-gray-50">
                        <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 truncate">{{ $expense->description }}</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">
                            @php
                                $methodLabels = [
                                    'cash' => 'نقدي',
                                    'card' => 'بطاقة',
                                    'bank_transfer' => 'تحويل بنكي',
                                    'cheque' => 'شيك',
                                    'other' => 'أخرى',
                                ];
                            @endphp
                            {{ $methodLabels[$expense->payment_method->value ?? $expense->payment_method] ?? $expense->payment_method }}
                        </td>
                        <td class="px-4 py-3 text-gray-500 text-xs">{{ $expense->creator?->name }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" 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>
</div>
