@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">أضف أصناف جديدة لعرض السعر المقدم للعميل</p>
                </div>
                <a href="{{ route('sales-quotations.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('sales-quotations.store') }}" method="POST" enctype="multipart/form-data" class="space-y-6">
            @csrf

            <!-- 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="customer_id" class="block text-sm font-medium text-gray-700 mb-2">العميل *</label>
                            <select id="customer_id" name="customer_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('customer_id') border-red-500 @enderror" required>
                                <option value="">اختر عميل</option>
                                @foreach($customers as $customer)
                                <option value="{{ $customer->id }}" {{ old('customer_id') == $customer->id ? 'selected' : '' }}>{{ $customer->name }}</option>
                                @endforeach
                            </select>
                            @error('customer_id')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="requisition_id" class="block text-sm font-medium text-gray-700 mb-2">طلب مبيعات (اختياري)</label>
                            <select id="requisition_id" name="requisition_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                                <option value="">اختر طلب (اختياري)</option>
                                @foreach($requisitions as $requisition)
                                <option value="{{ $requisition->id }}" {{ old('requisition_id') == $requisition->id ? 'selected' : '' }}>{{ $requisition->requisition_number }}</option>
                                @endforeach
                            </select>
                            @error('requisition_id')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label for="validity_date" class="block text-sm font-medium text-gray-700 mb-2">تاريخ الصلاحية *</label>
                            <input type="date" id="validity_date" name="validity_date" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent @error('validity_date') border-red-500 @enderror" value="{{ old('validity_date') }}" required>
                            @error('validity_date')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div></div>
                    </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') }}</textarea>
                        @error('notes')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label for="attachment" class="block text-sm font-medium text-gray-700 mb-2">مرفق (اختياري)</label>
                        <input type="file" id="attachment" name="attachment" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" accept=".pdf,.doc,.docx,.jpg,.jpeg,.png">
                        @error('attachment')
                        <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                        @enderror
                    </div>
                </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(old('items', [[]]) as $index => $item)
                        <div class="itemRow border border-gray-300 rounded-lg p-4">
                            <div class="grid grid-cols-1 md:grid-cols-4 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 }}" {{ old("items.{$index}.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="{{ old("items.{$index}.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 }}" {{ old("items.{$index}.unit_id") == $unit->id ? 'selected' : '' }}>{{ $unit->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 }}][unit_price]" step="0.01" min="0" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" value="{{ old("items.{$index}.unit_price") }}" required>
                                </div>
                            </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-4 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>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">سعر الوحدة *</label>
                                    <input type="number" name="items[0][unit_price]" step="0.01" min="0" 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>

                            <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('sales-quotations.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-4 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>
                <label class="block text-sm font-medium text-gray-700 mb-2">سعر الوحدة *</label>
                <input type="number" name="items[${rowCount}][unit_price]" step="0.01" min="0" 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>

        <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 rows = document.querySelectorAll('.itemRow');
    if (rows.length > 1) {
        button.closest('.itemRow').remove();
    } else {
        alert('يجب أن يحتوي عرض السعر على صنف واحد على الأقل');
    }
}
</script>
@endsection
