<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h5 class="mb-1">{{ __('store.clientarea_domains_management_child_nameservers_title') }}</h5>
        <p class="text-muted mb-0">{{ __('store.clientarea_domains_management_child_nameservers_description') }}</p>
    </div>
    <button type="button" id="childNsAddBtn" class="btn btn-primary">
        <i class="fas fa-plus me-2"></i>{{ __('store.clientarea_domains_management_child_nameservers_add_nameserver') }}
    </button>
</div>

@if($data)
    @php
        $nameservers = $data['nameservers'] ?? [];
    @endphp
    <div class="table-responsive">
        <table id="childNsTable" class="table table-hover align-middle mb-0">
            <thead>
                <tr>
                    <th>{{ __('store.domain_manage.nameserver') }}</th>
                    <th>{{ __('store.domain_manage.ip_addresses') }}</th>
                    <th class="text-end">{{ __('store.domain_manage.actions') }}</th>
                </tr>
            </thead>
            <tbody>
                @forelse($nameservers as $ns)
                    @php
                        $hostname = $ns['hostname'] ?? $ns['nameserver'] ?? '';
                        $ipRaw = $ns['ip'] ?? $ns['ip_addresses'] ?? '';
                        $ip = is_array($ipRaw) ? implode(', ', $ipRaw) : $ipRaw;
                        $nsId = $ns['id'] ?? $hostname;
                    @endphp
                    <tr class="ns-row" data-ns-id="{{ $nsId }}" data-original='@json(['hostname' => $hostname, 'ip' => $ip])'>
                        <td class="ns-cell-hostname">
                            <span class="ns-view">{{ $hostname }}</span>
                            <input type="text" class="form-control form-control-sm ns-input-hostname d-none" value="{{ $hostname }}" placeholder="ns1.example.com">
                        </td>
                        <td class="ns-cell-ip">
                            <span class="ns-view">{{ $ip ?: '-' }}</span>
                            <input type="text" class="form-control form-control-sm ns-input-ip d-none" value="{{ $ip }}" placeholder="192.0.2.1">
                        </td>
                        <td class="text-end ns-actions">
                            <button type="button" class="btn btn-sm btn-outline-primary ns-edit-btn" title="{{ __('store.common_edit') }}">
                                <i class="fas fa-edit"></i>
                            </button>
                            <button type="button" class="btn btn-sm btn-outline-danger ns-delete-btn" title="{{ __('store.common_delete') }}">
                                <i class="fas fa-trash"></i>
                            </button>
                            <button type="button" class="btn btn-sm btn-success ns-save-btn d-none" title="{{ __('store.common_save') }}">
                                <i class="fas fa-check"></i>
                            </button>
                            <button type="button" class="btn btn-sm btn-outline-secondary ns-cancel-btn d-none" title="{{ __('store.common_cancel') }}">
                                <i class="fas fa-times"></i>
                            </button>
                        </td>
                    </tr>
                @empty
                    <tr class="ns-empty">
                        <td colspan="3" class="text-center text-muted">{{ __('store.domain_manage.no_child_nameservers') }}</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
@else
    <div class="alert alert-info">
        <i class="fas fa-info-circle me-2"></i>{{ $error ?? 'Child nameserver bilgileri yüklenemedi.' }}
    </div>
@endif

@push('scripts')
<script>
window.childNsConfig = {
    csrfToken: '{{ csrf_token() }}',
    routes: {
        add: '{{ route('store.auth.domains.manage.child-nameservers.add', $domain->id) }}',
        update: '{{ route('store.auth.domains.manage.child-nameservers.update', $domain->id) }}',
        delete: '{{ route('store.auth.domains.manage.child-nameservers.delete', $domain->id) }}',
    },
    translations: {
        edit: @json(__('store.common_edit')),
        delete: @json(__('store.common_delete')),
        save: @json(__('store.common_save')),
        cancel: @json(__('store.common_cancel')),
        confirmDelete: @json(__('store.clientarea_domains_management_child_nameservers_confirm_delete')),
        fillAllFields: @json(__('store.clientarea_domains_management_child_nameservers_fill_all_fields')),
        empty: @json(__('store.domain_manage.no_child_nameservers')),
    }
};
</script>
@vite('resources/js/store/child-nameservers.js')
@endpush
