{* DNS servers tab. Attaches independent DNS servers to this group (many-to-many).
   Provisioning replicates zones/records onto every attached DNS server (active-active).
   Save button writes the attachment list — independent of the main "Save group" button. *}

{if $is_new}
    <div class="alert alert-info">
        <i class="fa fa-info-circle"></i> Save the group first (General tab), then you can attach DNS servers.
    </div>
{else}
{assign var=puq_is_vanity value=(($group.group_type|default:'standard') eq 'vanity')}
<div class="puq-dns-tab">

    {if $puq_is_vanity}
    <div class="alert alert-info" style="font-size:12px;">
        <i class="fa fa-info-circle"></i> Vanity group — only the DNS server selection applies here.
        Zones belong to your provider domains (configured on the <em>Vanity domains</em> tab);
        the subdomain records are pushed to the servers ticked below.
    </div>
    {/if}

    {* ============ Default zone template (standard only) ============ *}
    {if !$puq_is_vanity}
    <div class="panel panel-default">
        <div class="panel-heading">
            <strong><i class="fa fa-file-alt"></i> Default DNS zone template</strong>
            <small class="text-muted">— used for new services in this group unless the web server has its own override</small>
        </div>
        <div class="panel-body">
            <p class="text-muted small">
                Resolution chain: <code>web server.dns_template_id</code> →
                <code>group.dns_template_id</code> (this) →
                <code>global is_default</code>. Leave empty to fall through to the global default.
            </p>
            <div class="row">
                <div class="col-md-9">
                    <select id="puq-dns-tmpl-select" class="form-control input-sm">
                        <option value="">— inherit (use global default) —</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <button type="button" class="btn btn-primary btn-sm btn-block" id="puq-dns-tmpl-save"><i class="fa fa-save"></i> Save default template</button>
                </div>
            </div>
        </div>
    </div>

    {* ============ SOA defaults ============ *}
    <div class="panel panel-default">
        <div class="panel-heading">
            <strong><i class="fa fa-cog"></i> SOA defaults for new zones</strong>
            <small class="text-muted">— substituted into <code>puqWebHosting_dns_zones</code> on first deploy</small>
        </div>
        <div class="panel-body">
            <p class="text-muted small">
                These values run through the same placeholder substitution as zone template records.
                Available variables:
                {literal}
                <code>{domain}</code> <code>{server_ip}</code> <code>{mail_server_ip}</code>
                <code>{mail_server_hostname}</code> <code>{ns1}</code>..<code>{ns8}</code>
                <code>{client_id}</code> <code>{service_id}</code> <code>{year}</code>
                <code>{unixtime}</code>
                {/literal}
                + any custom key you define in the <em>Variables</em> tab.
            </p>
            <div class="row">
                <div class="col-md-6 form-group">
                    <label>SOA email <small class="text-muted">(use <code>.</code> instead of <code>@</code>)</small></label>
                    <input type="text" id="puq-soa-email" class="form-control input-sm" placeholder="hostmaster.{literal}{domain}{/literal}">
                </div>
                <div class="col-md-2 form-group">
                    <label>TTL (s)</label>
                    <input type="number" id="puq-soa-ttl" class="form-control input-sm" placeholder="3600" min="60" max="86400">
                </div>
                <div class="col-md-2 form-group">
                    <label>Refresh (s)</label>
                    <input type="number" id="puq-soa-refresh" class="form-control input-sm" placeholder="7200" min="60">
                </div>
                <div class="col-md-2 form-group">
                    <label>Retry (s)</label>
                    <input type="number" id="puq-soa-retry" class="form-control input-sm" placeholder="3600" min="60">
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 form-group">
                    <button type="button" class="btn btn-primary btn-sm" id="puq-soa-save"><i class="fa fa-save"></i> Save SOA defaults</button>
                </div>
                <div class="col-md-3 form-group">
                    <label>Expire (s)</label>
                    <input type="number" id="puq-soa-expire" class="form-control input-sm" placeholder="1209600" min="3600">
                </div>
                <div class="col-md-3 form-group">
                    <label>Minimum (s)</label>
                    <input type="number" id="puq-soa-minimum" class="form-control input-sm" placeholder="3600" min="60">
                </div>
            </div>
        </div>
    </div>
    {/if}{* /standard-only zone template + SOA *}

    {* ============ DNS servers attachment ============ *}
    <p class="text-muted">
        Attach DNS servers to this group. DNS servers are independent — one server can serve many groups.
        {if $puq_is_vanity}Subdomain records for this group's vanity domains are pushed to <strong>every</strong> attached server.{else}Zones and records of services in this group will be replicated to <strong>every</strong> attached
        DNS server (active-active cluster).{/if}
    </p>

    <div class="row" style="margin-bottom:10px;">
        <div class="col-md-9">
            <strong>Available DNS servers</strong>
            <span class="text-muted small" id="puq-dns-stats"></span>
        </div>
        <div class="col-md-3 text-right">
            <button type="button" class="btn btn-primary btn-sm" id="puq-dns-save">
                <i class="fa fa-save"></i> Save DNS selection
            </button>
        </div>
    </div>

    {* Unified-readiness banner — populated by refreshUnifiedReadiness() after
       loadDnsServers() completes. Tells the admin whether any server in this
       group can host a unified-deployment service (i.e. has BOTH Web and Mail
       caps ticked). Empty/idle until JS fills it. *}
    <div id="puq-unified-banner" class="alert" style="padding:6px 10px;font-size:12px;margin-bottom:10px;"></div>

    <table class="table table-condensed table-hover" id="puq-dns-table" style="background:#fff;">
        <thead>
            <tr>
                <th style="width:30px;"></th>
                <th>Name</th>
                <th>Driver</th>
                <th>Caps</th>
                <th>Hostname</th>
                <th>Status</th>
                <th>Capacity</th>
            </tr>
        </thead>
        <tbody>
            <tr><td colspan="7" class="text-muted text-center"><i class="fa fa-spinner fa-spin"></i> Loading DNS servers…</td></tr>
        </tbody>
    </table>

    <p class="text-muted small">
        Don't see your DNS server? Add it on the
        <a href="{$meta.admin_module_url}&page=dns_servers" target="_blank">DNS Servers</a> page first.
    </p>
</div>
{/if}
