<x-admin-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-lg text-gray-800 dark:text-gray-200 leading-tight">
            <i class="fas fa-edit mr-2"></i>
            Editează Înregistrare în Template: {{ $template->name }}
        </h2>
    </x-slot>

    @push('styles')
    <style>
        /* Scrollbar vizibil pentru lista de tipuri DNS */
        .scrollbar-visible::-webkit-scrollbar {
            width: 12px;
        }
        
        .scrollbar-visible::-webkit-scrollbar-track {
            background: #f1f5f9;
            border-radius: 6px;
        }
        
        .dark .scrollbar-visible::-webkit-scrollbar-track {
            background: #1e293b;
        }
        
        .scrollbar-visible::-webkit-scrollbar-thumb {
            background: #6366f1;
            border-radius: 6px;
            border: 2px solid #f1f5f9;
        }
        
        .dark .scrollbar-visible::-webkit-scrollbar-thumb {
            background: #818cf8;
            border-color: #1e293b;
        }
        
        .scrollbar-visible::-webkit-scrollbar-thumb:hover {
            background: #4f46e5;
        }
        
        .dark .scrollbar-visible::-webkit-scrollbar-thumb:hover {
            background: #a5b4fc;
        }
    </style>
    @endpush

    <div class="py-12">
        <div class="max-w-4xl 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">
                    <form method="POST" action="{{ route('admin.powerdns-templates.records.update', [$template, $record]) }}">
                        @csrf
                        @method('PUT')

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <!-- Name -->
                            <div>
                                <label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    <i class="fas fa-tag mr-1"></i>
                                    Nume *
                                </label>
                                <input type="text" name="name" id="name" value="{{ old('name', $record->name) }}" required 
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="ex: @, www, mail">
                                <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">Folosește @ pentru domeniul principal</p>
                                @error('name')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- Type -->
                            <div>
                                <label for="type" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    <i class="fas fa-list mr-1"></i>
                                    Tip Înregistrare *
                                </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', $record->type) }}">
                                    <input type="hidden" name="type" id="type" value="{{ old('type', $record->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">
                                            @php
                                                $typeDescriptions = [
                                                    'A' => 'A - Adresă IPv4',
                                                    'AAAA' => 'AAAA - Adresă IPv6',
                                                    'AFSDB' => 'AFSDB - AFS Database Record',
                                                    'ALIAS' => 'ALIAS - Auto-resolved alias',
                                                    'CAA' => 'CAA - Certification Authority Authorization',
                                                    'CERT' => 'CERT - Certificate Record',
                                                    'CDNSKEY' => 'CDNSKEY - Child DNSKEY',
                                                    'CDS' => 'CDS - Child DS',
                                                    'CNAME' => 'CNAME - Alias către alt domeniu',
                                                    'DNSKEY' => 'DNSKEY - DNSSEC Public Key',
                                                    'DNAME' => 'DNAME - Delegation Name',
                                                    'DS' => 'DS - Delegation Signer',
                                                    'HINFO' => 'HINFO - Host Information',
                                                    'KEY' => 'KEY - Public Key',
                                                    'LOC' => 'LOC - Location Information',
                                                    'MX' => 'MX - Mail Exchange',
                                                    'NAPTR' => 'NAPTR - Naming Authority Pointer',
                                                    'NS' => 'NS - Name Server',
                                                    'NSEC' => 'NSEC - Next Secure',
                                                    'NSEC3' => 'NSEC3 - NSEC version 3',
                                                    'NSEC3PARAM' => 'NSEC3PARAM - NSEC3 Parameters',
                                                    'OPENPGPKEY' => 'OPENPGPKEY - OpenPGP Public Key',
                                                    'PTR' => 'PTR - Pointer Record',
                                                    'RP' => 'RP - Responsible Person',
                                                    'RRSIG' => 'RRSIG - DNSSEC Signature',
                                                    'SOA' => 'SOA - Start of Authority',
                                                    'SSHFP' => 'SSHFP - SSH Fingerprint',
                                                    'SRV' => 'SRV - Service Record',
                                                    'TKEY' => 'TKEY - Transaction Key',
                                                    'TLSA' => 'TLSA - TLS Authentication',
                                                    'TSIG' => 'TSIG - Transaction Signature',
                                                    'TXT' => 'TXT - Text Record',
                                                    'URI' => 'URI - Uniform Resource Identifier',
                                                ];
                                            @endphp
                                            @foreach($typeDescriptions as $type => $description)
                                                <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="{{ $type }}"
                                                     data-label="{{ $description }}">
                                                    {{ $description }}
                                                </div>
                                            @endforeach
                                        </div>
                                    </div>
                                </div>
                                @error('type')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <!-- Content -->
                        <div class="mb-6">
                            <label for="content" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                <i class="fas fa-file-alt mr-1"></i>
                                Conținut *
                            </label>
                            <textarea name="content" id="content" rows="3" required 
                                      class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                      placeholder="ex: 192.168.1.1 sau mail.example.com">{{ old('content', $record->content) }}</textarea>
                            <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">Valoarea înregistrării (IP, domeniu, text, etc.)</p>
                            @error('content')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                            @enderror
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <!-- TTL -->
                            <div>
                                <label for="ttl" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    <i class="fas fa-clock mr-1"></i>
                                    TTL (secunde) *
                                </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', $record->ttl) == '60' ? 'selected' : '' }}>1 minut (60)</option>
                                    <option value="300" {{ old('ttl', $record->ttl) == '300' ? 'selected' : '' }}>5 minute (300)</option>
                                    <option value="1800" {{ old('ttl', $record->ttl) == '1800' ? 'selected' : '' }}>30 minute (1800)</option>
                                    <option value="3600" {{ old('ttl', $record->ttl) == '3600' ? 'selected' : '' }}>60 minute (3600)</option>
                                    <option value="86400" {{ old('ttl', $record->ttl) == '86400' ? 'selected' : '' }}>1 zi (86400)</option>
                                </select>
                                @error('ttl')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- Priority -->
                            <div>
                                <label for="priority" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    <i class="fas fa-sort-numeric-down mr-1"></i>
                                    Prioritate (opțional)
                                </label>
                                <input type="number" name="priority" id="priority" value="{{ old('priority', $record->priority) }}" min="0" max="65535"
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="ex: 10">
                                <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">Doar pentru înregistrări MX</p>
                                @error('priority')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <div class="flex items-center justify-between">
                            <a href="{{ route('admin.powerdns-templates.show', $template) }}" class="bg-gray-600 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded-lg transition">
                                <i class="fas fa-arrow-left mr-2"></i>
                                Înapoi
                            </a>
                            <button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2 px-4 rounded-lg transition">
                                <i class="fas fa-save mr-2"></i>
                                Actualizează Înregistrare
                            </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.querySelectorAll('[data-value]'));

            // Set initial label
            const initialType = hiddenInput.value;
            if (initialType) {
                const initialOption = allOptions.find(opt => opt.dataset.value === initialType);
                if (initialOption) {
                    input.value = initialOption.dataset.label;
                }
            }

            // Toggle dropdown
            input.addEventListener('click', function() {
                dropdown.classList.toggle('hidden');
                filterOptions('');
            });

            // Search/filter
            input.addEventListener('input', function() {
                const searchTerm = this.value.toLowerCase();
                dropdown.classList.remove('hidden');
                filterOptions(searchTerm);
            });

            // Filter options
            function filterOptions(searchTerm) {
                let hasResults = false;
                allOptions.forEach(option => {
                    const label = option.dataset.label.toLowerCase();
                    if (label.includes(searchTerm)) {
                        option.style.display = 'block';
                        hasResults = true;
                    } else {
                        option.style.display = 'none';
                    }
                });

                if (!hasResults) {
                    optionsContainer.innerHTML = '<div class="px-4 py-2 text-gray-500 dark:text-gray-400">Nu s-au găsit rezultate</div>';
                } else if (optionsContainer.children[0].textContent === 'Nu s-au găsit rezultate') {
                    optionsContainer.innerHTML = '';
                    allOptions.forEach(opt => optionsContainer.appendChild(opt));
                }
            }

            // Select option
            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');
                }
            });

            // Close dropdown when clicking outside
            document.addEventListener('click', function(e) {
                if (!input.contains(e.target) && !dropdown.contains(e.target)) {
                    dropdown.classList.add('hidden');
                }
            });

            // Keyboard navigation
            input.addEventListener('keydown', function(e) {
                if (e.key === 'Escape') {
                    dropdown.classList.add('hidden');
                } else if (e.key === 'Enter') {
                    e.preventDefault();
                    const visibleOption = Array.from(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-admin-layout>
