<?php
// Network & SSL tab — HAProxy Configuration Management, HAProxy API Key, and
// System Service SSL.
// Variables ($current_config) are prepared in the parent server-settings.php
// before the include.
?>

<div class="row g-3">
    <!-- HAProxy Configuration Management -->
    <div class="col-lg-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">HAProxy Configuration Management</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">Manage HAProxy configuration and monitor health status.</p>

                <div class="d-grid gap-2">
                    <form method="post" class="d-inline">
                        <input type="hidden" name="action" value="haproxy_regenerate_config">
                        <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fa fa-cog"></i> Regenerate Configuration
                        </button>
                    </form>

                    <form method="post" class="d-inline">
                        <input type="hidden" name="action" value="haproxy_reload_config">
                        <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">
                        <button type="submit" class="btn btn-warning w-100">
                            <i class="fa fa-refresh"></i> Reload Configuration
                        </button>
                    </form>

                    <form method="post" class="d-inline">
                        <input type="hidden" name="action" value="haproxy_health_check">
                        <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">
                        <button type="submit" class="btn btn-info w-100">
                            <i class="fa fa-heart"></i> Health Check
                        </button>
                    </form>
                </div>

                <hr>

                <div class="small text-muted">
                    <strong>Functions:</strong><br>
                    <strong>Regenerate:</strong> Rebuild HAProxy configuration for all active sites<br>
                    <strong>Reload:</strong> Reload HAProxy configuration without restarting<br>
                    <strong>Health Check:</strong> Check HAProxy service health status
                </div>
            </div>
        </div>
    </div>

    <!-- HAProxy API Key -->
    <div class="col-lg-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">HAProxy API Key</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">Bearer token used to authenticate against the HAProxy Manager API.</p>

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

                    <div class="mb-3">
                        <label for="haproxy_api_key" class="form-label">API Key</label>
                        <input type="text" class="form-control" id="haproxy_api_key" name="haproxy_api_key"
                               value="<?php echo htmlspecialchars($current_config['haproxy_api_key'] ?? ''); ?>"
                               placeholder="your-secure-api-key-here" minlength="16" required>
                        <div class="form-text text-muted">Minimum 16 characters. Restart the HAProxy container after updating to apply.</div>
                    </div>

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

    <!-- System Service SSL Management -->
    <div class="col-lg-6">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">System Service SSL Certificates</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">Enable SSL certificates for system services like HTTP and FTP.</p>

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

                    <div class="mb-3">
                        <label for="service_name" class="form-label">Service</label>
                        <select class="form-control" id="service_name" name="service_name" required>
                            <option value="">Select Service</option>
                            <option value="httpd">Apache HTTP Server</option>
                            <option value="proftpd">ProFTPD Server</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label for="domain_name" class="form-label">Domain Name</label>
                        <input type="text" class="form-control" id="domain_name" name="domain_name"
                               placeholder="example.com" required>
                        <div class="form-text text-muted">Domain name for the SSL certificate</div>
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="fa fa-lock"></i> Enable SSL
                    </button>
                </form>

                <hr>

                <?php
                // Display current system service SSL certificates
                try {
                    require_once('/docker/whp/web/libs/ssl_manager.php');
                    $ssl_manager = new ssl_manager();
                    $system_certs = $ssl_manager->getSystemServiceSSLCertificates();

                    if (!empty($system_certs)) {
                        echo '<div class="small">';
                        echo '<strong>Current SSL Certificates:</strong><br>';
                        foreach ($system_certs as $cert) {
                            $status_class = $cert['ssl_enabled'] ? 'text-success' : 'text-muted';
                            $status_text = $cert['ssl_enabled'] ? 'Enabled' : 'Disabled';
                            echo '<div class="' . $status_class . '">';
                            echo '<i class="fa fa-' . ($cert['ssl_enabled'] ? 'lock' : 'unlock') . '"></i> ';
                            echo htmlspecialchars($cert['service_name']) . ' - ' . htmlspecialchars($cert['domain_name']) . ' (' . $status_text . ')';
                            echo '</div>';
                        }
                        echo '</div>';
                    } else {
                        echo '<div class="small text-muted">No system service SSL certificates configured</div>';
                    }
                } catch (Exception $e) {
                    echo '<div class="small text-danger">Error loading SSL certificates: ' . htmlspecialchars($e->getMessage()) . '</div>';
                }
                ?>
            </div>
        </div>
    </div>
</div>
