<x-admin-layout>
    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <div class="flex justify-between items-center mb-6">
                        <h2 class="text-2xl font-bold text-gray-800">
                            <i class="fas fa-globe mr-2 text-cyan-600"></i>
                            Detalii Zonă: <span class="text-cyan-600">{{ $zone['name'] ?? $zoneId }}</span>
                        </h2>
                        <a href="{{ route('admin.powerdns.zones') }}"
                           class="bg-gray-600 hover:bg-gray-700 text-white px-4 py-2 rounded-lg transition">
                            <i class="fas fa-arrow-left mr-2"></i>Înapoi la Zone
                        </a>
                    </div>

                    @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 zonă -->
                    <div class="bg-gray-50 rounded-lg p-6 mb-6">
                        <h3 class="text-lg font-semibold text-gray-800 mb-4">Informații Zonă</h3>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div>
                                <p class="text-gray-600 text-sm">Nume Zonă</p>
                                <p class="text-gray-800 font-semibold">{{ $zone['name'] ?? 'N/A' }}</p>
                            </div>
                            <div>
                                <p class="text-gray-600 text-sm">Tip Zonă</p>
                                <p class="text-gray-800 font-semibold">{{ $zone['kind'] ?? 'N/A' }}</p>
                            </div>
                            <div>
                                <p class="text-gray-600 text-sm">Serial</p>
                                <p class="text-gray-800 font-semibold">{{ $zone['serial'] ?? 'N/A' }}</p>
                            </div>
                            <div>
                                <p class="text-gray-600 text-sm">DNSSEC</p>
                                <p class="text-gray-800 font-semibold">
                                    @if(($zone['dnssec'] ?? false))
                                        <span class="text-green-600"><i class="fas fa-check-circle"></i> Activat</span>
                                    @else
                                        <span class="text-gray-600"><i class="fas fa-times-circle"></i> Dezactivat</span>
                                    @endif
                                </p>
                            </div>
                            <div>
                                <p class="text-gray-600 text-sm">Număr Înregistrări</p>
                                <p class="text-gray-800 font-semibold">{{ count($records) }}</p>
                            </div>
                            <div>
                                <p class="text-gray-600 text-sm">ID Zonă</p>
                                <p class="text-gray-800 font-semibold">{{ $zone['id'] ?? $zoneId }}</p>
                            </div>
                        </div>
                    </div>

                    <!-- Înregistrări DNS -->
                    <div>
                        <h3 class="text-lg font-semibold text-gray-800 mb-4">
                            <i class="fas fa-list mr-2 text-cyan-600"></i>
                            Înregistrări DNS
                        </h3>

                        @if(count($records) > 0)
                            <div class="overflow-x-auto">
                                <table class="min-w-full divide-y divide-gray-200">
                                    <thead class="bg-gray-50">
                                        <tr>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                                Nume
                                            </th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                                Tip
                                            </th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                                TTL
                                            </th>
                                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                                Valoare
                                            </th>
                                            <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
                                                Acțiuni
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody class="bg-white divide-y divide-gray-200">
                                        @foreach($records as $record)
                                            @php
                                                $recordType = $record['type'] ?? '';
                                                $typeColors = [
                                                    'A' => 'blue',
                                                    'AAAA' => 'purple',
                                                    'CNAME' => 'green',
                                                    'MX' => 'orange',
                                                    'TXT' => 'gray',
                                                    'NS' => 'cyan',
                                                    'SOA' => 'red',
                                                    'PTR' => 'indigo',
                                                ];
                                                $color = $typeColors[$recordType] ?? 'gray';
                                            @endphp
                                            @php
                                                $recordsCount = count($record['records'] ?? []);
                                            @endphp
                                            @foreach(($record['records'] ?? []) as $index => $rec)
                                                <tr class="hover:bg-gray-50">
                                                    @if($index === 0)
                                                        <td class="px-6 py-4 text-sm text-gray-900" rowspan="{{ $recordsCount }}">
                                                            {{ $record['name'] ?? 'N/A' }}
                                                        </td>
                                                        <td class="px-6 py-4 whitespace-nowrap" rowspan="{{ $recordsCount }}">
                                                            <span class="px-2 py-1 text-xs rounded-full bg-{{ $color }}-100 text-{{ $color }}-800 font-semibold">
                                                                {{ $recordType }}
                                                            </span>
                                                        </td>
                                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600" rowspan="{{ $recordsCount }}">
                                                            {{ $record['ttl'] ?? 'N/A' }}
                                                        </td>
                                                    @endif
                                                    <td class="px-6 py-4 text-sm text-gray-600">
                                                        <span class="{{ ($rec['disabled'] ?? false) ? 'text-gray-400 line-through' : '' }}">
                                                            {{ $rec['content'] ?? '' }}
                                                        </span>
                                                    </td>
                                                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                                        <div class="flex items-center justify-end space-x-2">
                                                            @if($recordType !== 'SOA')
                                                                {{-- Editare --}}
                                                                <a href="{{ route('powerdns.records.edit', [rtrim($zoneId, '.'), rtrim($record['name'] ?? '', '.'), $recordType]) }}?content={{ urlencode($rec['content'] ?? '') }}" 
                                                                   class="text-blue-600 hover:text-blue-900" 
                                                                   title="Editează">
                                                                    <i class="fas fa-edit"></i>
                                                                </a>

                                                                @if($index === 0)
                                                                    {{-- Changelog - doar pe primul rând --}}
                                                                    <a href="{{ route('powerdns.records.changelog', [rtrim($zoneId, '.'), rtrim($record['name'] ?? '', '.')]) }}" 
                                                                       class="text-purple-600 hover:text-purple-900" 
                                                                       title="Vezi istoric modificări">
                                                                        <i class="fas fa-history"></i>
                                                                    </a>
                                                                @endif

                                                                {{-- Toggle Status --}}
                                                                <form action="{{ route('powerdns.records.toggle-status', [rtrim($zoneId, '.'), rtrim($record['name'] ?? '', '.')]) }}" 
                                                                      method="POST" 
                                                                      class="inline">
                                                                    @csrf
                                                                    <input type="hidden" name="type" value="{{ $recordType }}">
                                                                    <input type="hidden" name="content" value="{{ $rec['content'] ?? '' }}">
                                                                    <input type="hidden" name="disabled" value="{{ ($rec['disabled'] ?? false) ? '0' : '1' }}">
                                                                    <button type="submit" 
                                                                            class="{{ ($rec['disabled'] ?? false) ? 'text-green-600 hover:text-green-900' : 'text-orange-600 hover:text-orange-900' }}" 
                                                                            title="{{ ($rec['disabled'] ?? false) ? 'Activează' : 'Dezactivează' }}">
                                                                        <i class="fas {{ ($rec['disabled'] ?? false) ? 'fa-play' : 'fa-pause' }}"></i>
                                                                    </button>
                                                                </form>

                                                                {{-- Ștergere --}}
                                                                @if($recordsCount > 1)
                                                                    <form action="{{ route('admin.powerdns.records.delete', $zoneId) }}"
                                                                          method="POST"
                                                                          class="inline"
                                                                          onsubmit="return confirm('Sigur doriți să ștergeți această înregistrare?');">
                                                                        @csrf
                                                                        @method('DELETE')
                                                                        <input type="hidden" name="name" value="{{ $record['name'] ?? '' }}">
                                                                        <input type="hidden" name="type" value="{{ $recordType }}">
                                                                        <input type="hidden" name="content" value="{{ $rec['content'] ?? '' }}">
                                                                        <button type="submit" 
                                                                                class="text-red-600 hover:text-red-900" 
                                                                                title="Șterge">
                                                                            <i class="fas fa-trash"></i>
                                                                        </button>
                                                                    </form>
                                                                @else
                                                                    <form action="{{ route('admin.powerdns.records.delete', $zoneId) }}"
                                                                          method="POST"
                                                                          class="inline"
                                                                          onsubmit="return confirm('Sigur doriți să ștergeți această înregistrare?');">
                                                                        @csrf
                                                                        @method('DELETE')
                                                                        <input type="hidden" name="name" value="{{ $record['name'] ?? '' }}">
                                                                        <input type="hidden" name="type" value="{{ $recordType }}">
                                                                        <button type="submit" 
                                                                                class="text-red-600 hover:text-red-900" 
                                                                                title="Șterge">
                                                                            <i class="fas fa-trash"></i>
                                                                        </button>
                                                                    </form>
                                                                @endif
                                                            @else
                                                                @if($index === 0)
                                                                    <span class="text-gray-400 text-xs">Protejat</span>
                                                                @endif
                                                            @endif
                                                        </div>
                                                    </td>
                                                </tr>
                                            @endforeach
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        @else
                            <div class="text-center py-12 bg-gray-50 rounded-lg">
                                <i class="fas fa-list text-gray-300 text-6xl mb-4"></i>
                                <p class="text-gray-600 text-lg">Nu există înregistrări DNS pentru această zonă</p>
                            </div>
                        @endif
                    </div>

                    <!-- Acțiuni zonă -->
                    <div class="mt-6 pt-6 border-t border-gray-200">
                        <form action="{{ route('admin.powerdns.zones.delete', $zoneId) }}"
                              method="POST"
                              onsubmit="return confirm('ATENȚIE: Ștergerea zonei va șterge și toate înregistrările DNS asociate. Sigur doriți să continuați?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit"
                                    class="bg-red-600 hover:bg-red-700 text-white px-6 py-2 rounded-lg transition">
                                <i class="fas fa-trash mr-2"></i>Șterge Zonă Complet
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-admin-layout>
