<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>
        <a href="{{ route('wallets.list') }}" wire:navigate
           class="inline-flex items-center gap-2 px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 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="M10 19l-7-7m0 0l7-7m-7 7h18"/></svg>
            {{ __('عودة') }}
        </a>
    </div>

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

    {{-- Wallet Info Card --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6 mb-4 sm:mb-6">
        <div class="grid grid-cols-1 sm:grid-cols-3 gap-4 sm:gap-6">
            <div>
                <p class="text-sm text-gray-500 mb-1">{{ __('صاحب المحفظة') }}</p>
                <p class="text-lg font-semibold text-gray-800">
                    @if($wallet->owner && method_exists($wallet->owner, 'person') && $wallet->owner->person)
                        {{ $wallet->owner->person->name_ar }}
                    @elseif($wallet->owner)
                        {{ $wallet->owner->name_ar ?? $wallet->owner->name ?? '—' }}
                    @else
                        —
                    @endif
                </p>
            </div>
            <div>
                <p class="text-sm text-gray-500 mb-1">{{ __('الرصيد الحالي') }}</p>
                <p class="text-2xl font-bold {{ $wallet->balance > 0 ? 'text-green-700' : 'text-gray-800' }}" dir="ltr">
                    {{ number_format($wallet->balance / 100, 2) }} {{ __('ج.م') }}
                </p>
            </div>
            <div>
                <p class="text-sm text-gray-500 mb-1">{{ __('الحالة') }}</p>
                @if($wallet->is_active)
                    <span class="px-3 py-1 text-sm bg-green-100 text-green-700 rounded-full">{{ __('نشطة') }}</span>
                @else
                    <span class="px-3 py-1 text-sm bg-gray-100 text-gray-700 rounded-full">{{ __('غير نشطة') }}</span>
                @endif
            </div>
        </div>
    </div>

    {{-- Deposit & Withdraw Forms --}}
    @if($wallet->is_active)
    <div class="grid grid-cols-1 md:grid-cols-2 gap-4 sm:gap-6 mb-4 sm:mb-6">
        {{-- Deposit Form --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-lg font-semibold text-gray-800 mb-4">{{ __('إيداع') }}</h3>
            <form wire:submit="deposit" class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('المبلغ (ج.م)') }}</label>
                    <input type="number" wire:model="depositAmount" step="0.01" min="0.01" dir="ltr"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500">
                    @error('depositAmount') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الوصف') }}</label>
                    <input type="text" wire:model="depositDescription"
                           placeholder="{{ __('سبب الإيداع...') }}"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500">
                    @error('depositDescription') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
                </div>
                <button type="submit" wire:loading.attr="disabled" wire:target="deposit"
                        class="w-full px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 disabled:opacity-50 font-medium text-sm">
                    <span wire:loading.remove wire:target="deposit">{{ __('إيداع') }}</span>
                    <span wire:loading wire:target="deposit">{{ __('جارٍ الإيداع...') }}</span>
                </button>
            </form>
        </div>

        {{-- Withdraw Form --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-lg font-semibold text-gray-800 mb-4">{{ __('سحب') }}</h3>
            <form wire:submit="withdraw" class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('المبلغ (ج.م)') }}</label>
                    <input type="number" wire:model="withdrawAmount" step="0.01" min="0.01" dir="ltr"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-red-500 focus:border-red-500">
                    @error('withdrawAmount') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">{{ __('الوصف') }}</label>
                    <input type="text" wire:model="withdrawDescription"
                           placeholder="{{ __('سبب السحب...') }}"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-red-500 focus:border-red-500">
                    @error('withdrawDescription') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
                </div>
                <button type="submit" wire:loading.attr="disabled" wire:target="withdraw"
                        class="w-full px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 disabled:opacity-50 font-medium text-sm">
                    <span wire:loading.remove wire:target="withdraw">{{ __('سحب') }}</span>
                    <span wire:loading wire:target="withdraw">{{ __('جارٍ السحب...') }}</span>
                </button>
            </form>
        </div>
    </div>
    @endif

    {{-- Transaction History --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden overflow-x-auto">
        <div class="px-4 sm:px-6 py-3 sm:py-4 border-b border-gray-200">
            <h3 class="text-lg font-semibold text-gray-800">{{ __('سجل المعاملات') }}</h3>
        </div>
        <div 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-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-start font-medium text-gray-600">{{ __('الوصف') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($transactions as $transaction)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 text-gray-600" dir="ltr">
                            {{ $transaction->created_at->format('Y-m-d H:i') }}
                        </td>
                        <td class="px-4 py-3">
                            @if($transaction->direction === 'credit')
                                <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">{{ __('إيداع') }}</span>
                            @else
                                <span class="px-2 py-0.5 text-xs bg-red-100 text-red-700 rounded-full">{{ __('سحب') }}</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-end font-mono {{ $transaction->direction === 'credit' ? 'text-green-700' : 'text-red-700' }}" dir="ltr">
                            {{ $transaction->direction === 'credit' ? '+' : '-' }}{{ number_format($transaction->amount / 100, 2) }}
                        </td>
                        <td class="px-4 py-3 text-end font-mono text-gray-700" dir="ltr">
                            {{ number_format($transaction->balance_after / 100, 2) }}
                        </td>
                        <td class="px-4 py-3 text-gray-600">
                            {{ $transaction->description ?? '—' }}
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" 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="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
                                </svg>
                                <p class="text-gray-500 text-sm">{{ __('لا توجد معاملات') }}</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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