@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-2xl mx-auto">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900 mb-2">إضافة ترجمة جديدة</h1>
            <p class="text-gray-600">قم بإضافة ترجمة جديدة للنظام</p>
        </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">
                    <ul class="text-red-700 font-medium list-disc list-inside">
                        @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        </div>
        @endif

        <form action="{{ route('localization.translations.store') }}" method="POST" class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            @csrf

            <div class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                    <div>
                        <label for="language_id" class="block text-sm font-medium text-gray-700 mb-2">اللغة <span class="text-red-600">*</span></label>
                        <select name="language_id" id="language_id" class="w-full rounded-lg border-gray-300 border px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500" required>
                            <option value="">اختر اللغة</option>
                            @foreach($languages as $lang)
                            <option value="{{ $lang->id }}" @selected(old('language_id') == $lang->id)>
                                {{ $lang->flag_emoji }} {{ $lang->name }}
                            </option>
                            @endforeach
                        </select>
                        @error('language_id')
                        <span class="text-red-500 text-sm mt-1">{{ $message }}</span>
                        @enderror
                    </div>

                    <div>
                        <label for="translation_key_id" class="block text-sm font-medium text-gray-700 mb-2">المفتاح <span class="text-red-600">*</span></label>
                        <select name="translation_key_id" id="translation_key_id" class="w-full select2-search" data-placeholder="ابحث عن المفتاح..." required>
                            <option value="">اختر المفتاح</option>
                            @foreach($translationKeys->groupBy('group') as $group => $keys)
                            <optgroup label="{{ $group }}">
                                @foreach($keys as $key)
                                <option value="{{ $key->id }}" @selected(old('translation_key_id', $translationKeyId) == $key->id)>
                                    {{ $key->key }}
                                </option>
                                @endforeach
                            </optgroup>
                            @endforeach
                        </select>
                        @error('translation_key_id')
                        <span class="text-red-500 text-sm mt-1">{{ $message }}</span>
                        @enderror
                    </div>
                </div>

                <div class="mb-6">
                    <label for="value" class="block text-sm font-medium text-gray-700 mb-2">الترجمة <span class="text-red-600">*</span></label>
                    <textarea name="value" id="value" rows="6" class="w-full rounded-lg border-gray-300 border px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="أدخل الترجمة هنا" required>{{ old('value') }}</textarea>
                    @error('value')
                    <span class="text-red-500 text-sm mt-1">{{ $message }}</span>
                    @enderror
                    <p class="text-gray-500 text-sm mt-1">يمكنك استخدام متغيرات مثل :name, :email, إلخ</p>
                </div>

                <div class="mb-6">
                    <label for="notes" class="block text-sm font-medium text-gray-700 mb-2">ملاحظات</label>
                    <textarea name="notes" id="notes" rows="3" class="w-full rounded-lg border-gray-300 border px-4 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="أضف ملاحظات إذا لزم الأمر">{{ old('notes') }}</textarea>
                    @error('notes')
                    <span class="text-red-500 text-sm mt-1">{{ $message }}</span>
                    @enderror
                </div>

                <div class="flex items-center justify-between pt-6 border-t border-gray-200">
                    <a href="{{ route('localization.translations.index') }}" class="text-gray-600 hover:text-gray-900 font-medium">
                        <i class="fas fa-arrow-right ml-2"></i> إلغاء
                    </a>
                    <button type="submit" class="px-6 py-2 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">
                        <i class="fas fa-save ml-2"></i> حفظ الترجمة
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@push('styles')
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<style>
    .select2-container--default .select2-selection--single {
        height: 40px;
        border: 1px solid #d1d5db;
        border-radius: 0.5rem;
    }

    .select2-container--default.select2-container--focus .select2-selection--single {
        border-color: #3b82f6;
        box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
    }

    .select2-container--default .select2-selection--single .select2-selection__rendered {
        line-height: 38px;
        padding-right: 8px;
    }

    .select2-dropdown {
        border: 1px solid #d1d5db;
        border-top: none;
        border-radius: 0 0 0.5rem 0.5rem;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }

    .select2-results__option {
        padding: 10px 16px;
    }

    .select2-results__option--highlighted[aria-selected] {
        background-color: #3b82f6 !important;
    }

    .select2-results__option[aria-selected=true] {
        background-color: #dbeafe;
    }

    .select2-container--default .select2-search--dropdown .select2-search__field {
        border: 1px solid #d1d5db;
        border-radius: 0.375rem;
        padding: 8px 12px;
    }

    .select2-container--default .select2-results > .select2-results__options {
        max-height: 300px;
    }

    .select2-container--default .select2-results__group {
        padding: 0;
        margin: 0;
        font-weight: 600;
        color: #374151;
        background-color: #f9fafb;
        padding: 8px 16px;
    }
</style>
@endpush

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // تفعيل Select2 على حقل المفتاح
        $('#translation_key_id').select2({
            allowClear: true,
            searchInputPlaceholder: 'ابحث...',
            language: {
                noResults: function() {
                    return 'لا توجد نتائج';
                },
                searching: function() {
                    return 'جاري البحث...';
                }
            },
            dropdownParent: $('#translation_key_id').parent(),
            width: '100%'
        });

        // تحسين العرض
        $('#translation_key_id').on('select2:open', function() {
            const searchField = document.querySelector('.select2-search__field');
            if (searchField) {
                searchField.focus();
            }
        });
    });
</script>
@endpush
@endsection
