<div>
    {{-- Header --}}
    <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('pos.sell')
        <a href="{{ route('pos.terminal') }}"
           class="inline-flex items-center gap-2 px-4 py-2.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium transition-colors w-full sm:w-auto justify-center">
            <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 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>
            {{ __('نقطة البيع') }}
        </a>
        @endcan
    </div>

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3 sm:p-4 mb-6">
        <div class="grid grid-cols-2 md:grid-cols-4 gap-3 sm:gap-4">
            {{-- Search --}}
            <div class="col-span-2 md:col-span-1">
                <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('بحث') }}</label>
                <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 text-sm">
            </div>

            {{-- Date From --}}
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('من تاريخ') }}</label>
                <input type="date"
                       wire:model.live="dateFrom"
                       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 text-sm">
            </div>

            {{-- Date To --}}
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('إلى تاريخ') }}</label>
                <input type="date"
                       wire:model.live="dateTo"
                       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 text-sm">
            </div>

            {{-- Payment Method Filter --}}
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('طريقة الدفع') }}</label>
                <select wire:model.live="paymentMethodFilter"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm">
                    <option value="">{{ __('الكل') }}</option>
                    <option value="cash">{{ __('نقدي') }}</option>
                    <option value="card">{{ __('بطاقة') }}</option>
                    <option value="wallet">{{ __('محفظة') }}</option>
                    <option value="split">{{ __('تقسيم') }}</option>
                </select>
            </div>
        </div>
    </div>

    {{-- Transactions Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto" wire:loading.class="opacity-50 pointer-events-none">
            <table class="w-full text-sm hidden md:table">
                <thead class="bg-gray-50 border-b border-gray-200">
                    <tr>
                        <x-ui.sort-header column="receipt_number" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('رقم الإيصال') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start text-xs font-semibold text-gray-500 uppercase">{{ __('المشترك') }}</th>
                        <th class="px-4 py-3 text-center text-xs font-semibold text-gray-500 uppercase">{{ __('العناصر') }}</th>
                        <x-ui.sort-header column="total_amount" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('الإجمالي') }}</x-ui.sort-header>
                        <x-ui.sort-header column="payment_method" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الدفع') }}</x-ui.sort-header>
                        <th class="px-4 py-3 text-start text-xs font-semibold text-gray-500 uppercase">{{ __('الكاشير') }}</th>
                        <x-ui.sort-header column="processed_at" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('التاريخ') }}</x-ui.sort-header>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($transactions as $transaction)
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-4 py-3">
                                <span class="font-mono text-xs font-medium text-gray-800" dir="ltr">{{ $transaction->receipt_number }}</span>
                            </td>
                            <td class="px-4 py-3">
                                @if($transaction->participant && $transaction->participant->person)
                                    <span class="text-gray-800 font-medium">{{ $transaction->participant->person->name_ar }}</span>
                                @else
                                    <span class="text-gray-400 italic">{{ __('عميل عابر') }}</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span class="px-2 py-0.5 text-xs bg-gray-100 text-gray-700 rounded-full">
                                    {{ $transaction->items_count ?? $transaction->items->count() }}
                                </span>
                            </td>
                            <td class="px-4 py-3">
                                <span class="font-bold text-gray-800" dir="ltr">{{ number_format($transaction->total_amount / 100, 2) }} {{ __('ج.م') }}</span>
                            </td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $methodValue = $transaction->payment_method instanceof \BackedEnum ? $transaction->payment_method->value : $transaction->payment_method;
                                @endphp
                                @switch($methodValue)
                                    @case('cash')
                                        <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">{{ __('نقدي') }}</span>
                                        @break
                                    @case('card')
                                        <span class="px-2 py-0.5 text-xs bg-blue-100 text-blue-700 rounded-full">{{ __('بطاقة') }}</span>
                                        @break
                                    @case('wallet')
                                        <span class="px-2 py-0.5 text-xs bg-purple-100 text-purple-700 rounded-full">{{ __('محفظة') }}</span>
                                        @break
                                    @case('split')
                                        <span class="px-2 py-0.5 text-xs bg-amber-100 text-amber-700 rounded-full">{{ __('تقسيم') }}</span>
                                        @break
                                @endswitch
                            </td>
                            <td class="px-4 py-3">
                                <span class="text-gray-600 text-xs">{{ $transaction->processedBy?->name_ar ?? $transaction->processedBy?->name ?? '-' }}</span>
                            </td>
                            <td class="px-4 py-3">
                                <span class="text-gray-500 text-xs" dir="ltr">{{ $transaction->processed_at?->format('Y-m-d H:i') }}</span>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-4 py-12 text-center">
                                <svg class="w-12 h-12 text-gray-200 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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-400 text-sm">{{ __('لا توجد عمليات بيع في هذه الفترة') }}</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Mobile Cards --}}
        <div class="md:hidden" wire:loading.class="opacity-50 pointer-events-none">
            @forelse($transactions as $transaction)
                <div class="p-3 border-b border-gray-100 last:border-0">
                    {{-- Top row: receipt number + payment badge --}}
                    <div class="flex justify-between items-center mb-1">
                        <span class="font-mono text-xs font-medium text-gray-800" dir="ltr">{{ $transaction->receipt_number }}</span>
                        @php
                            $methodValue = $transaction->payment_method instanceof \BackedEnum ? $transaction->payment_method->value : $transaction->payment_method;
                        @endphp
                        @switch($methodValue)
                            @case('cash')
                                <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">{{ __('نقدي') }}</span>
                                @break
                            @case('card')
                                <span class="px-2 py-0.5 text-xs bg-blue-100 text-blue-700 rounded-full">{{ __('بطاقة') }}</span>
                                @break
                            @case('wallet')
                                <span class="px-2 py-0.5 text-xs bg-purple-100 text-purple-700 rounded-full">{{ __('محفظة') }}</span>
                                @break
                            @case('split')
                                <span class="px-2 py-0.5 text-xs bg-amber-100 text-amber-700 rounded-full">{{ __('تقسيم') }}</span>
                                @break
                        @endswitch
                    </div>
                    {{-- Middle: participant name --}}
                    <div class="mb-1">
                        @if($transaction->participant && $transaction->participant->person)
                            <span class="text-gray-800 font-medium text-sm">{{ $transaction->participant->person->name_ar }}</span>
                        @else
                            <span class="text-gray-400 italic text-sm">{{ __('عميل عابر') }}</span>
                        @endif
                    </div>
                    {{-- Bottom row: total + date --}}
                    <div class="flex justify-between items-center">
                        <span class="font-bold text-gray-800 text-sm" dir="ltr">{{ number_format($transaction->total_amount / 100, 2) }} {{ __('ج.م') }}</span>
                        <span class="text-xs text-gray-500" dir="ltr">{{ $transaction->processed_at?->format('Y-m-d H:i') }}</span>
                    </div>
                </div>
            @empty
                <div class="px-4 py-12 text-center">
                    <svg class="w-12 h-12 text-gray-200 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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-400 text-sm">{{ __('لا توجد عمليات بيع في هذه الفترة') }}</p>
                </div>
            @endforelse
        </div>

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