{* PowerDNS DNS server actions page.

   Unlike HestiaCP DNS nodes (which expose a full panel surface — users,
   packages, system probes, etc.), PowerDNS is an HTTP-API daemon. The
   only operational verbs that apply are:
     • Test the API endpoint (X-API-Key auth check)
     • Refresh the cached collectStatus snapshot (version + zone list)
     • Inspect the zone list it knows about

   URL: &page=server_actions&server_id=X  (type=dns, driver=powerdns) *}
<style>
    .puq-pdns-card { background:#fff; border:1px solid #ddd; border-radius:4px; padding:14px; margin-bottom:14px; }
    .puq-pdns-card h4 { margin:0 0 8px 0; font-size:14px; }
    .puq-pdns-meta dt { font-weight:600; color:#666; font-size:12px; }
    .puq-pdns-meta dd { font-family:monospace; margin-bottom:6px; }
    .puq-pdns-zones { max-height:480px; overflow:auto; border:1px solid #eee; padding:6px; }
    .puq-pdns-zones .label { margin-right:4px; }
</style>

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title pull-left">
            <i class="fa fa-server"></i> {$server.name|escape:'html'}
            <small class="text-muted">PowerDNS API @ {$server.hostname|escape:'html'}:{$server.port}</small>
        </h3>
        <div class="pull-right">
            <a href="{$meta.admin_module_url}&amp;page=dns_servers" class="btn btn-default btn-sm puq-nav-link">
                <i class="fa fa-arrow-left"></i> Back to DNS Servers
            </a>
        </div>
        <div class="clearfix"></div>
    </div>
    <div class="panel-body">
        <div class="alert alert-info">
            <strong><i class="fa fa-info-circle"></i> PowerDNS is an HTTP-API DNS daemon — the panel-user / packages / system-probe surface that HestiaCP nodes expose does not apply here.</strong>
            Use this page to test the API connection, refresh the cached zone count, and verify which zones PowerDNS is currently authoritative for.
        </div>

        <div class="row">
            <div class="col-md-6">
                <div class="puq-pdns-card">
                    <h4><i class="fa fa-plug"></i> Connection</h4>
                    <dl class="puq-pdns-meta">
                        <dt>Endpoint</dt>
                        <dd id="puq-pdns-endpoint">…</dd>
                        <dt>API key</dt>
                        <dd>{if $server.api_key_enc}<span class="label label-success">configured</span>{else}<span class="label label-danger">not set</span>{/if}</dd>
                        <dt>Status</dt>
                        <dd>
                            <span class="label label-default" id="puq-pdns-status-pill">{$server.status_state|default:'never'|upper}</span>
                            <span class="text-muted small" id="puq-pdns-status-ts">{if $server.status_last_collected_at}{$server.status_last_collected_at|escape:'html'}{else}never collected{/if}</span>
                        </dd>
                    </dl>
                    <button type="button" class="btn btn-info btn-sm" id="puq-pdns-test-btn"><i class="fa fa-plug"></i> Test connection</button>
                    <button type="button" class="btn btn-default btn-sm" id="puq-pdns-refresh-btn"><i class="fa fa-sync"></i> Refresh status</button>
                </div>
            </div>
            <div class="col-md-6">
                <div class="puq-pdns-card">
                    <h4><i class="fa fa-info"></i> Server info <small class="text-muted" id="puq-pdns-info-ts"></small></h4>
                    <dl class="puq-pdns-meta" id="puq-pdns-info">
                        <dt>Version</dt><dd id="puq-pdns-version">—</dd>
                        <dt>Server type</dt><dd id="puq-pdns-type">—</dd>
                        <dt>Zones (count)</dt><dd id="puq-pdns-zones-count">—</dd>
                    </dl>
                </div>
            </div>
        </div>

        <div class="puq-pdns-card">
            <h4><i class="fa fa-globe"></i> Zones authoritative on this server <small class="text-muted">(read-only — managed via service detail panels)</small></h4>
            <div id="puq-pdns-zones" class="puq-pdns-zones"><span class="text-muted">Loading…</span></div>
        </div>
    </div>
</div>

{literal}
<script>
(function(){
    var baseUrl = '{/literal}{$meta.admin_module_url}&ajax={literal}';
    var serverId = {/literal}{$server.id|intval}{literal};

    function renderInfoFromSummary(summary) {
        $('#puq-pdns-version').text(summary && summary.version ? summary.version : '—');
        $('#puq-pdns-type').text(summary && summary.type ? summary.type : '—');
        $('#puq-pdns-zones-count').text(summary && summary.zones_count != null ? summary.zones_count : '—');
    }
    function renderZones(zones) {
        var $h = $('#puq-pdns-zones').empty();
        if (!zones || !zones.length) {
            $h.html('<span class="text-muted">No zones reported.</span>');
            return;
        }
        zones.forEach(function(z){
            var name = (z.name || '').replace(/\.$/, '');
            $h.append('<span class="label label-default">' + esc(name) + '</span> ');
        });
        if (zones.length === 5) {
            $h.append(' <small class="text-muted">(sample — first 5 only; collectStatus caps the list)</small>');
        }
    }
    function loadFromCache() {
        $.ajax({
            url: baseUrl + 'getServer&id=' + serverId, dataType: 'json',
            success: function(r){
                if (r.status !== 'success' || !r.data) return;
                var srv = r.data;
                var scheme = 'http';
                try { var cfg = srv.config ? JSON.parse(srv.config) : {}; if (cfg.scheme === 'https') scheme = 'https'; } catch(e){}
                $('#puq-pdns-endpoint').text(scheme + '://' + srv.hostname + ':' + srv.port + '/api/v1');
                $('#puq-pdns-status-pill').removeClass().addClass('label label-' +
                    (srv.status_state === 'ok' ? 'success' : srv.status_state === 'error' ? 'danger' : 'default')).text((srv.status_state||'never').toUpperCase());
                $('#puq-pdns-status-ts').text(srv.status_last_collected_at || 'never collected');
                var sd = null;
                try { sd = srv.status_data ? JSON.parse(srv.status_data) : null; } catch(e){}
                if (sd) {
                    renderInfoFromSummary(sd.summary || {});
                    renderZones(sd.zones_sample || []);
                    $('#puq-pdns-info-ts').text(sd.collected_at ? '(' + sd.collected_at + ')' : '');
                }
            }
        });
    }
    $('#puq-pdns-test-btn').on('click', function(){
        var btn = this; btnSpinner(btn, 'Testing...');
        $.ajax({ url: baseUrl + 'testServerConnection', type: 'POST', data: { id: serverId }, dataType: 'json',
            success: function(r){
                if (r.status === 'success') showNotification('Connection OK', 'success');
                else showNotification('Connection FAILED: ' + (r.error || r.msg || 'unknown'), 'error');
            },
            error: function(){ showNotification('Request failed.', 'error'); },
            complete: function(){ btnRestore(btn, 'plug', 'Test connection'); }
        });
    });
    $('#puq-pdns-refresh-btn').on('click', function(){
        var btn = this; btnSpinner(btn, 'Refreshing...');
        $.ajax({ url: baseUrl + 'refreshServerStatus', type: 'POST', data: { id: serverId }, dataType: 'json',
            success: function(r){
                if (r.status === 'success') {
                    showNotification(r.msg || 'Refresh queued', 'success');
                    setTimeout(loadFromCache, 2500);
                } else {
                    showNotification(r.msg || 'Refresh failed', 'error');
                }
            },
            error: function(){ showNotification('Request failed.', 'error'); },
            complete: function(){ btnRestore(btn, 'sync', 'Refresh status'); }
        });
    });

    $(document).ready(loadFromCache);
})();
</script>
{/literal}
