<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('cash_sessions.manage')
        <a href="{{ route('cash-sessions.manage') }}" wire:navigate
           class="inline-flex items-center justify-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium">
            <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>
            <span class="hidden sm:inline">{{ __('إدارة الوردية') }}</span>
        </a>
        @endcan
    </div>

    {{-- Flash Messages --}}
    @if(session('success'))
    <div class="mb-4 p-3 sm:p-4 bg-green-50 border border-green-200 rounded-lg text-green-700 text-sm">
        {{ session('success') }}
    </div>
    @endif

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3 sm:p-4 mb-4">
        <div class="grid grid-cols-1 sm:grid-cols-3 gap-3">
            <select wire:model.live="status" class="px-4 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                <option value="">{{ __('كل الحالات') }}</option>
                @foreach($statusOptions as $value => $label)
                <option value="{{ $value }}">{{ $label }}</option>
                @endforeach
            </select>
        </div>
    </div>

    {{-- Desktop Table --}}
    <div class="hidden md:block 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">
                <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-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-end font-medium text-gray-600">{{ __('رصيد الافتتاح') }}</th>
                        <th class="px-4 py-3 text-end font-medium text-gray-600">{{ __('رصيد الإغلاق') }}</th>
                        <th class="px-4 py-3 text-end 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">
                    @forelse($sessions as $session)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 font-medium text-gray-800">
                            {{ $session->user?->name_ar ?? $session->user?->name ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            {{ $session->branch?->name_ar ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center">
                            @php
                                $statusColors = [
                                    'open' => 'green',
                                    'closed' => 'gray',
                                    'suspended' => 'amber',
                                ];
                                $color = $statusColors[$session->status] ?? 'gray';
                            @endphp
                            <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                                {{ $statusOptions[$session->status] ?? $session->status }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-end font-mono text-gray-700" dir="ltr">
                            {{ number_format($session->opening_balance / 100, 2) }}
                        </td>
                        <td class="px-4 py-3 text-end font-mono text-gray-700" dir="ltr">
                            {{ $session->closing_balance !== null ? number_format($session->closing_balance / 100, 2) : '—' }}
                        </td>
                        <td class="px-4 py-3 text-end font-mono {{ ($session->variance ?? 0) < 0 ? 'text-red-700' : (($session->variance ?? 0) > 0 ? 'text-amber-700' : 'text-gray-700') }}" dir="ltr">
                            @if($session->variance !== null)
                                {{ $session->variance >= 0 ? '+' : '' }}{{ number_format($session->variance / 100, 2) }}
                            @else
                                —
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                            {{ $session->opened_at?->format('Y-m-d H:i') ?? '—' }}
                        </td>
                        <td class="px-4 py-3 text-center text-gray-600" dir="ltr">
                            {{ $session->closed_at?->format('Y-m-d H:i') ?? '—' }}
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="px-4 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <svg class="w-12 h-12 text-gray-300 mb-3" 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>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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

    {{-- Mobile Cards --}}
    <div class="md:hidden space-y-3" wire:loading.class="opacity-50 pointer-events-none">
        @forelse($sessions as $session)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <div class="flex items-center justify-between mb-3">
                <span class="font-medium text-gray-800 text-sm">
                    {{ $session->user?->name_ar ?? $session->user?->name ?? '—' }}
                </span>
                @php
                    $statusColors = [
                        'open' => 'green',
                        'closed' => 'gray',
                        'suspended' => 'amber',
                    ];
                    $color = $statusColors[$session->status] ?? 'gray';
                @endphp
                <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                    {{ $statusOptions[$session->status] ?? $session->status }}
                </span>
            </div>

            <div class="text-xs text-gray-500 mb-3">
                {{ $session->branch?->name_ar ?? '—' }}
            </div>

            <div class="grid grid-cols-2 gap-3 text-sm">
                <div>
                    <span class="text-gray-500 text-xs block mb-0.5">{{ __('رصيد الافتتاح') }}</span>
                    <span class="font-mono text-gray-700" dir="ltr">{{ number_format($session->opening_balance / 100, 2) }}</span>
                </div>
                <div>
                    <span class="text-gray-500 text-xs block mb-0.5">{{ __('الفرق') }}</span>
                    <span class="font-mono {{ ($session->variance ?? 0) < 0 ? 'text-red-700' : (($session->variance ?? 0) > 0 ? 'text-amber-700' : 'text-gray-700') }}" dir="ltr">
                        @if($session->variance !== null)
                            {{ $session->variance >= 0 ? '+' : '' }}{{ number_format($session->variance / 100, 2) }}
                        @else
                            —
                        @endif
                    </span>
                </div>
            </div>

            <div class="mt-3 pt-3 border-t border-gray-100 text-xs text-gray-500" dir="ltr">
                {{ $session->opened_at?->format('Y-m-d H:i') ?? '—' }}
            </div>
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-8">
            <div class="flex flex-col items-center">
                <svg class="w-12 h-12 text-gray-300 mb-3" 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>
        </div>
        @endforelse

        @if($sessions->hasPages())
        <div class="mt-3">
            {{ $sessions->links() }}
        </div>
        @endif
    </div>
</div>
