@extends('layouts.app')

@section('title', \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.EditSupplier', 'تعديل بيانات المورد'))

@section('content')
<div class="container m-auto p-4 py-6">
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-2l font-semibold text-gray-800">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Edit', 'تعديل') }}: {{ $supplier->name }}</h1>
        <div class="flex space-y-2 space-y-reverse">
            <a href="{{ route('suppliers.index') }}" class="bg-gray-200 hover:bg-gray-300 text-gray-800 font-bold py-2 p-4 rounded ml-2">
                <i class="fas fa-arrow-right ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.BackToList', 'العودة للقائمة') }}
            </a>
            <a href="{{ route('suppliers.show', $supplier) }}" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 p-4 rounded">
                <i class="fas fa-eye ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.View', 'عرض') }}
            </a>
        </div>
    </div>

    <div class="bg-white rounded-lg shadow overflow-hidden">
        <form action="{{ route('suppliers.update', $supplier) }}" method="POST" class="p-6">
            @csrf
            @method('PUT')
            
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div>
                    <label for="name" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.SupplierName', 'اسم المورد') }} <span class="text-red-600">*</span></label>
                    <input type="text" name="name" id="name" value="{{ old('name', $supplier->name) }}" required
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('name')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="contact_person" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.ContactPerson', 'الشخص المسؤول') }}</label>
                    <input type="text" name="contact_person" id="contact_person" value="{{ old('contact_person', $supplier->contact_person) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('contact_person')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="email" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Email', 'البريد الإلكتروني') }}</label>
                    <input type="email" name="email" id="email" value="{{ old('email', $supplier->email) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('email')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="phone" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Phone', 'الهاتف') }}</label>
                    <input type="text" name="phone" id="phone" value="{{ old('phone', $supplier->phone) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('phone')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="mobile" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Mobile', 'الجوال') }}</label>
                    <input type="text" name="mobile" id="mobile" value="{{ old('mobile', $supplier->mobile) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('mobile')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="address" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Address', 'العنوان') }}</label>
                    <input type="text" name="address" id="address" value="{{ old('address', $supplier->address) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('address')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="city" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.City', 'المدينة') }}</label>
                    <input type="text" name="city" id="city" value="{{ old('city', $supplier->city) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('city')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="postal_code" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.PostalCode', 'الرمز البريدي') }}</label>
                    <input type="text" name="postal_code" id="postal_code" value="{{ old('postal_code', $supplier->postal_code) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('postal_code')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="country" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Country', 'الدولة') }}</label>
                    <input type="text" name="country" id="country" value="{{ old('country', $supplier->country) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('country')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="tax_number" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.TaxNumber', 'الرقم الضريبي') }}</label>
                    <input type="text" name="tax_number" id="tax_number" value="{{ old('tax_number', $supplier->tax_number) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('tax_number')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="commercial_register" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.BusinessRegistrationNumber', 'رقم السجل التجاري') }}</label>
                    <input type="text" name="commercial_register" id="commercial_register" value="{{ old('commercial_register', $supplier->commercial_register) }}"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                    @error('commercial_register')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div>
                    <label for="status" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Status', 'الحالة') }} <span class="text-red-600">*</span></label>
                    <select name="status" id="status" required
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">
                        <option value="active" {{ old('status', $supplier->status) == 'active' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Active', 'نشط') }}</option>
                        <option value="inactive" {{ old('status', $supplier->status) == 'inactive' ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Inactive', 'غير نشط') }}</option>
                    </select>
                    @error('status')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
                <div>
                    <label for="linked_customer_id" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.LinkedCustomer', 'عميل مرتبط (إذا كان المورد عميلاً أيضاً)') }}</label>
                    <select name="linked_customer_id" id="linked_customer_id"
                        class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500 select2">
                        <option value="">{{ \App\Helpers\TranslationHelper::transFromDatabase('General.SelectCustomer', 'اختر العميل') }}</option>
                        @foreach($customers as $customer)
                            <option value="{{ $customer->id }}" {{ old('linked_customer_id', $supplier->linked_customer_id) == $customer->id ? 'selected' : '' }}>{{ $customer->name }}</option>
                        @endforeach
                    </select>
                    @error('linked_customer_id')
                    <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                    @enderror
                </div>
            </div>

            <div class="mt-6">
                <label for="notes" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Notes', 'ملاحظات') }}</label>
                <textarea name="notes" id="notes" rows="3"
                    class="w-full p-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-primary-500 focus:border-primary-500">{{ old('notes', $supplier->notes) }}</textarea>
                @error('notes')
                <p class="text-red-500 text-s mt-1">{{ $message }}</p>
                @enderror
            </div>

            <div class="mt-6 flex justify-end">
                <button type="submit" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded">
                    <i class="fas fa-save ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Suppliers.Save', 'حفظ') }}
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
