{include file='modules/servers/puqWebHosting/templates/header.tpl'}
{assign var='page_icon'  value='check-circle'}
{assign var='page_title' value=$L.dz_dns_verify}
{include file='modules/servers/puqWebHosting/templates/_page_header.tpl'}

<div class="panel panel-default">
    <div class="panel-heading">
        <h4 style="margin:0"><i class="fa fa-globe"></i> {$L.dz_records_to_publish}</h4>
    </div>
    <div class="panel-body">
        <p>{$L.dz_canonical_set_pre} <strong><code>{$domain|escape}</code></strong>.
            {$L.dz_canonical_set_post}</p>

        <div id="puq-dns-mode" class="alert alert-info">{$L.dz_loading}</div>

        <h5><i class="fa fa-server"></i> {$L.dz_nameservers} <small class="text-muted">{$L.dz_ns_only_if_we_manage}</small></h5>
        <ul id="puq-expected-ns" style="margin-bottom:14px;"><li class="text-muted">{$L.dz_loading}</li></ul>

        <button type="button" class="btn btn-primary" id="puq-verify-dns-btn">
            <i class="fa fa-check-circle"></i> {$L.dz_verify_all_records}
        </button>
        <span id="puq-verify-summary" style="margin-left:10px;"></span>

        <table class="table table-condensed table-striped" style="margin-top:14px;">
            <thead>
                <tr>
                    <th>{$L.dz_type}</th>
                    <th>{$L.dz_name}</th>
                    <th>{$L.dz_value_expected}</th>
                    <th>{$L.dz_priority_ttl}</th>
                    <th>{$L.dz_live}</th>
                </tr>
            </thead>
            <tbody id="puq-required-list">
                <tr><td colspan="5" class="text-muted text-center"><i class="fa fa-spinner fa-spin"></i> {$L.dz_loading}</td></tr>
            </tbody>
        </table>
    </div>
</div>

{literal}
<script>
(function(){
    var sid = {/literal}{$service_id|default:0|intval}{literal};
    if (!sid) {
        var m = window.location.search.match(/[?&]id=(\d+)/);
        if (m) sid = parseInt(m[1], 10);
    }
    // Expose to the shared helpers (puqClientAjax, restore-lock poll) — this
    // splash sub-page isn't rendered with the {$service_id}-setting wrapper.
    window.puqServiceId = sid;
    var esc = window.puqEsc;
    function url(action){
        return 'clientarea.php?action=productdetails&id=' + sid + '&ajax=' + action;
    }

    function renderRecords(records, liveMap){
        if (!records || !records.length) {
            $('#puq-required-list').html('<tr><td colspan="5" class="text-muted">' + puqT('dz_no_records_configured') + '</td></tr>');
            return;
        }
        var html = '';
        records.forEach(function(r, idx){
            var key = r.record_type + '|' + (r.name || '@');
            var live = liveMap ? liveMap[key] : null;
            var liveCell = '<span class="text-muted small">' + puqT('dz_not_checked') + '</span>';
            if (live) {
                if (live.match === true) {
                    liveCell = '<span class="label label-success">' + puqT('dz_match') + '</span>';
                } else if (live.match === null) {
                    // Unverifiable type (DNSKEY / DS / TLSA). PHP can't query
                    // these directly — show neutral state with hint.
                    liveCell = '<span class="label label-default" title="' + esc(live.note || puqT('dz_verify_externally')) + '">' + puqT('dz_unverifiable') + '</span>';
                } else {
                    liveCell = '<span class="label label-danger">' + puqT('dz_missing') + '</span>';
                    if (live.actual && live.actual.length) {
                        liveCell += '<div class="small text-muted">' + puqT('dz_got') + ' <code>' + live.actual.map(esc).join('</code>, <code>') + '</code></div>';
                    }
                }
            }
            html += '<tr>' +
                '<td><strong>' + esc(r.record_type) + '</strong></td>' +
                '<td><code>' + esc(r.name || '@') + '</code></td>' +
                '<td><code style="white-space:pre-wrap; word-break:break-all;">' + esc(r.value) + '</code></td>' +
                '<td>' + (r.priority ? 'P:' + esc(r.priority) + ' ' : '') + 'TTL:' + esc(r.ttl || 3600) + '</td>' +
                '<td>' + liveCell + '</td>' +
                '</tr>';
        });
        $('#puq-required-list').html(html);
    }

    function loadRequired(){
        $.ajax({ url: url('requiredDnsRecords'), dataType: 'json', success: function(r){
            if (r.status !== 'success') {
                $('#puq-dns-mode').removeClass('alert-info alert-warning alert-success')
                    .addClass('alert-danger').text(r.msg || puqT('dz_failed_load_required'));
                return;
            }
            var d = r.data;
            var pendingNote = (d.source === 'template')
                ? ' <em>' + puqT('dz_showing_planned_set') + '</em>'
                : '';
            $('#puq-dns-mode').removeClass('alert-info alert-warning alert-success').html(
                d.external_dns ?
                    '<i class="fa fa-info-circle"></i> <strong>' + puqT('dz_external_dns_mode') + '</strong> ' + puqT('dz_external_dns_desc') + pendingNote :
                    '<i class="fa fa-info-circle"></i> <strong>' + puqT('dz_our_dns') + '</strong> ' + puqT('dz_our_dns_desc') + pendingNote
            ).addClass(d.external_dns ? 'alert-warning' : 'alert-info');

            var $ns = $('#puq-expected-ns').empty();
            if (d.expected_ns && d.expected_ns.length) {
                d.expected_ns.forEach(function(n){ $ns.append('<li><code>' + esc(n) + '</code></li>'); });
            } else {
                $ns.append('<li class="text-muted">' + puqT('dz_not_configured_by_admin') + '</li>');
            }

            renderRecords(d.records, null);
        }});
    }

    $('#puq-verify-dns-btn').on('click', function(){
        var $btn = $(this); puqBtnSpin($btn, puqT('dz_querying_dns'));
        // Route through puqClientAjax — form-urlencoded POSTs to clientarea.php
        // get 302'd by WHMCS's anti-forgery layer.
        window.puqClientAjax('verifyDns', {}, function(r){
            try {
                if (!r || r.status !== 'success') {
                    if (window.puqToast) puqToast('error', (r && r.msg) || puqT('dz_verify_failed'));
                    return;
                }
                var d = r.data;
                var nsHtml = '';
                if (d.ns.match === true) nsHtml = '<span class="puq-badge puq-badge-success">' + puqT('dz_ns_match') + '</span>';
                else if (d.ns.match === false) nsHtml = '<span class="puq-badge puq-badge-danger">' + puqT('dz_ns_mismatch') + '</span>';
                else nsHtml = '<span class="puq-badge puq-badge-muted">' + puqT('dz_ns_not_configured') + '</span>';
                var ok = 0, bad = 0, skipped = 0;
                var liveMap = {};
                (d.records || []).forEach(function(c){
                    var key = c.type + '|' + (c.name || '@');
                    liveMap[key] = c;
                    if (c.match === true)       ok++;
                    else if (c.match === null)  skipped++;   // unverifiable (DNSKEY/DS/TLSA)
                    else                        bad++;
                });
                $('#puq-verify-summary').html(nsHtml + ' &nbsp; ' +
                    '<span class="puq-badge puq-badge-success">' + ok + ' ' + puqT('dz_ok') + '</span> ' +
                    (bad ? '<span class="puq-badge puq-badge-danger">' + bad + ' ' + puqT('dz_missing') + '</span> ' : '') +
                    (skipped ? '<span class="puq-badge puq-badge-muted">' + skipped + ' ' + puqT('dz_unverifiable') + '</span>' : '')
                );
                $.ajax({ url: url('requiredDnsRecords'), dataType: 'json', success: function(rr){
                    renderRecords(rr.data && rr.data.records, liveMap);
                }});
            } finally {
                puqBtnRestore($btn);
            }
        }, 'POST');
    });

    loadRequired();
})();
</script>
{/literal}
