@extends('layouts.app')

@section('title', 'تعديل طلب عرض السعر')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-5xl mx-auto">
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">تعديل طلب عرض السعر</h1>
                    <p class="text-gray-600">{{ $quotationRequest->rq_number }}</p>
                </div>
                <a href="{{ route('quotation-requests.index') }}" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white font-semibold rounded-lg hover:bg-gray-700 transition-all duration-200">
                    <i class="fas fa-arrow-right ml-2"></i>
                    <span>العودة</span>
                </a>
            </div>

            @if ($errors->any())
            <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-circle text-red-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-red-700 font-medium">حدث خطأ في البيانات المدخلة:</p>
                        <ul class="mt-2 list-disc list-inside text-red-600 text-sm">
                            @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                </div>
            </div>
            @endif
        </div>

        <form action="{{ route('quotation-requests.update', $quotationRequest) }}" method="POST" enctype="multipart/form-data" class="space-y-6">
            @csrf
            @method('PUT')

            <!-- Basic Information -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
                <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                    <h2 class="text-xl font-semibold">معلومات الطلب</h2>
                </div>
                <div class="p-6 space-y-4">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label for="deadline" class="block text-sm font-medium text-gray-700 mb-2">الموعد النهائي *</label>
                            <input type="date" id="deadline" name="deadline" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('deadline') border-red-500 @enderror" value="{{ old('deadline', $quotationRequest->deadline->format('Y-m-d')) }}" required>
                            @error('deadline')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div></div>
                    </div>

                    <div>
                        <label for="description" class="block text-sm font-medium text-gray-700 mb-2">الوصف *</label>
                        <textarea id="description" name="description" rows="3" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('description') border-red-500 @enderror" required>{{ old('description', $quotationRequest->description) }}</textarea>
                        @error('description')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label for="notes" class="block text-sm font-medium text-gray-700 mb-2">ملاحظات (اختيارية)</label>
                        <textarea id="notes" name="notes" rows="2" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="أضف أي ملاحظات إضافية">{{ old('notes', $quotationRequest->notes) }}</textarea>
                        @error('notes')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                </div>
            </div>

            <!-- Suppliers Section -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
                <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                    <h2 class="text-xl font-semibold">الموردون المطلوبون *</h2>
                </div>
                <div class="p-6">
                    <div class="mb-4">
                        <p class="text-sm text-gray-600 mb-4">اختر الموردين الذين تريد طلب عروض أسعار منهم</p>
                    </div>
                    <div class="space-y-3">
                        @forelse($suppliers as $supplier)
                        <div class="flex items-center">
                            <input type="checkbox" id="supplier_{{ $supplier->id }}" name="suppliers[]" value="{{ $supplier->id }}" class="w-4 h-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500" {{ in_array($supplier->id, old('suppliers', $selectedSuppliers)) ? 'checked' : '' }}>
                            <label for="supplier_{{ $supplier->id }}" class="mr-3 text-gray-700 cursor-pointer flex-grow">{{ $supplier->name }}</label>
                        </div>
                        @empty
                        <p class="text-gray-600">لا توجد موردين متاحين</p>
                        @endforelse
                    </div>
                    @error('suppliers')
                    <p class="text-red-500 text-sm mt-2">{{ $message }}</p>
                    @enderror
                </div>
            </div>

            <!-- Items Section -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
                <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white flex justify-between items-center">
                    <h2 class="text-xl font-semibold">الأصناف المطلوبة</h2>
                    <button type="button" class="inline-flex items-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-all" onclick="addItemRow()">
                        <i class="fas fa-plus ml-2"></i>
                        <span>إضافة صنف</span>
                    </button>
                </div>

                <div class="p-6">
                    <div id="itemsContainer" class="space-y-4">
                        @forelse($quotationRequest->items as $index => $item)
                        <div class="itemRow border border-gray-300 rounded-lg p-4">
                            <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">الصنف *</label>
                                    <select name="items[{{ $index }}][item_id]" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
                                        <option value="">اختر صنف</option>
                                        @foreach($items as $it)
                                        <option value="{{ $it->id }}" {{ $item->item_id == $it->id ? 'selected' : '' }}>{{ $it->name }}</option>
                                        @endforeach
                                    </select>
                                </div>

                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">الكمية *</label>
                                    <input type="number" name="items[{{ $index }}][quantity]" step="0.01" min="0.01" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" value="{{ $item->quantity }}" required>
                                </div>

                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">الوحدة *</label>
                                    <select name="items[{{ $index }}][unit_id]" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
                                        <option value="">اختر وحدة</option>
                                        @foreach($units as $unit)
                                        <option value="{{ $unit->id }}" {{ $item->unit_id == $unit->id ? 'selected' : '' }}>{{ $unit->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

                            <div class="mb-4">
                                <label class="block text-sm font-medium text-gray-700 mb-2">المواصفات (اختيارية)</label>
                                <textarea name="items[{{ $index }}][specifications]" rows="2" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="أضف المواصفات الخاصة بهذا الصنف">{{ $item->specifications }}</textarea>
                            </div>

                            <button type="button" class="inline-flex items-center px-4 py-2 bg-red-100 text-red-600 rounded-lg hover:bg-red-200 transition-all" onclick="removeItemRow(this)">
                                <i class="fas fa-trash ml-2"></i>
                                <span>حذف</span>
                            </button>
                        </div>
                        @empty
                        <div class="itemRow border border-gray-300 rounded-lg p-4">
                            <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">الصنف *</label>
                                    <select name="items[0][item_id]" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
                                        <option value="">اختر صنف</option>
                                        @foreach($items as $it)
                                        <option value="{{ $it->id }}">{{ $it->name }}</option>
                                        @endforeach
                                    </select>
                                </div>

                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">الكمية *</label>
                                    <input type="number" name="items[0][quantity]" step="0.01" min="0.01" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
                                </div>

                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">الوحدة *</label>
                                    <select name="items[0][unit_id]" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
                                        <option value="">اختر وحدة</option>
                                        @foreach($units as $unit)
                                        <option value="{{ $unit->id }}">{{ $unit->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

                            <div class="mb-4">
                                <label class="block text-sm font-medium text-gray-700 mb-2">المواصفات (اختيارية)</label>
                                <textarea name="items[0][specifications]" rows="2" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="أضف المواصفات الخاصة بهذا الصنف"></textarea>
                            </div>

                            <button type="button" class="inline-flex items-center px-4 py-2 bg-red-100 text-red-600 rounded-lg hover:bg-red-200 transition-all" onclick="removeItemRow(this)">
                                <i class="fas fa-trash ml-2"></i>
                                <span>حذف</span>
                            </button>
                        </div>
                        @endforelse
                    </div>
                </div>
            </div>

            <!-- Actions -->
            <div class="flex gap-3 justify-end">
                <a href="{{ route('quotation-requests.index') }}" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white font-semibold rounded-lg hover:bg-gray-700 transition-all duration-200">
                    <i class="fas fa-times ml-2"></i>
                    <span>إلغاء</span>
                </a>
                <button type="submit" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl">
                    <i class="fas fa-check ml-2"></i>
                    <span>حفظ التغييرات</span>
                </button>
            </div>
        </form>
    </div>
</div>

<script>
function addItemRow() {
    const container = document.getElementById('itemsContainer');
    const rowCount = container.querySelectorAll('.itemRow').length;
    
    const newRow = document.createElement('div');
    newRow.className = 'itemRow border border-gray-300 rounded-lg p-4';
    newRow.innerHTML = `
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">الصنف *</label>
                <select name="items[${rowCount}][item_id]" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
                    <option value="">اختر صنف</option>
                    @foreach($items as $it)
                    <option value="{{ $it->id }}">{{ $it->name }}</option>
                    @endforeach
                </select>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">الكمية *</label>
                <input type="number" name="items[${rowCount}][quantity]" step="0.01" min="0.01" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">الوحدة *</label>
                <select name="items[${rowCount}][unit_id]" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required>
                    <option value="">اختر وحدة</option>
                    @foreach($units as $unit)
                    <option value="{{ $unit->id }}">{{ $unit->name }}</option>
                    @endforeach
                </select>
            </div>
        </div>

        <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 mb-2">المواصفات (اختيارية)</label>
            <textarea name="items[${rowCount}][specifications]" rows="2" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="أضف المواصفات الخاصة بهذا الصنف"></textarea>
        </div>

        <button type="button" class="inline-flex items-center px-4 py-2 bg-red-100 text-red-600 rounded-lg hover:bg-red-200 transition-all" onclick="removeItemRow(this)">
            <i class="fas fa-trash ml-2"></i>
            <span>حذف</span>
        </button>
    `;
    
    container.appendChild(newRow);
}

function removeItemRow(button) {
    const container = document.getElementById('itemsContainer');
    if (container.querySelectorAll('.itemRow').length > 1) {
        button.closest('.itemRow').remove();
    } else {
        alert('يجب أن يحتوي الطلب على صنف واحد على الأقل');
    }
}
</script>
@endsection
