@extends('layouts.app')

@section('title', TranslationHelper::transFromDatabase('Warehouses.EditTitle', __('تعديل المخزن')))

@section('content')
<div class="container m-auto p-4 py-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2l font-semibold text-gray-900">{{ TranslationHelper::transFromDatabase('Warehouses.EditTitle', __('تعديل المخزن')) }}: {{ $warehouse->name }}</h1>
        <a href="{{ route('inventory.warehouses.index') }}" class="bg-gray-200 hover:bg-gray-300 text-gray-800 font-bold py-2 p-4 rounded inline-flex items-center">
            <span>{{ TranslationHelper::transFromDatabase('General.BackToList', __('العودة لقائمة المخازن')) }}</span>
        </a>
    </div>

    <div class="bg-white rounded-lg shadow-md p-6">
        @if ($errors->any())
            <div class="bg-red-100 border border-red-400 text-red-700 p-4 py-3 rounded mb-4">
                <ul class="list-disc list-inside">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form action="{{ route('inventory.warehouses.update', $warehouse) }}" method="POST">
            @csrf
            @method('PUT')
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="mb-4">
                    <label for="name" class="block text-gray-700 font-bold mb-2">{{ TranslationHelper::transFromDatabase('Warehouses.Name', __('اسم المخزن')) }} <span class="text-red-600">*</span></label>
                    <input type="text" id="name" name="name" value="{{ old('name', $warehouse->name) }}" required
                        class="shadow appearance-none border rounded w-full py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline @error('name') border-red-500 @enderror">
                    @error('name')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div class="mb-4">
                    <label for="code" class="block text-gray-700 font-bold mb-2">{{ TranslationHelper::transFromDatabase('Warehouses.Code', __('كود المخزن')) }}</label>
                    <input type="text" id="code" name="code" value="{{ old('code', $warehouse->code) }}"
                        class="shadow appearance-none border rounded w-full py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
                </div>

                <div class="mb-4">
                    <label for="manager_ids" class="block text-gray-700 font-bold mb-2">{{ TranslationHelper::transFromDatabase('Warehouses.Managers', __('مديرو المخزن')) }} <span class="text-red-600">*</span></label>
                    <select id="manager_ids" name="manager_ids[]" multiple required
                        class="shadow appearance-none border rounded w-full py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline @error('manager_ids') border-red-500 @enderror"
                        size="5">
                        <option value="">{{ TranslationHelper::transFromDatabase('General.SelectManagers', __('-- اختر المديرين --')) }}</option>
                        @foreach($users as $user)
                            <option value="{{ $user->id }}" 
                                {{ in_array($user->id, old('manager_ids', $warehouse->managers->pluck('id')->toArray())) ? 'selected' : '' }}>
                                {{ $user->name }}
                            </option>
                        @endforeach
                    </select>
                    <small class="text-gray-600">{{ TranslationHelper::transFromDatabase('General.SelectManagersHint', __('اختر مدير واحد على الأقل. استخدم Ctrl/Cmd + Click لاختيار أكثر من شخص')) }}</small>
                    @error('manager_ids')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                <div class="mb-4">
                    <label for="phone" class="block text-gray-700 font-bold mb-2">{{ TranslationHelper::transFromDatabase('Warehouses.Phone', __('رقم الهاتف')) }}</label>
                    <input type="text" id="phone" name="phone" value="{{ old('phone', $warehouse->phone) }}"
                        class="shadow appearance-none border rounded w-full py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
                </div>

                <div class="mb-4">
                    <label for="manager" class="block text-gray-700 font-bold mb-2">{{ TranslationHelper::transFromDatabase('Warehouses.AdditionalManagerInfo', __('معلومات إضافية عن المدير')) }}</label>
                    <input type="text" id="manager" name="manager" value="{{ old('manager', $warehouse->manager) }}"
                        class="shadow appearance-none border rounded w-full py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
                    <small class="text-gray-600">{{ TranslationHelper::transFromDatabase('General.AdditionalManagerInfoHint', __('مثلاً: درجة الوظيفة أو المسئولية')) }}</small>
                </div>

                <div class="mb-4 col-span-2">
                    <label for="address" class="block text-gray-700 font-bold mb-2">{{ TranslationHelper::transFromDatabase('Warehouses.Address', __('العنوان')) }}</label>
                    <textarea id="address" name="address" rows="3"
                        class="shadow appearance-none border rounded w-full py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">{{ old('address', $warehouse->address) }}</textarea>
                </div>

                <div class="mb-4 col-span-2">
                    <label for="notes" class="block text-gray-700 font-bold mb-2">{{ TranslationHelper::transFromDatabase('Warehouses.Notes', __('ملاحظات')) }}</label>
                    <textarea id="notes" name="notes" rows="3"
                        class="shadow appearance-none border rounded w-full py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">{{ old('notes', $warehouse->notes) }}</textarea>
                </div>

                <div class="mb-4">
                    <div class="flex items-center">
                        <input type="checkbox" id="is_active" name="is_active" value="1" {{ old('is_active', $warehouse->is_active) ? 'checked' : '' }}
                            class="w-4 h-4 text-primary-600 bg-gray-100 border-gray-300 rounded focus:ring-primary-500">
                        <label for="is_active" class="mr-2 text-gray-700 font-bold">{{ TranslationHelper::transFromDatabase('Status.Active', __('نشط')) }}</label>
                    </div>
                </div>
            </div>

            <div class="flex items-center justify-end mt-6">
                <button type="submit" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded focus:outline-none focus:shadow-outline">
                    {{ TranslationHelper::transFromDatabase('General.Update', __('تحديث المخزن')) }}
                </button>
            </div>
        </form>
    </div>
</div>

<style>
    select[multiple] {
        height: auto;
    }
    select[multiple] option {
        padding: 5px;
    }
    select[multiple] option:checked {
        background: linear-gradient(#1e40af, #1e40af);
        background-color: #1e40af;
        color: white;
    }
</style>
@endsection
