<?php
// DNS tab — WHP Nameserver Settings, Network DNS Settings, and DNS Configuration
// (default TTL, server name/description). Mail server configuration moved to
// the Mail tab; the update_dns_settings handler now only writes DNS-scope
// fields.
// Variables ($current_config, $server_ip, $current_nameservers, $default_ttl,
// $server_name, $server_description) are prepared in the parent
// server-settings.php before the include.
?>

<div class="row g-3">
    <!-- WHP Nameserver Settings -->
    <div class="col-lg-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">WHP Nameserver Configuration</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">Configure the nameserver hostnames and IP addresses that WHP will use for customer domain configurations.</p>

                <form method="post">
                    <input type="hidden" name="action" value="update_whp_nameservers">
                    <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="whp_ns1" class="form-label">Primary Nameserver Hostname</label>
                                <input type="text" class="form-control" id="whp_ns1" name="whp_ns1"
                                       value="<?php echo htmlspecialchars($current_config['whp_nameservers']['ns1'] ?? 'ns1.example.com'); ?>"
                                       placeholder="ns1.yourdomain.com" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="whp_ns1_ip" class="form-label">Primary Nameserver IP</label>
                                <input type="text" class="form-control" id="whp_ns1_ip" name="whp_ns1_ip"
                                       value="<?php echo htmlspecialchars($current_config['whp_nameservers']['ns1_ip'] ?? $server_ip); ?>"
                                       placeholder="<?php echo $server_ip; ?>" required>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="whp_ns2" class="form-label">Secondary Nameserver Hostname</label>
                                <input type="text" class="form-control" id="whp_ns2" name="whp_ns2"
                                       value="<?php echo htmlspecialchars($current_config['whp_nameservers']['ns2'] ?? 'ns2.example.com'); ?>"
                                       placeholder="ns2.yourdomain.com" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="whp_ns2_ip" class="form-label">Secondary Nameserver IP</label>
                                <input type="text" class="form-control" id="whp_ns2_ip" name="whp_ns2_ip"
                                       value="<?php echo htmlspecialchars($current_config['whp_nameservers']['ns2_ip'] ?? $server_ip); ?>"
                                       placeholder="<?php echo $server_ip; ?>" required>
                            </div>
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="fa fa-save"></i> Update WHP Nameservers
                    </button>
                </form>

                <hr>

                <div class="small text-muted">
                    <strong>Note:</strong> These hostnames and IP addresses will be used when configuring DNS zones for customer domains. Make sure these hostnames point to the specified IP addresses.
                </div>
            </div>
        </div>
    </div>

    <!-- Network DNS Settings -->
    <div class="col-lg-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">Network DNS Settings</h5>
            </div>
            <div class="card-body">
                <form method="post">
                    <input type="hidden" name="action" value="update_nameservers">
                    <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">

                    <div class="mb-3">
                        <label for="nameserver1" class="form-label">Primary Nameserver</label>
                        <input type="text" class="form-control" id="nameserver1" name="nameserver1"
                               value="<?php echo htmlspecialchars($current_nameservers[0] ?? '8.8.8.8'); ?>"
                               placeholder="8.8.8.8" required>
                    </div>

                    <div class="mb-3">
                        <label for="nameserver2" class="form-label">Secondary Nameserver</label>
                        <input type="text" class="form-control" id="nameserver2" name="nameserver2"
                               value="<?php echo htmlspecialchars($current_nameservers[1] ?? '8.8.4.4'); ?>"
                               placeholder="8.8.4.4">
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="fa fa-save"></i> Update Nameservers
                    </button>
                </form>

                <hr>

                <div class="small text-muted">
                    <strong>Common DNS Providers:</strong><br>
                    <strong>Google:</strong> 8.8.8.8, 8.8.4.4<br>
                    <strong>Cloudflare:</strong> 1.1.1.1, 1.0.0.1<br>
                    <strong>Quad9:</strong> 9.9.9.9, 149.112.112.112<br>
                    <strong>OpenDNS:</strong> 208.67.222.222, 208.67.220.220
                </div>
            </div>
        </div>
    </div>
</div>

<!-- DNS Configuration Settings (also contains mail server hostname, mail API
     config, and server identity fields — single update_dns_settings form) -->
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">DNS Configuration Settings</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">Configure DNS settings that affect customer domains and email services.</p>

                <form method="post">
                    <input type="hidden" name="action" value="update_dns_settings">
                    <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">

                    <div class="mb-3">
                        <label for="default_ttl" class="form-label">Default TTL (seconds)</label>
                        <input type="number" class="form-control" id="default_ttl" name="default_ttl"
                               value="<?php echo $default_ttl; ?>" min="60" max="86400" required>
                        <div class="form-text text-muted">Default TTL for new DNS records (60-86400 seconds)</div>
                    </div>

                    <hr>

                    <h6 class="mb-3">Server Information</h6>

                    <div class="mb-3">
                        <label for="server_name" class="form-label">Server Name</label>
                        <input type="text" class="form-control" id="server_name" name="server_name"
                               value="<?php echo htmlspecialchars($server_name); ?>"
                               placeholder="WHP Server" required>
                        <div class="form-text text-muted">Display name for this server</div>
                    </div>

                    <div class="mb-3">
                        <label for="server_description" class="form-label">Server Description</label>
                        <textarea class="form-control" id="server_description" name="server_description" rows="3"><?php echo htmlspecialchars($server_description); ?></textarea>
                        <div class="form-text text-muted">Description of this server</div>
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="fa fa-save"></i> Update DNS Settings
                    </button>
                </form>

                <hr>

                <div class="small text-muted">
                    <strong>Note:</strong> These settings affect all new domains. Mail server hostname is used for MX records, and TTL is the default time-to-live for DNS records.
                </div>
            </div>
        </div>
    </div>
</div>
