@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-orders.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-orders.update', $salesOrder) }}" 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="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', $salesOrder->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="quotation_id" class="block text-sm font-medium text-gray-700 mb-2">عرض سعر (اختياري)</label>
                            <select id="quotation_id" name="quotation_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($quotations as $quotation)
                                <option value="{{ $quotation->id }}" {{ old('quotation_id', $salesOrder->quotation_id) == $quotation->id ? 'selected' : '' }}>{{ $quotation->quotation_number }}</option>
                                @endforeach
                            </select>
                            @error('quotation_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="so_date" class="block text-sm font-medium text-gray-700 mb-2">تاريخ أمر البيع *</label>
                            <input type="date" id="so_date" name="so_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('so_date') border-red-500 @enderror" value="{{ old('so_date', $salesOrder->so_date ? $salesOrder->so_date->format('Y-m-d') : '') }}" required>
                            @error('so_date')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="delivery_date" class="block text-sm font-medium text-gray-700 mb-2">تاريخ التسليم المتوقع *</label>
                            <input type="date" id="delivery_date" name="delivery_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('delivery_date') border-red-500 @enderror" value="{{ old('delivery_date', $salesOrder->delivery_date ? $salesOrder->delivery_date->format('Y-m-d') : '') }}" required>
                            @error('delivery_date')
                            <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                            @enderror
                        </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', $salesOrder->notes) }}</textarea>
                        @error('notes')
                        <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($salesOrder->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", $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_ordered]" 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_ordered", $item->quantity_ordered) }}" 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", $item->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", $item->unit_price) }}" required>
                                </div>
                            </div>

                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">مركز التكلفة (اختياري)</label>
                                    <select name="items[{{ $index }}][cost_center_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($costCenters as $center)
                                        <option value="{{ $center->id }}" {{ old("items.{$index}.cost_center_id", $item->cost_center_id) == $center->id ? 'selected' : '' }}>{{ $center->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

                            <button type="button" class="mt-4 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_ordered]" 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>

                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-medium text-gray-700 mb-2">مركز التكلفة (اختياري)</label>
                                    <select name="items[0][cost_center_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($costCenters as $center)
                                        <option value="{{ $center->id }}">{{ $center->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

                            <button type="button" class="mt-4 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-orders.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_ordered]" 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>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">مركز التكلفة (اختياري)</label>
                <select name="items[${rowCount}][cost_center_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($costCenters as $center)
                    <option value="{{ $center->id }}">{{ $center->name }}</option>
                    @endforeach
                </select>
            </div>
        </div>

        <button type="button" class="mt-4 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
