<x-app-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">
                    {{ __('Detalii zona PowerDNS') }}: {{ $zone['name'] }}
                </h2>
            </div>
            <div class="flex space-x-2">
                <a href="{{ route('powerdns.user.index') }}"
                   class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded">
                    ← Înapoi la listă
                </a>
                @if(in_array($assignment->access_level, ['full', 'records_only']))
                    <a href="{{ route('powerdns.user.records', $zoneId) }}"
                       class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
                        Gestionează înregistrări
                    </a>
                @endif
            </div>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900 dark:text-gray-100">
                    @if(session('success'))
                        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">
                            {{ session('success') }}
                        </div>
                    @endif

                    @if(session('error'))
                        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                            {{ session('error') }}
                        </div>
                    @endif

                    <!-- Informații despre zonă -->
                    <div class="mb-8">
                        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                            <div class="bg-blue-50 dark:bg-blue-900 p-4 rounded-lg">
                                <div class="flex items-center">
                                    <div class="p-2 bg-blue-500 rounded-md">
                                        <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 0a9 9 0 01-9 9"></path>
                                        </svg>
                                    </div>
                                    <div class="ml-4">
                                        <h3 class="text-lg font-semibold text-blue-900 dark:text-blue-100">Zona DNS</h3>
                                        <p class="text-blue-700 dark:text-blue-300 font-medium">{{ $zone['name'] }}</p>
                                    </div>
                                </div>
                            </div>

                            <div class="bg-green-50 dark:bg-green-900 p-4 rounded-lg">
                                <div class="flex items-center">
                                    <div class="p-2 bg-green-500 rounded-md">
                                        <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                        </svg>
                                    </div>
                                    <div class="ml-4">
                                        <h3 class="text-lg font-semibold text-green-900 dark:text-green-100">Tip acces</h3>
                                        <p class="text-green-700 dark:text-green-300 font-medium">
                                            @if($assignment->access_level === 'full')
                                                Acces complet
                                            @else
                                                Doar înregistrări
                                            @endif
                                        </p>
                                    </div>
                                </div>
                            </div>

                            <div class="bg-purple-50 dark:bg-purple-900 p-4 rounded-lg">
                                <div class="flex items-center">
                                    <div class="p-2 bg-purple-500 rounded-md">
                                        <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"></path>
                                        </svg>
                                    </div>
                                    <div class="ml-4">
                                        <h3 class="text-lg font-semibold text-purple-900 dark:text-purple-100">Înregistrări</h3>
                                        <p class="text-purple-700 dark:text-purple-300 font-medium">{{ count($records) }} total</p>
                                    </div>
                                </div>
                            </div>

                            <div class="bg-yellow-50 dark:bg-yellow-900 p-4 rounded-lg">
                                <div class="flex items-center">
                                    <div class="p-2 bg-yellow-500 rounded-md">
                                        <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                        </svg>
                                    </div>
                                    <div class="ml-4">
                                        <h3 class="text-lg font-semibold text-yellow-900 dark:text-yellow-100">Atribuit la</h3>
                                        <p class="text-yellow-700 dark:text-yellow-300 font-medium">{{ $assignment->assigned_at->format('d.m.Y H:i') }}</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Lista înregistrărilor DNS -->
                    <div>
                        <div class="flex justify-between items-center mb-4">
                            <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100">
                                Înregistrări DNS
                            </h3>
                        </div>

                        @if(count($records) > 0)
                            <div class="overflow-x-auto">
                                <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                                    <thead class="bg-gray-50 dark:bg-gray-700">
                                        <tr>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                                Nume
                                            </th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                                Tip
                                            </th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                                TTL
                                            </th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                                Valoare
                                            </th>
                                            @if(in_array($assignment->access_level, ['full', 'records_only']))
                                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                                    Acțiuni
                                                </th>
                                            @endif
                                        </tr>
                                    </thead>
                                    <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
                                        @foreach($records as $record)
                                            @foreach($record['records'] as $recordData)
                                                <tr>
                                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-gray-100">
                                                        {{ $record['name'] }}
                                                    </td>
                                                    <td class="px-6 py-4 whitespace-nowrap">
                                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
                                                            @if($record['type'] === 'A') bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200
                                                            @elseif($record['type'] === 'AAAA') bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200
                                                            @elseif($record['type'] === 'CNAME') bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200
                                                            @elseif($record['type'] === 'MX') bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200
                                                            @elseif($record['type'] === 'TXT') bg-indigo-100 text-indigo-800 dark:bg-indigo-900 dark:text-indigo-200
                                                            @elseif($record['type'] === 'NS') bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200
                                                            @elseif($record['type'] === 'SOA') bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-200
                                                            @else bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-200
                                                            @endif">
                                                            {{ $record['type'] }}
                                                        </span>
                                                    </td>
                                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">
                                                        {{ $record['ttl'] }}
                                                    </td>
                                                    <td class="px-6 py-4 text-sm text-gray-900 dark:text-gray-100">
                                                        <div class="max-w-xs truncate" title="{{ $recordData['content'] }}">
                                                            {{ $recordData['content'] }}
                                                        </div>
                                                    </td>
                                                    @if(in_array($assignment->access_level, ['full', 'records_only']))
                                                        <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                                            @if($record['type'] !== 'SOA')
                                                                <form method="POST" action="{{ route('powerdns.user.delete-record', $zoneId) }}" class="inline">
                                                                    @csrf
                                                                    @method('DELETE')
                                                                    <input type="hidden" name="name" value="{{ $record['name'] }}">
                                                                    <input type="hidden" name="type" value="{{ $record['type'] }}">
                                                                    <button type="submit"
                                                                            class="text-red-600 hover:text-red-900 dark:text-red-400 dark:hover:text-red-300"
                                                                            onclick="return confirm('Sigur vrei să ștergi această înregistrare?')">
                                                                        Șterge
                                                                    </button>
                                                                </form>
                                                            @else
                                                                <span class="text-gray-400 dark:text-gray-600">Protejat</span>
                                                            @endif
                                                        </td>
                                                    @endif
                                                </tr>
                                            @endforeach
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        @else
                            <div class="text-center py-8">
                                <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                                </svg>
                                <h3 class="mt-2 text-sm font-medium text-gray-900 dark:text-gray-100">Nu există înregistrări</h3>
                                <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
                                    Această zonă DNS nu conține încă înregistrări.
                                </p>
                            </div>
                        @endif
                    </div>

                    <!-- Informații de atribuire -->
                    <div class="mt-8 p-4 bg-gray-50 dark:bg-gray-700 rounded-lg">
                        <h4 class="text-sm font-medium text-gray-900 dark:text-gray-100 mb-2">
                            Informații despre atribuire
                        </h4>
                        <dl class="grid grid-cols-1 gap-x-4 gap-y-2 sm:grid-cols-3">
                            <div>
                                <dt class="text-sm font-medium text-gray-500 dark:text-gray-400">Atribuit de:</dt>
                                <dd class="text-sm text-gray-900 dark:text-gray-100">{{ $assignment->assignedByUser->name ?? 'Necunoscut' }}</dd>
                            </div>
                            <div>
                                <dt class="text-sm font-medium text-gray-500 dark:text-gray-400">Data atribuirii:</dt>
                                <dd class="text-sm text-gray-900 dark:text-gray-100">{{ $assignment->assigned_at->format('d.m.Y H:i') }}</dd>
                            </div>
                            <div>
                                <dt class="text-sm font-medium text-gray-500 dark:text-gray-400">Nivel de acces:</dt>
                                <dd class="text-sm text-gray-900 dark:text-gray-100">
                                    @if($assignment->access_level === 'full')
                                        Acces complet la zonă
                                    @else
                                        Doar gestionare înregistrări
                                    @endif
                                </dd>
                            </div>
                        </dl>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
