<x-admin-layout>
    <x-slot name="header">
        <div class="flex items-center justify-between">
            <div>
                <h2 class="font-semibold text-lg text-gray-800 dark:text-gray-200 leading-tight">
                    <i class="fas fa-tachometer-alt mr-2"></i>Adaugă Pachet Reseller Nou
                </h2>
                <p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
                    <a href="{{ route('admin.dashboard') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Dashboard</a>
                    <span class="mx-2">/</span>
                    <a href="{{ route('admin.reseller-plans.index') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Pachete Reseller</a>
                    <span class="mx-2">/</span>
                    <span>Adaugă Pachet</span>
                </p>
            </div>
            <a href="{{ route('admin.reseller-plans.index') }}" class="inline-flex items-center justify-center w-10 h-10 text-gray-600 dark:text-gray-400 hover:text-indigo-600 dark:hover:text-indigo-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition" title="Înapoi la Pachete Reseller">
                <i class="fas fa-arrow-left"></i>
            </a>
        </div>
    </x-slot>

    <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.reseller-plans.store') }}">
                        @csrf

                        <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">
                                    Nume Pachet <span class="text-red-500">*</span>
                                </label>
                                <input type="text" name="name" id="name" value="{{ old('name') }}"
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="Reseller Basic" required>
                                @error('name')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- Price -->
                            <div>
                                <label for="price" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Preț (RON/lună) <span class="text-red-500">*</span>
                                </label>
                                <input type="number" name="price" id="price" value="{{ old('price', 0) }}" step="0.01" min="0"
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="0.00" required>
                                @error('price')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
                            <!-- Max Clients -->
                            <div>
                                <label for="max_clients" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Număr Maxim Clienți <span class="text-red-500">*</span>
                                </label>
                                <input type="number" name="max_clients" id="max_clients" value="{{ old('max_clients', 10) }}" min="1"
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="10" required>
                                @error('max_clients')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- DNS Zones per Client -->
                            <div>
                                <label for="dns_zones_per_client" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Zone DNS / Client <span class="text-red-500">*</span>
                                </label>
                                <input type="number" name="dns_zones_per_client" id="dns_zones_per_client" value="{{ old('dns_zones_per_client', 5) }}" min="1"
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="5" required>
                                @error('dns_zones_per_client')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- Subdomains per Client -->
                            <div>
                                <label for="subdomains_per_client" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Subdomenii / Client <span class="text-red-500">*</span>
                                </label>
                                <input type="number" name="subdomains_per_client" id="subdomains_per_client" value="{{ old('subdomains_per_client', 100) }}" min="1"
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="100" required>
                                @error('subdomains_per_client')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>

                            <!-- Nameservers per Client -->
                            <div>
                                <label for="nameservers_per_client" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Nameservere / Client <span class="text-red-500">*</span>
                                </label>
                                <input type="number" name="nameservers_per_client" id="nameservers_per_client" value="{{ old('nameservers_per_client', 5) }}" min="1"
                                       class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                       placeholder="5" required>
                                @error('nameservers_per_client')
                                    <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                                @enderror
                            </div>
                        </div>

                        <!-- Tipuri Înregistrări DNS Permise -->
                        <div class="mb-6">
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
                                <i class="fas fa-list-check text-blue-600 mr-2"></i>
                                Tipuri Înregistrări DNS Permise
                                <span class="text-xs text-gray-500 dark:text-gray-400 font-normal ml-2">(lasă toate nebifate pentru acces complet)</span>
                            </label>
                            <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-2 max-h-96 overflow-y-auto p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg border border-gray-200 dark:border-gray-700">
                                @php
                                    $recordTypes = [
                                        'A' => ['icon' => 'fa-globe', 'color' => 'blue', 'desc' => 'IPv4 Address'],
                                        'AAAA' => ['icon' => 'fa-globe', 'color' => 'indigo', 'desc' => 'IPv6 Address'],
                                        'AFSDB' => ['icon' => 'fa-database', 'color' => 'purple', 'desc' => 'AFS Database'],
                                        'ALIAS' => ['icon' => 'fa-link', 'color' => 'teal', 'desc' => 'Auto Resolved Alias'],
                                        'CAA' => ['icon' => 'fa-shield-alt', 'color' => 'red', 'desc' => 'Certification Authority'],
                                        'CERT' => ['icon' => 'fa-certificate', 'color' => 'orange', 'desc' => 'Certificate Record'],
                                        'CDNSKEY' => ['icon' => 'fa-key', 'color' => 'yellow', 'desc' => 'Child DNSKEY'],
                                        'CDS' => ['icon' => 'fa-fingerprint', 'color' => 'pink', 'desc' => 'Child DS'],
                                        'CNAME' => ['icon' => 'fa-link', 'color' => 'purple', 'desc' => 'Canonical Name'],
                                        'DNSKEY' => ['icon' => 'fa-key', 'color' => 'yellow', 'desc' => 'DNS Key Record'],
                                        'DNAME' => ['icon' => 'fa-sitemap', 'color' => 'indigo', 'desc' => 'Delegation Name'],
                                        'DS' => ['icon' => 'fa-fingerprint', 'color' => 'pink', 'desc' => 'Delegation Signer'],
                                        'HINFO' => ['icon' => 'fa-info-circle', 'color' => 'gray', 'desc' => 'Host Information'],
                                        'KEY' => ['icon' => 'fa-key', 'color' => 'yellow', 'desc' => 'Key Record'],
                                        'LOC' => ['icon' => 'fa-map-marker-alt', 'color' => 'red', 'desc' => 'Location Record'],
                                        'MX' => ['icon' => 'fa-envelope', 'color' => 'red', 'desc' => 'Mail Exchange'],
                                        'NAPTR' => ['icon' => 'fa-phone', 'color' => 'green', 'desc' => 'Naming Authority Pointer'],
                                        'NS' => ['icon' => 'fa-server', 'color' => 'blue', 'desc' => 'Name Server'],
                                        'NSEC' => ['icon' => 'fa-lock', 'color' => 'red', 'desc' => 'Next Secure'],
                                        'NSEC3' => ['icon' => 'fa-lock', 'color' => 'orange', 'desc' => 'NSEC Version 3'],
                                        'NSEC3PARAM' => ['icon' => 'fa-cog', 'color' => 'gray', 'desc' => 'NSEC3 Parameters'],
                                        'OPENPGPKEY' => ['icon' => 'fa-key', 'color' => 'purple', 'desc' => 'OpenPGP Key'],
                                        'PTR' => ['icon' => 'fa-arrow-left', 'color' => 'pink', 'desc' => 'Pointer Record'],
                                        'RP' => ['icon' => 'fa-user', 'color' => 'blue', 'desc' => 'Responsible Person'],
                                        'RRSIG' => ['icon' => 'fa-signature', 'color' => 'indigo', 'desc' => 'DNSSEC Signature'],
                                        'SOA' => ['icon' => 'fa-certificate', 'color' => 'orange', 'desc' => 'Start of Authority'],
                                        'SPF' => ['icon' => 'fa-shield-alt', 'color' => 'green', 'desc' => 'Sender Policy Framework'],
                                        'SSHFP' => ['icon' => 'fa-fingerprint', 'color' => 'teal', 'desc' => 'SSH Fingerprint'],
                                        'SRV' => ['icon' => 'fa-network-wired', 'color' => 'teal', 'desc' => 'Service Record'],
                                        'TKEY' => ['icon' => 'fa-key', 'color' => 'yellow', 'desc' => 'Transaction Key'],
                                        'TLSA' => ['icon' => 'fa-lock', 'color' => 'green', 'desc' => 'TLS Authentication'],
                                        'TSIG' => ['icon' => 'fa-signature', 'color' => 'purple', 'desc' => 'Transaction Signature'],
                                        'TXT' => ['icon' => 'fa-file-alt', 'color' => 'green', 'desc' => 'Text Record'],
                                        'URI' => ['icon' => 'fa-link', 'color' => 'blue', 'desc' => 'Uniform Resource Identifier'],
                                    ];
                                    $oldTypes = old('allowed_record_types', []);
                                @endphp

                                @foreach($recordTypes as $type => $config)
                                    <label class="flex items-start p-2 border border-gray-200 dark:border-gray-700 rounded-lg hover:border-{{ $config['color'] }}-400 dark:hover:border-{{ $config['color'] }}-500 cursor-pointer transition hover:bg-gray-50 dark:hover:bg-gray-700/30">
                                        <input type="checkbox"
                                               name="allowed_record_types[]"
                                               value="{{ $type }}"
                                               {{ in_array($type, $oldTypes) ? 'checked' : '' }}
                                               class="mt-0.5 rounded border-gray-300 dark:border-gray-700 text-{{ $config['color'] }}-600 focus:ring-{{ $config['color'] }}-500">
                                        <span class="ml-2 flex-1">
                                            <span class="flex items-center">
                                                <i class="fas {{ $config['icon'] }} text-{{ $config['color'] }}-600 dark:text-{{ $config['color'] }}-400 text-xs mr-1"></i>
                                                <span class="font-semibold text-gray-900 dark:text-gray-100 text-sm">{{ $type }}</span>
                                            </span>
                                            <span class="block text-xs text-gray-500 dark:text-gray-400 leading-tight">{{ $config['desc'] }}</span>
                                        </span>
                                    </label>
                                @endforeach
                            </div>
                            <p class="mt-3 text-xs text-gray-500 dark:text-gray-400 bg-blue-50 dark:bg-blue-900/20 p-3 rounded-lg border border-blue-200 dark:border-blue-800">
                                <i class="fas fa-info-circle mr-1 text-blue-600"></i>
                                <strong>Notă:</strong> Dacă niciun tip nu este selectat, clienții reseller vor putea crea toate tipurile de înregistrări DNS.
                            </p>
                        </div>

                        <!-- DNS Templates Permise -->
                        <div class="mb-6">
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
                                <i class="fas fa-file-code text-purple-600 mr-2"></i>
                                DNS Templates Permise
                                <span class="text-xs text-gray-500 dark:text-gray-400 font-normal ml-2">(lasă toate nebifate pentru acces complet)</span>
                            </label>
                            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3 max-h-64 overflow-y-auto p-4 bg-gray-50 dark:bg-gray-900/50 rounded-lg border border-gray-200 dark:border-gray-700">
                                @php
                                    $templates = \App\Models\DnsTemplate::orderBy('name')->get();
                                    $oldTemplates = old('allowed_templates', []);
                                @endphp

                                @forelse($templates as $template)
                                    <label class="flex items-start p-3 border border-gray-200 dark:border-gray-700 rounded-lg hover:border-purple-400 dark:hover:border-purple-500 cursor-pointer transition hover:bg-white dark:hover:bg-gray-800">
                                        <input type="checkbox"
                                               name="allowed_templates[]"
                                               value="{{ $template->id }}"
                                               {{ in_array($template->id, $oldTemplates) ? 'checked' : '' }}
                                               class="mt-0.5 rounded border-gray-300 dark:border-gray-700 text-purple-600 focus:ring-purple-500">
                                        <span class="ml-2 flex-1">
                                            <span class="flex items-center">
                                                <i class="fas fa-file-code text-purple-600 dark:text-purple-400 text-xs mr-1"></i>
                                                <span class="font-semibold text-gray-900 dark:text-gray-100 text-sm">{{ $template->name }}</span>
                                            </span>
                                            @if($template->description)
                                                <span class="block text-xs text-gray-500 dark:text-gray-400 leading-tight">{{ Str::limit($template->description, 50) }}</span>
                                            @endif
                                            <span class="block text-xs text-gray-400 dark:text-gray-500 mt-1">
                                                {{ $template->dns_records_count ?? $template->dnsRecords()->count() }} înregistrări
                                            </span>
                                        </span>
                                    </label>
                                @empty
                                    <div class="col-span-full text-center py-8 text-gray-500 dark:text-gray-400">
                                        <i class="fas fa-file-code text-4xl mb-2 opacity-50"></i>
                                        <p>Nu există template-uri DNS în sistem.</p>
                                    </div>
                                @endforelse
                            </div>
                            <p class="mt-3 text-xs text-gray-500 dark:text-gray-400 bg-purple-50 dark:bg-purple-900/20 p-3 rounded-lg border border-purple-200 dark:border-purple-800">
                                <i class="fas fa-info-circle mr-1 text-purple-600"></i>
                                <strong>Notă:</strong> Dacă niciun template nu este selectat, clienții reseller vor putea folosi toate template-urile DNS.
                            </p>
                        </div>

                        <!-- Description -->
                        <div class="mb-6">
                            <label for="description" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                Descriere
                            </label>
                            <textarea name="description" id="description" rows="3"
                                      class="w-full rounded-lg border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300"
                                      placeholder="Descriere opțională a pachetului">{{ old('description') }}</textarea>
                            @error('description')
                                <p class="mt-2 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                            @enderror
                        </div>

                        <!-- Is Active -->
                        <div class="mb-6">
                            <label class="flex items-center">
                                <input type="checkbox" name="is_active" value="1" {{ old('is_active', true) ? 'checked' : '' }}
                                       class="rounded border-gray-300 text-blue-600 shadow-sm focus:ring-blue-500">
                                <span class="ml-2 text-sm text-gray-700 dark:text-gray-300">Pachet Activ</span>
                            </label>
                        </div>

                        <div class="flex items-center justify-end gap-4">
                            <a href="{{ route('admin.reseller-plans.index') }}" class="text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-200">
                                Anulează
                            </a>
                            <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-lg">
                                <i class="fas fa-save mr-2"></i>
                                Salvează Pachetul
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-admin-layout>

