﻿<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-l text-gray-800 leading-tight">
            {{ __('إضافة إهلاك جديد') }}
        </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">
                    
                    @if(session('error'))
                    <div class="bg-red-100 border border-red-400 text-red-700 p-4 py-3 rounded relative mb-4" role="alert">
                        <span class="block sm:inline">{{ session('error') }}</span>
                    </div>
                    @endif

                    <form action="{{ route('assets.depreciations.store') }}" method="POST">
                        @csrf
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <div>
                                <x-label for="asset_id" :value="__('الأصل')" />
                                <select id="asset_id" name="asset_id" required
                                    class="block mt-1 w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                                    <option value="">{{ __('اختر الأصل') }}</option>
                                    @foreach($assets as $asset)
                                    <option value="{{ $asset->id }}" {{ old('asset_id') == $asset->id ? 'selected' : '' }}>
                                        {{ $asset->name }} ({{ $asset->asset_code }}) - {{ number_format($asset->current_value, 2) }}
                                    </option>
                                    @endforeach
                                </select>
                                @error('asset_id')
                                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <div>
                                <x-label for="depreciation_date" :value="__('تاريخ الإهلاك')" />
                                <x-input id="depreciation_date" type="date" name="depreciation_date" :value="old('depreciation_date', date('Y-m-d'))" required class="block mt-1 w-full" />
                                @error('depreciation_date')
                                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <div>
                                <x-label for="depreciation_amount" :value="__('مبلغ الإهلاك')" />
                                <x-input id="depreciation_amount" type="number" step="0.01" name="depreciation_amount" :value="old('depreciation_amount')" required class="block mt-1 w-full" />
                                @error('depreciation_amount')
                                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <div>
                                <x-label for="notes" :value="__('ملاحظات')" />
                                <textarea id="notes" name="notes" rows="3"
                                    class="block mt-1 w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">{{ old('notes') }}</textarea>
                                @error('notes')
                                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <div class="flex items-center justify-between mt-8">
                            <x-button-link href="{{ route('assets.depreciations.index') }}" class="bg-gray-600 hover:bg-gray-700">
                                {{ __('إلغاء') }}
                            </x-button-link>
                            
                            <x-button type="submit" class="bg-green-600 hover:bg-green-700">
                                {{ __('حفظ') }}
                            </x-button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const assetSelect = document.getElementById('asset_id');
            
            assetSelect.addEventListener('change', function() {
                // يمكن إضافة كود هنا لحساب قيمة الإهلاك تلقائيًا بناءً على الأصل المختار
                // على سبيل المثال، يمكن استخدام AJA لجلب معلومات الأصل وحساب الإهلاك
            });
        });
    </script>
</x-app-layout>
