﻿<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">
                {{ __('تعديل الأصل') }}: {{ $asset->name }}
            </h2>
            <div class="flex space-y-2 space-y-reverse">
                <a href="{{ route('assets.index') }}" 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>
                <a href="{{ route('assets.show', $asset) }}" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 p-4 rounded">
                    <i class="fas fa-eye 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(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.update', $asset) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <!-- فئة الأصل -->
                            <div>
                                <label for="asset_category_id" class="block text-sm font-medium text-gray-700 mb-1">فئة الأصل <span class="text-red-600">*</span></label>
                                <select id="asset_category_id" name="asset_category_id" 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 @error('asset_category_id') border-red-500 @enderror" required>
                                    <option value="">اختر فئة الأصل</option>
                                    @foreach($categories as $category)
                                        <option value="{{ $category->id }}" {{ (old('asset_category_id', $asset->asset_category_id) == $category->id) ? 'selected' : '' }}>
                                            {{ $category->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('asset_category_id')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- اسم الأصل -->
                            <div>
                                <label for="name" class="block text-sm font-medium text-gray-700 mb-1">اسم الأصل <span class="text-red-600">*</span></label>
                                <input type="text" name="name" id="name" value="{{ old('name', $asset->name) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('name') border-red-500 @enderror" required>
                                @error('name')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- رمز الأصل -->
                            <div>
                                <label for="asset_code" class="block text-sm font-medium text-gray-700 mb-1">رمز الأصل <span class="text-red-600">*</span></label>
                                <input type="text" name="asset_code" id="asset_code" value="{{ old('asset_code', $asset->asset_code) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('asset_code') border-red-500 @enderror" required>
                                @error('asset_code')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- تاريخ الشراء -->
                            <div>
                                <label for="purchase_date" class="block text-sm font-medium text-gray-700 mb-1">تاريخ الشراء <span class="text-red-600">*</span></label>
                                <input type="date" name="purchase_date" id="purchase_date" value="{{ old('purchase_date', $asset->purchase_date->format('Y-m-d')) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('purchase_date') border-red-500 @enderror" required>
                                @error('purchase_date')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- تكلفة الشراء -->
                            <div>
                                <label for="purchase_cost" class="block text-sm font-medium text-gray-700 mb-1">تكلفة الشراء <span class="text-red-600">*</span></label>
                                <input type="number" step="0.01" name="purchase_cost" id="purchase_cost" value="{{ old('purchase_cost', $asset->purchase_cost) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('purchase_cost') border-red-500 @enderror" required>
                                @error('purchase_cost')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- الموقع -->
                            <div>
                                <label for="location" class="block text-sm font-medium text-gray-700 mb-1">الموقع</label>
                                <input type="text" name="location" id="location" value="{{ old('location', $asset->location) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('location') border-red-500 @enderror">
                                @error('location')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- الرقم التسلسلي -->
                            <div>
                                <label for="serial_number" class="block text-sm font-medium text-gray-700 mb-1">الرقم التسلسلي</label>
                                <input type="text" name="serial_number" id="serial_number" value="{{ old('serial_number', $asset->serial_number) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('serial_number') border-red-500 @enderror">
                                @error('serial_number')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- الموديل -->
                            <div>
                                <label for="model" class="block text-sm font-medium text-gray-700 mb-1">الموديل</label>
                                <input type="text" name="model" id="model" value="{{ old('model', $asset->model) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('model') border-red-500 @enderror">
                                @error('model')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- الشركة المصنعة -->
                            <div>
                                <label for="manufacturer" class="block text-sm font-medium text-gray-700 mb-1">الشركة المصنعة</label>
                                <input type="text" name="manufacturer" id="manufacturer" value="{{ old('manufacturer', $asset->manufacturer) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('manufacturer') border-red-500 @enderror">
                                @error('manufacturer')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- المورد -->
                            <div>
                                <label for="supplier" class="block text-sm font-medium text-gray-700 mb-1">المورد</label>
                                <input type="text" name="supplier" id="supplier" value="{{ old('supplier', $asset->supplier) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('supplier') border-red-500 @enderror">
                                @error('supplier')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- فترة الضمان -->
                            <div>
                                <label for="warranty_period" class="block text-sm font-medium text-gray-700 mb-1">فترة الضمان</label>
                                <input type="text" name="warranty_period" id="warranty_period" value="{{ old('warranty_period', $asset->warranty_period) }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('warranty_period') border-red-500 @enderror">
                                @error('warranty_period')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- تاريخ انتهاء الضمان -->
                            <div>
                                <label for="warranty_epiry_date" class="block text-sm font-medium text-gray-700 mb-1">تاريخ انتهاء الضمان</label>
                                <input type="date" name="warranty_epiry_date" id="warranty_epiry_date" value="{{ old('warranty_epiry_date', $asset->warranty_epiry_date ? $asset->warranty_epiry_date->format('Y-m-d') : '') }}" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('warranty_epiry_date') border-red-500 @enderror">
                                @error('warranty_epiry_date')
                                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <!-- الوصف -->
                        <div class="mt-6">
                            <label for="description" class="block text-sm font-medium text-gray-700 mb-1">الوصف</label>
                            <textarea name="description" id="description" rows="3" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('description') border-red-500 @enderror">{{ old('description', $asset->description) }}</textarea>
                            @error('description')
                                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <!-- الملاحظات -->
                        <div class="mt-6">
                            <label for="notes" class="block text-sm font-medium text-gray-700 mb-1">ملاحظات</label>
                            <textarea name="notes" id="notes" rows="3" class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md @error('notes') border-red-500 @enderror">{{ old('notes', $asset->notes) }}</textarea>
                            @error('notes')
                                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <!-- صورة الأصل -->
                        <div class="mt-6">
                            <label for="image" class="block text-sm font-medium text-gray-700 mb-1">صورة الأصل</label>
                            @if($asset->image)
                                <div class="mt-2 mb-4">
                                    <img src="{{ asset('storage/' . $asset->image) }}" alt="{{ $asset->name }}" class="w-48 h-auto rounded">
                                    <p class="text-sm text-gray-500 mt-1">الصورة الحالية</p>
                                </div>
                            @endif
                            <input type="file" name="image" id="image" class="mt-1 block w-full @error('image') border-red-500 @enderror" accept="image/*">
                            <p class="text-sm text-gray-500 mt-1">اترك هذا الحقل فارغًا إذا كنت لا ترغب في تغيير الصورة</p>
                            @error('image')
                                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div class="mt-6">
                            <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">
                                تحديث الأصل
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
