﻿<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-l text-gray-800 leading-tight">
                {{ __('تسجيل عملية تخلص من أصل') }}
            </h2>
            <div>
                <a href="{{ route('assets.show', $asset->id) }}" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 p-4 rounded">
                    <i class="fas fa-arrow-right ml-1"></i> العودة للأصل
                </a>
            </div>
        </div>
    </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 ($errors->any())
                        <div class="bg-red-100 border border-red-400 text-red-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <strong class="font-bold">يوجد أخطاء في البيانات المدخلة!</strong>
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <!-- معلومات الأصل -->
                    <div class="mb-6 bg-gray-50 p-4 rounded-lg">
                        <h3 class="text-lg font-medium text-gray-700 mb-4">معلومات الأصل</h3>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div>
                                <h4 class="text-sm font-medium text-gray-700 mb-1">اسم الأصل</h4>
                                <p class="text-lg font-semibold">{{ $asset->name }}</p>
                            </div>
                            <div>
                                <h4 class="text-sm font-medium text-gray-700 mb-1">رمز الأصل</h4>
                                <p class="text-lg font-semibold">{{ $asset->asset_code }}</p>
                            </div>
                            <div>
                                <h4 class="text-sm font-medium text-gray-700 mb-1">الفئة</h4>
                                <p class="text-lg font-semibold">{{ $asset->category ? $asset->category->name : 'غير محدد' }}</p>
                            </div>
                            <div>
                                <h4 class="text-sm font-medium text-gray-700 mb-1">تاريخ الشراء</h4>
                                <p class="text-lg font-semibold">{{ $asset->purchase_date->format('Y-m-d') }}</p>
                            </div>
                            <div>
                                <h4 class="text-sm font-medium text-gray-700 mb-1">تكلفة الشراء</h4>
                                <p class="text-lg font-semibold">{{ number_format($asset->purchase_cost, 2) }}</p>
                            </div>
                            <div>
                                <h4 class="text-sm font-medium text-gray-700 mb-1">القيمة الحالية</h4>
                                <p class="text-lg font-semibold">{{ number_format($asset->current_value, 2) }}</p>
                            </div>
                        </div>
                    </div>

                    <!-- نموذج تسجيل عملية التخلص -->
                    <form action="{{ route('assets.disposal.store', $asset->id) }}" method="POST" class="space-y-6">
                        @csrf
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label for="disposal_date" class="block text-sm font-medium text-gray-700 mb-1">تاريخ التخلص <span class="text-red-600">*</span></label>
                                <input type="date" id="disposal_date" name="disposal_date" value="{{ old('disposal_date', date('Y-m-d')) }}" required class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                            </div>
                            
                            <div>
                                <label for="disposal_type" class="block text-sm font-medium text-gray-700 mb-1">نوع التخلص <span class="text-red-600">*</span></label>
                                <select id="disposal_type" name="disposal_type" required class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                                    <option value="">اختر نوع التخلص</option>
                                    <option value="sale" {{ old('disposal_type') == 'sale' ? 'selected' : '' }}>بيع</option>
                                    <option value="scrap" {{ old('disposal_type') == 'scrap' ? 'selected' : '' }}>تخريد</option>
                                    <option value="donation" {{ old('disposal_type') == 'donation' ? 'selected' : '' }}>تبرع</option>
                                    <option value="theft" {{ old('disposal_type') == 'theft' ? 'selected' : '' }}>سرقة</option>
                                    <option value="loss" {{ old('disposal_type') == 'loss' ? 'selected' : '' }}>فقدان</option>
                                    <option value="other" {{ old('disposal_type') == 'other' ? 'selected' : '' }}>أخرى</option>
                                </select>
                            </div>
                            
                            <div>
                                <label for="disposal_amount" class="block text-sm font-medium text-gray-700 mb-1">مبلغ التخلص <span class="text-red-600">*</span></label>
                                <input type="number" id="disposal_amount" name="disposal_amount" value="{{ old('disposal_amount', 0) }}" step="0.01" min="0" required class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                                <p class="text-sm text-gray-500 mt-1">أدخل 0 في حالة التخلص بدون مقابل مادي (مثل التبرع أو الفقدان)</p>
                            </div>
                            
                            <div>
                                <label for="recipient" class="block text-sm font-medium text-gray-700 mb-1">المستلم</label>
                                <input type="text" id="recipient" name="recipient" value="{{ old('recipient') }}" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                            </div>
                        </div>
                        
                        <div>
                            <label for="reason" class="block text-sm font-medium text-gray-700 mb-1">سبب التخلص</label>
                            <textarea id="reason" name="reason" rows="3" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">{{ old('reason') }}</textarea>
                        </div>
                        
                        <div>
                            <label for="notes" class="block text-sm font-medium text-gray-700 mb-1">ملاحظات</label>
                            <textarea id="notes" name="notes" rows="3" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">{{ old('notes') }}</textarea>
                        </div>
                        
                        <div class="flex justify-end">
                            <button type="submit" class="inline-flex justify-center py-2 p-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                                <i class="fas fa-save ml-1"></i> تسجيل عملية التخلص
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- سكريبت لحساب الربح/الخسارة المتوقعة -->
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const disposalAmountInput = document.getElementById('disposal_amount');
            const currentValue = {{ $asset->current_value }};
            
            disposalAmountInput.addEventListener('input', function() {
                const disposalAmount = parseFloat(this.value) || 0;
                const gainLoss = disposalAmount - currentValue;
                
                // يمكن إضافة عنصر لعرض الربح/الخسارة المتوقعة هنا إذا كان مطلوبًا
            });
        });
    </script>
</x-app-layout>