@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-4xl mx-auto">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900 mb-2">تعديل أذن صرف المنتجات</h1>
            <p class="text-gray-600">رقم الأذن: {{ $issue->document_number }}</p>
        </div>

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="p-8">
                <form action="{{ route('inventory.finished-goods-issues.update', $issue) }}" method="POST" class="space-y-6">
                    @csrf
                    @method('PUT')

                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                        <div>
                            <label for="document_number" class="block text-sm font-semibold text-gray-700 mb-2">رقم الأذن</label>
                            <input type="text" 
                                   id="document_number" 
                                   name="document_number" 
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg"
                                   value="{{ $issue->document_number }}"
                                   disabled>
                        </div>

                        <div>
                            <label for="document_date" class="block text-sm font-semibold text-gray-700 mb-2">التاريخ</label>
                            <input type="date" 
                                   id="document_date" 
                                   name="document_date" 
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                                   value="{{ $issue->document_date->format('Y-m-d') }}"
                                   required>
                            @error('document_date')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </div>

                        <div>
                            <label for="warehouse_id" class="block text-sm font-semibold text-gray-700 mb-2">المخزن</label>
                            <select id="warehouse_id" 
                                    name="warehouse_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>
                                @foreach($warehouses as $warehouse)
                                    <option value="{{ $warehouse->id }}" {{ $warehouse->id == $issue->from_warehouse_id ? 'selected' : '' }}>
                                        {{ $warehouse->name }}
                                    </option>
                                @endforeach
                            </select>
                            @error('warehouse_id')
                                <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                            @enderror
                        </div>
                    </div>

                    <div>
                        <label for="recipient_name" class="block text-sm font-semibold text-gray-700 mb-2">الطرف المستقبل</label>
                        <input type="text" 
                               id="recipient_name" 
                               name="recipient_name" 
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                               value="{{ $issue->recipient ? $issue->recipient->name : '' }}"
                               required>
                        @error('recipient_name')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>

                    <div>
                        <label for="customer_invoice_id" class="block text-sm font-semibold text-gray-700 mb-2">
                            <i class="fas fa-file-invoice ml-1 text-blue-600"></i>
                            فاتورة مبيعات العميل <span class="text-red-600">*</span>
                        </label>
                        <select id="customer_invoice_id" 
                                name="customer_invoice_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($customerInvoices as $invoice)
                                <option value="{{ $invoice->id }}" {{ $invoice->id == $issue->customer_invoice_id ? 'selected' : '' }}>
                                    {{ $invoice->invoice_number }} - {{ $invoice->customer_name }}
                                </option>
                            @endforeach
                        </select>
                        @error('customer_invoice_id')
                            <p class="mt-1 text-sm text-red-600">{{ $message }}</p>
                        @enderror
                        <p class="mt-1 text-xs text-gray-500">الفواتير المعروضة هنا هي الفواتير التي تم إرسالها فقط</p>
                    </div>

                    <div>
                        <label for="description" class="block text-sm font-semibold 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">{{ $issue->notes }}</textarea>
                    </div>

                    <div class="flex justify-between gap-4 pt-4 border-t border-gray-200">
                        <a href="{{ route('inventory.finished-goods-issues.index') }}" 
                           class="px-6 py-2 text-gray-700 bg-gray-200 rounded-lg hover:bg-gray-300 transition-colors font-medium">رجوع</a>
                        <button type="submit" 
                                class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors font-medium">حفظ التعديلات</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
