<?php
// Services tab — Service Status, Docker Container Management, Shared Apache Pool,
// and Backup Settings.
// Variables ($service_status, $container_status, $shared_httpd_instances,
// $shared_httpd_settings, $current_config, $backup_storage_config) are prepared
// in the parent server-settings.php before the include.
?>

<!-- Service Status -->
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">Service Status</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <?php
                    $service_display_names = [
                        'httpd' => 'Apache',
                        'php-fpm' => 'PHP-FPM',
                        'docker' => 'Docker',
                        'proftpd' => 'ProFTPD',
                        'whp-backup-upload' => 'Backup Upload',
                    ];
                    ?>
                    <?php foreach ($service_status as $service => $status): ?>
                    <div class="col-md-3 mb-3" data-service="<?php echo $service; ?>">
                        <div class="text-center">
                            <h6><?php echo $service_display_names[$service] ?? ucfirst($service); ?></h6>
                            <span class="badge <?php echo $status === 'active' ? 'bg-success' : 'bg-danger'; ?> fs-6">
                                <?php echo ucfirst($status ?: 'inactive'); ?>
                            </span>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>

                <hr>

                <form id="serviceRestartForm">
                    <input type="hidden" name="action" value="restart_services">
                    <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">
                    <div class="mb-3">
                        <label class="form-label text-center d-block">Restart Services</label>
                        <div class="row justify-content-center">
                            <div class="col-auto">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="services[]" value="httpd" id="service-httpd">
                                    <label class="form-check-label" for="service-httpd">Apache (httpd)</label>
                                </div>
                            </div>
                            <div class="col-auto">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="services[]" value="php-fpm" id="service-php">
                                    <label class="form-check-label" for="service-php">PHP-FPM</label>
                                </div>
                            </div>
                            <div class="col-auto">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="services[]" value="docker" id="service-docker">
                                    <label class="form-check-label" for="service-docker">Docker</label>
                                </div>
                            </div>
                            <div class="col-auto">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="services[]" value="network" id="service-network">
                                    <label class="form-check-label" for="service-network">Network</label>
                                </div>
                            </div>
                            <div class="col-auto">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="services[]" value="proftpd" id="service-proftpd">
                                    <label class="form-check-label" for="service-proftpd">FTP (ProFTPD)</label>
                                </div>
                            </div>
                            <div class="col-auto">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="services[]" value="whp-backup-upload" id="service-whp-backup-upload">
                                    <label class="form-check-label" for="service-whp-backup-upload">Backup Upload</label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="text-center">
                        <button type="submit" class="btn btn-warning" id="restartServicesBtn">
                            <i class="fa fa-refresh"></i> Restart Selected Services
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Docker Container Management -->
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">Docker Container Management</h5>
            </div>
            <div class="card-body">
                <div class="row mb-3">
                    <?php foreach ($container_status as $container => $status): ?>
                    <div class="col-md-4 mb-3" data-container="<?php echo $container; ?>">
                        <div class="text-center">
                            <h6><?php echo ucfirst(str_replace('-', ' ', $container)); ?></h6>
                            <?php
                            $status_class = 'bg-success';
                            $status_text = 'Running';
                            if ($status === 'stopped') {
                                $status_class = 'bg-warning';
                                $status_text = 'Stopped';
                            } elseif ($status === 'not_found') {
                                $status_class = 'bg-danger';
                                $status_text = 'Not Found';
                            }
                            ?>
                            <span class="badge <?php echo $status_class; ?> fs-6">
                                <?php echo $status_text; ?>
                            </span>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>

                <hr>

                <form id="dockerContainerForm">
                    <input type="hidden" name="action" value="manage_docker_container">
                    <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="container" class="form-label">Container</label>
                                <select class="form-control" id="container" name="container" required>
                                    <option value="">Select Container</option>
                                    <option value="mysql">MariaDB (mysql)</option>
                                    <option value="postgresql">PostgreSQL</option>
                                    <option value="haproxy-manager">HAProxy Manager</option>
                                    <option value="memcache">Memcache</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="operation" class="form-label">Operation</label>
                                <select class="form-control" id="operation" name="operation" required>
                                    <option value="">Select Operation</option>
                                    <option value="start">Start</option>
                                    <option value="stop">Stop</option>
                                    <option value="restart">Restart</option>
                                    <option value="pull_recreate">Pull Latest & Recreate</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">&nbsp;</label>
                                <div>
                                    <button type="submit" class="btn btn-primary" id="executeDockerBtn">
                                        <i class="fa fa-cogs"></i> Execute Operation
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>

                <div class="alert alert-info">
                    <i class="fa fa-info-circle"></i>
                    <strong>Note:</strong>
                    <ul class="mb-0 mt-2">
                        <li><strong>Start/Stop/Restart:</strong> Controls the container state</li>
                        <li><strong>Pull Latest & Recreate:</strong> Downloads the latest image and recreates the container with current settings</li>
                        <li>All containers are configured to restart automatically unless stopped</li>
                        <li>MariaDB data is preserved in volumes during container operations</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Shared Apache Pool Management -->
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">Shared Apache Pool</h5>
            </div>
            <div class="card-body">
                <!-- Instance Status -->
                <h6 class="mb-3">Running Instances</h6>
                <div class="row mb-3">
                    <?php if (empty($shared_httpd_instances)): ?>
                    <div class="col-12">
                        <span class="text-muted">No shared httpd instances running. Use Scale to start instances.</span>
                    </div>
                    <?php else: ?>
                    <?php foreach ($shared_httpd_instances as $name => $status): ?>
                    <div class="col-md-3 mb-2">
                        <div class="text-center">
                            <h6><?php echo htmlspecialchars($name); ?></h6>
                            <?php
                            $status_class = $status === 'running' ? 'bg-success' : 'bg-warning';
                            $status_text = $status === 'running' ? 'Running' : 'Stopped';
                            ?>
                            <span class="badge <?php echo $status_class; ?> fs-6"><?php echo $status_text; ?></span>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </div>

                <hr>

                <!-- Pool Operations -->
                <h6 class="mb-3">Pool Operations</h6>
                <form id="sharedHttpdForm">
                    <input type="hidden" name="action" value="manage_shared_httpd">
                    <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">
                    <div class="row align-items-end">
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label for="shared_httpd_scale_count" class="form-label">Instance Count</label>
                                <input type="number" class="form-control" id="shared_httpd_scale_count" name="instance_count"
                                       value="<?php echo count($shared_httpd_instances) ?: 1; ?>"
                                       min="0" max="10">
                            </div>
                        </div>
                        <div class="col-md-9">
                            <div class="mb-3 d-flex gap-2 flex-wrap">
                                <button type="submit" name="operation" value="scale" class="btn btn-primary shared-httpd-btn">
                                    <i class="fa fa-arrows-v"></i> Scale
                                </button>
                                <button type="submit" name="operation" value="restart_all" class="btn btn-warning shared-httpd-btn">
                                    <i class="fa fa-refresh"></i> Restart All
                                </button>
                                <button type="submit" name="operation" value="pull_recreate_all" class="btn btn-info shared-httpd-btn">
                                    <i class="fa fa-download"></i> Pull & Recreate All
                                </button>
                                <button type="submit" name="operation" value="stop_all" class="btn btn-danger shared-httpd-btn">
                                    <i class="fa fa-stop"></i> Stop All
                                </button>
                                <button type="submit" name="operation" value="start_all" class="btn btn-success shared-httpd-btn">
                                    <i class="fa fa-play"></i> Start All
                                </button>
                            </div>
                        </div>
                    </div>
                </form>

                <hr>

                <!-- Default Settings -->
                <h6 class="mb-3">Default Settings</h6>
                <form id="sharedHttpdSettingsForm">
                    <input type="hidden" name="action" value="update_shared_httpd_settings">
                    <input type="hidden" name="csrf_token" value="<?php echo get_csrf_token(); ?>">
                    <div class="row align-items-end">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="shared_httpd_default_mode" class="form-label">Default Mode for New PHP Sites</label>
                                <select class="form-control" id="shared_httpd_default_mode" name="default_mode">
                                    <option value="standalone" <?php echo ($shared_httpd_settings['default_mode'] === 'standalone') ? 'selected' : ''; ?>>Standalone (Apache+PHP per container)</option>
                                    <option value="shared_httpd" <?php echo ($shared_httpd_settings['default_mode'] === 'shared_httpd') ? 'selected' : ''; ?>>Shared Apache (PHP-FPM)</option>
                                </select>
                                <div class="form-text text-muted">Sets the default container mode when creating new PHP sites</div>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="mb-3">
                                <button type="submit" class="btn btn-primary" id="saveSharedHttpdSettingsBtn">
                                    <i class="fa fa-save"></i> Save
                                </button>
                            </div>
                        </div>
                    </div>
                </form>

                <div class="alert alert-info mt-2">
                    <i class="fa fa-info-circle"></i>
                    <strong>Note:</strong>
                    <ul class="mb-0 mt-2">
                        <li><strong>Scale:</strong> Adjusts the number of shared httpd instances (creates or removes as needed)</li>
                        <li><strong>Pull & Recreate All:</strong> Pulls the latest shared-httpd image and recreates all instances</li>
                        <li><strong>Default Mode:</strong> Controls whether new PHP sites default to shared Apache or standalone containers</li>
                        <li>All shared httpd instances share the same vhost configuration volume</li>
                        <li>HAProxy load-balances across all running instances automatically</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Backup Settings -->
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">Backup Settings</h5>
            </div>
            <div class="card-body">
                <p class="text-muted small">Configure backup system performance settings to prevent server overload.</p>

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

                    <div class="mb-3">
                        <label for="max_concurrent_backups" class="form-label">Maximum Concurrent Backups</label>
                        <input type="number" class="form-control" id="max_concurrent_backups" name="max_concurrent_backups"
                               value="<?php echo intval($current_config['backup_max_concurrent'] ?? 2); ?>"
                               min="1" max="10" required>
                        <div class="form-text text-muted">Number of backup jobs that can run simultaneously (1-10). Default: 2</div>
                    </div>

                    <div class="mb-3">
                        <label for="backup_upload_space_gb" class="form-label">Backup Local Storage Limit (GB)</label>
                        <input type="number" class="form-control" id="backup_upload_space_gb" name="backup_upload_space_gb"
                               value="<?php echo intval($backup_storage_config['max_size_gb']); ?>"
                               min="10" max="10000" required>
                        <div class="form-text text-muted">Maximum disk space for local backup staging (10-10,000 GB). Default: 100</div>
                        <?php
                            $current_bytes = intval($backup_storage_config['current_size_bytes']);
                            $max_bytes = intval($backup_storage_config['max_size_gb']) * 1073741824;
                            $usage_pct = $max_bytes > 0 ? round(($current_bytes / $max_bytes) * 100, 1) : 0;
                            $current_gb = round($current_bytes / 1073741824, 2);
                        ?>
                        <div class="form-text text-muted">
                            Current usage: <?php echo $current_gb; ?> GB / <?php echo intval($backup_storage_config['max_size_gb']); ?> GB (<?php echo $usage_pct; ?>%)
                        </div>
                    </div>

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

                <hr>

                <div class="small text-muted">
                    <strong>Note:</strong> Lowering the concurrent backups number reduces server load but increases backup queue time.
                    The local storage limit controls how much disk space is reserved for backup files awaiting upload to remote storage.
                </div>
            </div>
        </div>
    </div>
</div>
