<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('facilities.update')
        <a href="{{ route('expenses.rent.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-emerald-50 to-white border border-emerald-200 rounded-xl">
        <div class="flex items-center gap-3">
            <div class="w-10 h-10 bg-emerald-100 rounded-lg flex items-center justify-center">
                <svg class="w-5 h-5 text-emerald-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/></svg>
            </div>
            <div>
                <p class="text-sm text-gray-500">{{ __('إجمالي المدفوع') }}</p>
                <p class="text-lg font-bold text-emerald-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-3 gap-3">
            <input type="text" wire:model.live.debounce.300ms="search"
                   class="w-full rounded-lg border-gray-300 text-sm py-2" placeholder="{{ __('بحث برقم شيك أو مستلم...') }}">
            <input type="month" wire:model.live="period_filter" dir="ltr"
                   class="w-full rounded-lg border-gray-300 text-sm py-2">
        </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>
                        <x-ui.sort-header column="period" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('الشهر') }}</x-ui.sort-header>
                        <x-ui.sort-header column="amount" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('المبلغ') }}</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>
                        <th class="px-4 py-3 text-start text-xs font-medium text-gray-500">{{ __('المستلم') }}</th>
                        <x-ui.sort-header column="payment_date" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('تاريخ الدفع') }}</x-ui.sort-header>
                        <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($payments as $payment)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-medium text-gray-800">{{ $payment->facility?->name_ar }}</td>
                        <td class="px-4 py-3 text-gray-600" dir="ltr">{{ $payment->period }}</td>
                        <td class="px-4 py-3 font-semibold text-gray-800" dir="ltr">{{ format_money($payment->amount) }}</td>
                        <td class="px-4 py-3 text-gray-600">
                            @php
                                $methodLabels = [
                                    'cheque' => 'شيك',
                                    'cash' => 'نقدي',
                                    'bank_transfer' => 'تحويل بنكي',
                                    'card' => 'بطاقة',
                                    'other' => 'أخرى',
                                ];
                            @endphp
                            {{ $methodLabels[$payment->payment_method->value ?? $payment->payment_method] ?? $payment->payment_method }}
                        </td>
                        <td class="px-4 py-3 text-gray-600" dir="ltr">{{ $payment->cheque_number ?? '—' }}</td>
                        <td class="px-4 py-3 text-gray-600">{{ $payment->recipient_name ?? '—' }}</td>
                        <td class="px-4 py-3 text-gray-600" dir="ltr">{{ $payment->payment_date?->format('Y-m-d') }}</td>
                        <td class="px-4 py-3 text-gray-500 text-xs">{{ $payment->creator?->name }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="px-4 py-8 text-center text-gray-400">{{ __('لا توجد دفعات إيجار مسجلة') }}</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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