<x-app-layout>
    <x-slot name="header">
        <div class="flex items-center justify-between">
            <h2 class="font-semibold text-lg text-gray-800 dark:text-gray-200 leading-tight">
                {{ __('Add Record') }} PowerDNS - {{ $zoneName }}
            </h2>
            <a href="{{ route('dns-zones.show', $zoneName) }}" class="text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-200">
                <i class="fas fa-arrow-left mr-2"></i>
                Înapoi
            </a>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-3xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    <div class="mb-6 p-4 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg">
                        <div class="flex items-center">
                            <i class="fas fa-server text-green-600 dark:text-green-400 mr-2"></i>
                            <span class="text-green-800 dark:text-green-200 font-medium">Nouă înregistrare PowerDNS</span>
                        </div>
                        <p class="text-green-700 dark:text-green-300 text-sm mt-1">
                            Această înregistrare va fi creată direct în PowerDNS și va fi activă imediat.
                        </p>
                    </div>

                    <form method="POST" action="{{ route('powerdns.records.store', $zoneName) }}">
                        @csrf

                        <!-- Name -->
                        <div class="mb-6">
                            <label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                Nume <span class="text-red-500">*</span>
                            </label>
                            <div class="flex items-center">
                                <input type="text"
                                       name="name"
                                       id="name"
                                       value="{{ old('name') }}"
                                       class="flex-1 rounded-l-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600"
                                       placeholder="www, mail, ftp sau @ pentru root"
                                       required>
                                <span class="px-4 py-2 bg-gray-100 dark:bg-gray-700 border border-l-0 border-gray-300 dark:border-gray-600 rounded-r-lg text-gray-600 dark:text-gray-400">
                                    .{{ $zoneName }}
                                </span>
                            </div>
                            @error('name')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-500">{{ $message }}</p>
                            @enderror
                            <p class="mt-2 text-sm text-gray-500 dark:text-gray-400">
                                Introduceți @ pentru a crea o înregistrare pentru domeniul root
                            </p>
                        </div>

                        <!-- Type -->
                        <div class="mb-6">
                            <label for="type" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                {{ __('Record Type') }} <span class="text-red-500">*</span>
                            </label>
                            <div class="relative">
                                <input type="text"
                                       id="type-input"
                                       placeholder="Selectează sau caută tip înregistrare..."
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600 pr-10"
                                       autocomplete="off"
                                       value="{{ old('type') }}">
                                <input type="hidden" name="type" id="type" value="{{ old('type') }}" required>
                                <div class="absolute inset-y-0 right-0 flex items-center pr-3 pointer-events-none">
                                    <svg class="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
                                    </svg>
                                </div>
                                <div id="type-dropdown" class="hidden absolute z-10 w-full mt-1 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg shadow-lg max-h-60 overflow-auto">
                                    <div id="type-options">
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="A" data-label="A - IPv4 Address">A - IPv4 Address</div>
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="AAAA" data-label="AAAA - IPv6 Address">AAAA - IPv6 Address</div>
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="CNAME" data-label="CNAME - Canonical Name">CNAME - Canonical Name</div>
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="MX" data-label="MX - Mail Exchange">MX - Mail Exchange</div>
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="TXT" data-label="TXT - Text Record">TXT - Text Record</div>
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="NS" data-label="NS - Name Server">NS - Name Server</div>
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="PTR" data-label="PTR - Pointer Record">PTR - Pointer Record</div>
                                        <div class="px-4 py-2 cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-900/30 text-gray-900 dark:text-gray-100" data-value="SRV" data-label="SRV - Service Record">SRV - Service Record</div>
                                    </div>
                                </div>
                            </div>
                            @error('type')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-500">{{ $message }}</p>
                            @enderror
                        </div>

                        <!-- Content -->
                        <div class="mb-6">
                            <label for="content" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                {{ __('Content') }} <span class="text-red-500">*</span>
                            </label>
                            <textarea name="content"
                                      id="content"
                                      rows="3"
                                      class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600"
                                      placeholder="Conținutul înregistrării"
                                      required>{{ old('content') }}</textarea>
                            @error('content')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-500">{{ $message }}</p>
                            @enderror
                            <div class="mt-2 text-sm text-gray-500 dark:text-gray-400">
                                <div id="content-help">
                                    <p id="help-default">Selectați tipul înregistrării pentru a vedea exemple</p>
                                    <p id="help-A" class="hidden">Introduceți adresa IPv4 (ex: 192.168.1.1)</p>
                                    <p id="help-AAAA" class="hidden">Introduceți adresa IPv6 (ex: 2001:db8::1)</p>
                                    <p id="help-CNAME" class="hidden">Introduceți numele domeniului țintă (ex: target.example.com.)</p>
                                    <p id="help-MX" class="hidden">Introduceți prioritatea și serverul de mail (ex: 10 mail.example.com.)</p>
                                    <p id="help-TXT" class="hidden">Introduceți textul înregistrării (ex: "v=spf1 include:_spf.google.com ~all")</p>
                                    <p id="help-NS" class="hidden">Introduceți numele serverului (ex: ns1.example.com.)</p>
                                    <p id="help-PTR" class="hidden">Introduceți numele domeniului pentru reverse DNS (ex: example.com.)</p>
                                    <p id="help-SRV" class="hidden">Introduceți prioritatea, greutatea, portul și ținta (ex: 10 5 443 target.example.com.)</p>
                                </div>
                            </div>
                        </div>

                        <!-- TTL -->
                        <div class="mb-6">
                            <label for="ttl" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                TTL (secunde) <span class="text-red-500">*</span>
                            </label>
                            <select name="ttl"
                                    id="ttl"
                                    class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600"
                                    required>
                                <option value="60" {{ old('ttl') == 60 ? 'selected' : '' }}>1 minut (60)</option>
                                <option value="300" {{ old('ttl', 300) == 300 ? 'selected' : '' }}>5 minute (300)</option>
                                <option value="1800" {{ old('ttl') == 1800 ? 'selected' : '' }}>30 minute (1800)</option>
                                <option value="3600" {{ old('ttl') == 3600 ? 'selected' : '' }}>60 minute (3600)</option>
                                <option value="86400" {{ old('ttl') == 86400 ? 'selected' : '' }}>1 zi (86400)</option>
                            </select>
                            @error('ttl')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-500">{{ $message }}</p>
                            @enderror
                        </div>

                        <!-- Notițe -->
                        <div class="mb-6">
                            <label for="notes" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                Notițe (opțional)
                            </label>
                            <textarea name="notes"
                                      id="notes"
                                      rows="3"
                                      class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600"
                                      placeholder="Adaugă notițe sau comentarii despre această înregistrare...">{{ old('notes') }}</textarea>
                            @error('notes')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                            @enderror
                            <p class="mt-2 text-sm text-gray-500 dark:text-gray-400">
                                Poți adăuga notițe sau comentarii pentru a documenta scopul acestei înregistrări
                            </p>
                        </div>

                        <!-- Submit Buttons -->
                        <div class="flex items-center justify-between pt-6 border-t border-gray-200 dark:border-gray-700">
                            <a href="{{ route('dns-zones.show', $zoneName) }}" class="px-4 py-2 bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg hover:bg-gray-300 dark:hover:bg-gray-600 transition">
                                {{ __('Cancel') }}
                            </a>
                            <button type="submit" class="px-6 py-2 bg-green-600 hover:bg-green-700 text-white font-semibold rounded-lg transition">
                                <i class="fas fa-plus mr-2"></i>
                                Creează în PowerDNS
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const input = document.getElementById('type-input');
            const hiddenInput = document.getElementById('type');
            const dropdown = document.getElementById('type-dropdown');
            const optionsContainer = document.getElementById('type-options');
            const allOptions = Array.from(optionsContainer.children);

            input.addEventListener('click', () => {
                dropdown.classList.toggle('hidden');
                filterOptions('');
            });

            input.addEventListener('input', function() {
                dropdown.classList.remove('hidden');
                filterOptions(this.value.toLowerCase());
            });

            function filterOptions(searchTerm) {
                allOptions.forEach(option => {
                    option.style.display = option.dataset.label.toLowerCase().includes(searchTerm) ? 'block' : 'none';
                });
            }

            optionsContainer.addEventListener('click', function(e) {
                const option = e.target.closest('[data-value]');
                if (option) {
                    input.value = option.dataset.label;
                    hiddenInput.value = option.dataset.value;
                    dropdown.classList.add('hidden');
                    
                    // Help text logic
                    const helpElements = document.querySelectorAll('#content-help p');
                    helpElements.forEach(el => el.classList.add('hidden'));
                    const helpEl = document.getElementById('help-' + option.dataset.value);
                    if (helpEl) helpEl.classList.remove('hidden');
                    else document.getElementById('help-default').classList.remove('hidden');
                }
            });

            document.addEventListener('click', (e) => {
                if (!input.contains(e.target) && !dropdown.contains(e.target)) {
                    dropdown.classList.add('hidden');
                }
            });

            input.addEventListener('keydown', function(e) {
                if (e.key === 'Escape') dropdown.classList.add('hidden');
                else if (e.key === 'Enter') {
                    e.preventDefault();
                    const visibleOption = allOptions.find(opt => opt.style.display !== 'none');
                    if (visibleOption) {
                        input.value = visibleOption.dataset.label;
                        hiddenInput.value = visibleOption.dataset.value;
                        dropdown.classList.add('hidden');
                    }
                }
            });
        });
    </script>
</x-app-layout>
