﻿<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-l text-gray-800 leading-tight">
            {{ __('تعديل الإيداع البنكي') }} #{{ $deposit->id }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7l m-auto sm:p-6 lg:p-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <form method="POST" action="{{ route('banking.deposits.update', $deposit) }}" class="space-y-6">
                        @csrf
                        @method('PUT')

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <!-- الحساب البنكي -->
                            <div>
                                <x-input-label for="bank_account_id" :value="__('الحساب البنكي')" />
                                <select id="bank_account_id" name="bank_account_id" class="select2 mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" required>
                                    <option value="">{{ __('اختر الحساب البنكي') }}</option>
                                    @foreach($bankAccounts as $account)
                                        @if($account->chartOfAccount && $account->chartOfAccount->is_parent == 0)
                                            <option value="{{ $account->id }}" {{ old('bank_account_id', $deposit->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="deposit_date" :value="__('تاريخ الإيداع')" />
                                <x-text-input id="deposit_date" class="block mt-1 w-full" type="date" name="deposit_date" :value="old('deposit_date', $deposit->deposit_date->format('Y-m-d'))" required />
                                <x-input-error :messages="$errors->get('deposit_date')" class="mt-2" />
                            </div>

                            <!-- المبلغ -->
                            <div>
                                <x-input-label for="amount" :value="__('المبلغ')" />
                                <x-text-input id="amount" class="block mt-1 w-full" type="number" step="0.01" name="amount" :value="old('amount', $deposit->amount)" required />
                                <x-input-error :messages="$errors->get('amount')" class="mt-2" />
                            </div>

                            <!-- رقم الإيداع -->
                            <div>
                                <x-input-label for="deposit_number" :value="__('رقم الإيداع')" />
                                <x-text-input id="deposit_number" class="block mt-1 w-full" type="text" name="deposit_number" :value="old('deposit_number', $deposit->deposit_number)" required readonly />
                                <x-input-error :messages="$errors->get('deposit_number')" class="mt-2" />
                            </div>

                            <!-- رقم المرجع -->
                            <div>
                                <x-input-label for="reference_number" :value="__('رقم المرجع')" />
                                <x-text-input id="reference_number" class="block mt-1 w-full" type="text" name="reference_number" :value="old('reference_number', $deposit->reference_number)" />
                                <x-input-error :messages="$errors->get('reference_number')" class="mt-2" />
                            </div>

                            <!-- طريقة الإيداع -->
                            <div>
                                <x-input-label for="deposit_method" :value="__('طريقة الإيداع')" />
                                <select id="deposit_method" name="deposit_method" class="select2 mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" required>
                                    <option value="">{{ __('اختر طريقة الإيداع') }}</option>
                                    <option value="cash" {{ old('deposit_method', $deposit->deposit_method) == 'cash' ? 'selected' : '' }}>{{ __('نقدي') }}</option>
                                    <option value="check" {{ old('deposit_method', $deposit->deposit_method) == 'check' ? 'selected' : '' }}>{{ __('شيك') }}</option>
                                    <option value="transfer" {{ old('deposit_method', $deposit->deposit_method) == 'transfer' ? 'selected' : '' }}>{{ __('تحويل بنكي') }}</option>
                                    <option value="other" {{ old('deposit_method', $deposit->deposit_method) == 'other' ? 'selected' : '' }}>{{ __('أخرى') }}</option>
                                </select>
                                <x-input-error :messages="$errors->get('deposit_method')" class="mt-2" />
                            </div>

                            <!-- اسم المودع -->
                            <div>
                                <x-input-label for="depositor_name" :value="__('اسم المودع')" />
                                <x-text-input id="depositor_name" class="block mt-1 w-full" type="text" name="depositor_name" :value="old('depositor_name', $deposit->depositor_name)" />
                                <x-input-error :messages="$errors->get('depositor_name')" class="mt-2" />
                            </div>

                            <!-- الحساب المصدر -->
                            <div>
                                <x-input-label for="chart_of_account_id" :value="__('الحساب المصدر')" />
                                <select id="chart_of_account_id" name="chart_of_account_id" class="select2 mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" required>
                                    <option value="">{{ __('اختر الحساب المصدر') }}</option>
                                    @foreach($accounts as $account)
                                        <option value="{{ $account->id }}" {{ old('chart_of_account_id', $deposit->chart_of_account_id) == $account->id ? 'selected' : '' }}>
                                            {{ $account->name }} ({{ $account->code }})
                                        </option>
                                    @endforeach
                                </select>
                                <x-input-error :messages="$errors->get('chart_of_account_id')" class="mt-2" />
                            </div>

                            <!-- مركز التكلفة -->
                            <div>
                                <x-input-label for="cost_center_id" :value="__('مركز التكلفة')" />
                                <select id="cost_center_id" name="cost_center_id" class="select2 mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">
                                    <option value="">{{ __('اختر مركز التكلفة (اختياري)') }}</option>
                                    @foreach($costCenters as $costCenter)
                                        <option value="{{ $costCenter->id }}" {{ old('cost_center_id', $deposit->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>

                        <!-- الوصف -->
                        <div>
                            <x-input-label for="description" :value="__('الوصف')" />
                            <textarea id="description" name="description" rows="3" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500">{{ old('description', $deposit->description) }}</textarea>
                            <x-input-error :messages="$errors->get('description')" class="mt-2" />
                        </div>

                        <!-- المرفقات -->
                        <div>
                            <x-input-label for="attachments" :value="__('المرفقات الجديدة')" />
                            <input id="attachments" type="file" name="attachments[]" multiple class="mt-1 block w-full">
                            <x-input-error :messages="$errors->get('attachments')" class="mt-2" />
                        </div>

                        @if($deposit->attachments && count($deposit->attachments) > 0)
                            <div class="border-t pt-4">
                                <h3 class="text-lg font-semibold text-gray-800 mb-4">{{ __('المرفقات الحالية') }}</h3>
                                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                                    @foreach($deposit->attachments as $attachment)
                                        <div class="border rounded-lg p-3">
                                            <div class="flex items-center justify-between">
                                                <span class="text-sm font-medium truncate">{{ $attachment->original_name }}</span>
                                                <div class="flex space-y-2 space-y-reverse">
                                                    <a href="{{ route('attachments.download', $attachment) }}" class="text-blue-600 hover:text-blue-800">
                                                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path>
                                                        </svg>
                                                    </a>
                                                    <button type="button" onclick="document.getElementById('delete-attachment-{{ $attachment->id }}').submit();" class="text-red-600 hover:text-red-800">
                                                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                                                        </svg>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                        <form id="delete-attachment-{{ $attachment->id }}" action="{{ route('attachments.destroy', $attachment) }}" method="POST" class="hidden">
                                            @csrf
                                            @method('DELETE')
                                        </form>
                                    @endforeach
                                </div>
                            </div>
                        @endif

                        <div class="flex items-center justify-end mt-4">
                            <x-primary-button class="ms-3">
                                {{ __('حفظ التغييرات') }}
                            </x-primary-button>
                            <a href="{{ route('banking.deposits.show', $deposit) }}" class="inline-flex items-center p-4 py-2 bg-gray-300 border border-transparent rounded-md font-semibold text-s text-gray-800 uppercase tracking-widest hover:bg-gray-400 active:bg-gray-500 focus:outline-none focus:border-gray-500 focus:ring ring-gray-300 disabled:opacity-25 transition ease-in-out duration-150">
                                {{ __('إلغاء') }}
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>