<x-app-layout>
    <x-slot name="header">
        <div class="flex items-center justify-between">
            <h2 class="font-bold text-2xl text-gray-800 leading-tight flex items-center">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8 ml-2 text-amber-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                </svg>
                {{ \App\Helpers\TranslationHelper::transFromDatabase('Edit Bank Withdrawal Slip', __('تعديل سند السحب البنكي')) }}
                <span class="mr-3 px-3 py-1 bg-gray-100 text-gray-600 rounded-lg text-sm font-mono tracking-tight font-normal">#{{ $withdrawal->withdrawal_number }}</span>
            </h2>
            <div class="flex items-center gap-4">
                <a href="{{ route('banking.withdrawals.show', $withdrawal) }}" class="flex items-center text-gray-600 hover:text-indigo-600 transition-colors duration-200">
                    <span class="ml-1">{{ __('عرض التفاصيل') }}</span>
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                    </svg>
                </a>
            </div>
        </div>
    </x-slot>

    <div class="py-8">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-xl sm:rounded-2xl border border-gray-100">
                <div class="p-8">
                    <form method="POST" action="{{ route('banking.withdrawals.update', $withdrawal) }}" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')

                        <!-- معلومات السند الأساسية -->
                        <div class="mb-8 border-b border-gray-100 pb-6">
                            <h3 class="text-lg font-bold text-gray-900 mb-6 flex items-center">
                                <span class="w-8 h-8 bg-indigo-50 text-indigo-600 rounded-full flex items-center justify-center ml-3 font-sans text-sm">1</span>
                                {{ __('المعلومات الأساسية') }}
                            </h3>
                            <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
                                <!-- رقم السحب -->
                                <div class="relative">
                                    <x-input-label for="withdrawal_number" :value="__('رقم السحب')" class="text-gray-700 font-semibold mb-2" />
                                    <div class="relative group">
                                        <div class="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-400 group-focus-within:text-indigo-500 transition-colors" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 20l4-16m2 16l4-16M6 9h14M4 15h14" />
                                            </svg>
                                        </div>
                                        <x-text-input id="withdrawal_number" class="block w-full pr-10 bg-gray-50 border-gray-200 focus:bg-white focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 rounded-xl transition-all duration-200" type="text" name="withdrawal_number" :value="old('withdrawal_number', $withdrawal->withdrawal_number)" required readonly />
                                    </div>
                                    <x-input-error :messages="$errors->get('withdrawal_number')" class="mt-2" />
                                </div>

                                <!-- التاريخ -->
                                <div>
                                    <x-input-label for="withdrawal_date" :value="__('تاريخ السحب')" class="text-gray-700 font-semibold mb-2" />
                                    <div class="relative group">
                                        <div class="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-400 group-focus-within:text-indigo-500 transition-colors" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
                                            </svg>
                                        </div>
                                        <x-text-input id="withdrawal_date" class="block w-full pr-10 border-gray-200 focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 rounded-xl transition-all duration-200" type="date" name="withdrawal_date" :value="old('withdrawal_date', $withdrawal->withdrawal_date ? $withdrawal->withdrawal_date->format('Y-m-d') : '')" required />
                                    </div>
                                    <x-input-error :messages="$errors->get('withdrawal_date')" class="mt-2" />
                                </div>

                                <!-- المبلغ -->
                                <div>
                                    <x-input-label for="amount" :value="__('المبلغ')" class="text-gray-700 font-semibold mb-2" />
                                    <div class="relative group">
                                        <div class="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none">
                                            <span class="text-gray-400 font-bold group-focus-within:text-indigo-500">SAR</span>
                                        </div>
                                        <x-text-input id="amount" class="block w-full pr-12 border-gray-200 focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 rounded-xl text-lg font-bold text-indigo-700 placeholder-gray-300 transition-all duration-200" type="number" step="0.01" name="amount" :value="old('amount', $withdrawal->amount)" placeholder="0.00" required />
                                    </div>
                                    <x-input-error :messages="$errors->get('amount')" class="mt-2" />
                                </div>
                            </div>
                        </div>

                        <!-- الحسابات والأطراف -->
                        <div class="mb-8 border-b border-gray-100 pb-6">
                            <h3 class="text-lg font-bold text-gray-900 mb-6 flex items-center">
                                <span class="w-8 h-8 bg-emerald-50 text-emerald-600 rounded-full flex items-center justify-center ml-3 font-sans text-sm">2</span>
                                {{ __('الحسابات والعملية المالية') }}
                            </h3>
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                                <!-- الحساب البنكي -->
                                <div>
                                    <x-input-label for="bank_account_id" :value="__('الحساب البنكي (المصدر)')" class="text-gray-700 font-semibold mb-2" />
                                    <select id="bank_account_id" name="bank_account_id" class="mt-1 block w-full rounded-xl border-gray-200 shadow-sm focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 transition-all duration-200" required>
                                        <option value="">{{ __('اختر الحساب البنكي') }}</option>
                                        @foreach($bankAccounts as $account)
                                            @if($account->chartOfAccount && $account->chartOfAccount->is_parent == 0)
                                                <option value="{{ $account->id }}" {{ old('bank_account_id', $withdrawal->bank_account_id) == $account->id ? 'selected' : '' }}>
                                                    {{ $account->bank->name }} - {{ $account->account_name }} - {{ $account->account_number }} ({{ $account->chartOfAccount->code }})
                                                </option>
                                            @endif
                                        @endforeach
                                    </select>
                                    <x-input-error :messages="$errors->get('bank_account_id')" class="mt-2" />
                                </div>

                                <!-- الحساب الوجهة -->
                                <div>
                                    <x-input-label for="chart_of_account_id" :value="__('الحساب الوجهة (المدين)')" class="text-gray-700 font-semibold mb-2" />
                                    <select id="chart_of_account_id" name="chart_of_account_id" class="mt-1 block w-full rounded-xl border-gray-200 shadow-sm focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 transition-all duration-200" required>
                                        <option value="">{{ __('اختر الحساب الوجهة') }}</option>
                                        @foreach($accounts as $account)
                                            @php
                                                $level = substr_count($account->code, '.') + (strlen($account->code) > 4 ? 1 : 0);
                                                $indent = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level);
                                            @endphp
                                            <option value="{{ $account->id }}" {{ old('chart_of_account_id', $withdrawal->chart_of_account_id) == $account->id ? 'selected' : '' }}>
                                                {!! $indent !!}{{ $account->code }} - {{ $account->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                    <x-input-error :messages="$errors->get('chart_of_account_id')" class="mt-2" />
                                </div>
                            </div>
                        </div>

                        <!-- تفاصيل إضافية -->
                        <div class="mb-8 border-b border-gray-100 pb-6">
                            <h3 class="text-lg font-bold text-gray-900 mb-6 flex items-center">
                                <span class="w-8 h-8 bg-amber-50 text-amber-600 rounded-full flex items-center justify-center ml-3 font-sans text-sm">3</span>
                                {{ __('تفاصيل إضافية وتصنيف') }}
                            </h3>
                            <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                                <!-- طريقة السحب -->
                                <div>
                                    <x-input-label for="withdrawal_method" :value="__('طريقة السحب')" class="text-gray-700 font-semibold mb-2" />
                                    <select id="withdrawal_method" name="withdrawal_method" class="mt-1 block w-full rounded-xl border-gray-200 shadow-sm focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 transition-all duration-200" required>
                                        <option value="">{{ __('اختر الطريقة') }}</option>
                                        <option value="cash" {{ old('withdrawal_method', $withdrawal->withdrawal_method) == 'cash' ? 'selected' : '' }}>{{ __('نقدي') }}</option>
                                        <option value="check" {{ old('withdrawal_method', $withdrawal->withdrawal_method) == 'check' ? 'selected' : '' }}>{{ __('شيك') }}</option>
                                        <option value="transfer" {{ old('withdrawal_method', $withdrawal->withdrawal_method) == 'transfer' ? 'selected' : '' }}>{{ __('تحويل بنكي') }}</option>
                                        <option value="other" {{ old('withdrawal_method', $withdrawal->withdrawal_method) == 'other' ? 'selected' : '' }}>{{ __('أخرى') }}</option>
                                    </select>
                                    <x-input-error :messages="$errors->get('withdrawal_method')" class="mt-2" />
                                </div>

                                <!-- رقم المرجع -->
                                <div>
                                    <x-input-label for="reference_number" :value="__('رقم المرجع')" class="text-gray-700 font-semibold mb-2" />
                                    <x-text-input id="reference_number" class="block w-full border-gray-200 focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 rounded-xl transition-all duration-200" type="text" name="reference_number" :value="old('reference_number', $withdrawal->reference_number)" />
                                    <x-input-error :messages="$errors->get('reference_number')" class="mt-2" />
                                </div>

                                <!-- اسم المستفيد -->
                                <div class="md:col-span-2">
                                    <x-input-label for="beneficiary_name" :value="__('اسم المستفيد')" class="text-gray-700 font-semibold mb-2" />
                                    <x-text-input id="beneficiary_name" class="block w-full border-gray-200 focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 rounded-xl transition-all duration-200" type="text" name="beneficiary_name" :value="old('beneficiary_name', $withdrawal->beneficiary_name)" placeholder="{{ __('أدخل اسم المستفيد') }}" />
                                    <x-input-error :messages="$errors->get('beneficiary_name')" class="mt-2" />
                                </div>

                                <!-- مركز التكلفة -->
                                <div class="md:col-span-2">
                                    <x-input-label for="cost_center_id" :value="__('مركز التكلفة')" class="text-gray-700 font-semibold mb-2" />
                                    <select id="cost_center_id" name="cost_center_id" class="mt-1 block w-full rounded-xl border-gray-200 shadow-sm focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 transition-all duration-200">
                                        <option value="">{{ __('اختر مركز التكلفة (اختياري)') }}</option>
                                        @foreach($costCenters as $costCenter)
                                            <option value="{{ $costCenter->id }}" {{ old('cost_center_id', $withdrawal->cost_center_id) == $costCenter->id ? 'selected' : '' }}>
                                                {{ $costCenter->name }} ({{ $costCenter->code }})
                                            </option>
                                        @endforeach
                                    </select>
                                    <x-input-error :messages="$errors->get('cost_center_id')" class="mt-2" />
                                </div>

                                <!-- المرفقات -->
                                <div class="md:col-span-2">
                                    <x-input-label for="attachment" :value="__('رفع مرفق جديد')" class="text-gray-700 font-semibold mb-2" />
                                    <div class="mt-1 flex justify-center px-6 pt-5 pb-6 border-2 border-gray-200 border-dashed rounded-xl hover:border-indigo-400 transition-colors">
                                        <div class="space-y-1 text-center">
                                            <svg class="mx-auto h-12 w-12 text-gray-400" stroke="currentColor" fill="none" viewBox="0 0 48 48" aria-hidden="true">
                                                <path d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
                                            </svg>
                                            <div class="flex text-sm text-gray-600">
                                                <label for="attachment" class="relative cursor-pointer bg-white rounded-md font-medium text-indigo-600 hover:text-indigo-500 focus-within:outline-none focus-within:ring-2 focus-within:ring-offset-2 focus-within:ring-indigo-500">
                                                    <span>{{ __('قم برفع ملف') }}</span>
                                                    <input id="attachment" name="attachment" type="file" class="sr-only">
                                                </label>
                                                <p class="pr-1 text-gray-500">{{ __('أو سحب وإفلات') }}</p>
                                            </div>
                                            <p class="text-xs text-gray-400">PNG, JPG, PDF up to 2MB</p>
                                        </div>
                                    </div>
                                    <x-input-error :messages="$errors->get('attachment')" class="mt-2" />
                                    
                                    @if($withdrawal->attachment)
                                        <div class="mt-3 p-3 bg-gray-50 rounded-xl border border-gray-100 flex items-center justify-between">
                                            <div class="flex items-center">
                                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-indigo-500 ml-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13" />
                                                </svg>
                                                <span class="text-sm text-gray-600">{{ __('المرفق الحالي متوفر') }}</span>
                                            </div>
                                            <a href="{{ asset($withdrawal->attachment) }}" target="_blank" class="text-xs font-bold text-indigo-600 hover:underline">
                                                {{ __('معاينة') }}
                                            </a>
                                        </div>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <!-- الوصف -->
                        <div class="mb-8">
                            <x-input-label for="description" :value="__('الوصف / الملاحظات')" class="text-gray-700 font-semibold mb-2" />
                            <textarea id="description" name="description" rows="4" class="mt-1 block w-full rounded-xl border-gray-200 shadow-sm focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 transition-all duration-200" placeholder="{{ __('أدخل تفاصيل إضافية حول عملية السحب...') }}">{{ old('description', $withdrawal->description) }}</textarea>
                            <x-input-error :messages="$errors->get('description')" class="mt-2" />
                        </div>

                        <!-- أزرار الإجراءات -->
                        <div class="flex items-center justify-end gap-4 pt-6 border-t border-gray-100">
                            <a href="{{ route('banking.withdrawals.index') }}" class="px-6 py-3 bg-white border border-gray-200 rounded-xl font-bold text-gray-700 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:ring-2 focus:ring-gray-200 transition-all duration-200">
                                {{ __('إلغاء') }}
                            </a>
                            <button type="submit" class="px-10 py-3 bg-indigo-600 border border-transparent rounded-xl font-bold text-white hover:bg-indigo-700 focus:outline-none focus:ring-4 focus:ring-indigo-500/30 transition-all duration-200 flex items-center shadow-lg shadow-indigo-200">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                                </svg>
                                {{ __('تحديث سند السحب') }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
