<?php
/**
 * EasyStream Admin - System Settings Management
 * Comprehensive settings interface for all system configurations
 */

declare(strict_types=1);

require_once __DIR__ . '/admin/includes/bootstrap.php';
require_once __DIR__ . '/admin/includes/data_providers.php';

// Check admin authentication
if (empty($_SESSION['admin_logged_in'])) {
    header('Location: /admin_login.php');
    exit;
}

$page_title = 'System Settings';
$success_message = '';
$error_message = '';
$active_tab = $_GET['tab'] ?? 'general';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        if (isset($_POST['action'])) {
            switch ($_POST['action']) {
                case 'save_general':
                    $settings = [
                        'website_shortname' => $_POST['website_shortname'] ?? '',
                        'head_title' => $_POST['head_title'] ?? '',
                        'backend_email' => $_POST['backend_email'] ?? '',
                        'backend_email_fromname' => $_POST['backend_email_fromname'] ?? ''
                    ];
                    if (admin_save_multiple_settings($pdo, $settings)) {
                        $success_message = 'General settings saved successfully!';
                    } else {
                        $error_message = 'Failed to save general settings.';
                    }
                    break;

                case 'save_modules':
                    $modules = [
                        'video_module', 'live_module', 'short_module',
                        'image_module', 'audio_module', 'document_module',
                        'blog_module', 'paid_memberships', 'token_system_enabled'
                    ];
                    foreach ($modules as $module) {
                        $enabled = isset($_POST[$module]) && $_POST[$module] === '1';
                        admin_toggle_module($pdo, $module, $enabled);
                    }
                    $success_message = 'Module settings saved successfully!';
                    break;

                case 'save_branding':
                    $settings = [
                        'website_shortname' => $_POST['site_name'] ?? '',
                        'head_title' => $_POST['site_title'] ?? '',
                        'branding_primary_color' => $_POST['primary_color'] ?? '#1a73e8',
                        'branding_secondary_color' => $_POST['secondary_color'] ?? '#34a853',
                        'branding_logo_url' => $_POST['logo_url'] ?? '',
                        'branding_favicon_url' => $_POST['favicon_url'] ?? '',
                        'branding_footer_text' => $_POST['footer_text'] ?? ''
                    ];
                    if (admin_save_multiple_settings($pdo, $settings)) {
                        $success_message = 'Branding settings saved successfully!';
                    }
                    break;

                case 'save_payment':
                    $settings = [
                        'payment_methods' => $_POST['payment_methods'] ?? 'Paypal',
                        'paypal_email' => $_POST['paypal_email'] ?? '',
                        'paypal_test' => isset($_POST['paypal_test']) ? '1' : '0',
                        'paypal_client_id' => $_POST['paypal_client_id'] ?? '',
                        'paypal_secret' => $_POST['paypal_secret'] ?? '',
                        'stripe_enabled' => isset($_POST['stripe_enabled']) ? '1' : '0',
                        'stripe_publishable_key' => $_POST['stripe_publishable_key'] ?? '',
                        'stripe_secret_key' => $_POST['stripe_secret_key'] ?? '',
                        'stripe_webhook_secret' => $_POST['stripe_webhook_secret'] ?? ''
                    ];
                    if (admin_save_multiple_settings($pdo, $settings)) {
                        $success_message = 'Payment settings saved successfully!';
                    }
                    break;

                case 'save_email':
                    $settings = [
                        'mail_type' => $_POST['mail_type'] ?? 'smtp',
                        'backend_email' => $_POST['backend_email'] ?? '',
                        'backend_email_fromname' => $_POST['backend_email_fromname'] ?? '',
                        'mail_smtp_host' => $_POST['mail_smtp_host'] ?? '',
                        'mail_smtp_port' => $_POST['mail_smtp_port'] ?? '587',
                        'mail_smtp_username' => $_POST['mail_smtp_username'] ?? '',
                        'mail_smtp_password' => $_POST['mail_smtp_password'] ?? '',
                        'mail_smtp_auth' => isset($_POST['mail_smtp_auth']) ? 'true' : 'false',
                        'mail_smtp_prefix' => $_POST['mail_smtp_prefix'] ?? 'tls'
                    ];
                    if (admin_save_multiple_settings($pdo, $settings)) {
                        $success_message = 'Email settings saved successfully!';
                    }
                    break;

                case 'save_payout':
                    $settings = [
                        'creator_payout_enabled' => isset($_POST['creator_payout_enabled']) ? '1' : '0',
                        'creator_payout_percentage' => $_POST['creator_payout_percentage'] ?? '70',
                        'minimum_payout_amount' => $_POST['minimum_payout_amount'] ?? '50.00',
                        'payout_schedule' => $_POST['payout_schedule'] ?? 'monthly',
                        'payout_method' => $_POST['payout_method'] ?? 'paypal'
                    ];
                    if (admin_save_multiple_settings($pdo, $settings)) {
                        $success_message = 'Creator payout settings saved successfully!';
                    }
                    break;

                case 'save_seo':
                    $settings = [
                        'head_title' => $_POST['head_title'] ?? '',
                        'metaname_description' => $_POST['metaname_description'] ?? '',
                        'metaname_keywords' => $_POST['metaname_keywords'] ?? ''
                    ];
                    if (admin_save_multiple_settings($pdo, $settings)) {
                        $success_message = 'SEO settings saved successfully!';
                    }
                    break;

                case 'save_security':
                    $settings = [
                        'signup_min_age' => $_POST['signup_min_age'] ?? '18',
                        'signup_max_age' => $_POST['signup_max_age'] ?? '70',
                        'signup_min_password' => $_POST['signup_min_password'] ?? '5',
                        'signup_max_password' => $_POST['signup_max_password'] ?? '15',
                        'signup_min_username' => $_POST['signup_min_username'] ?? '5',
                        'signup_max_username' => $_POST['signup_max_username'] ?? '15',
                        'login_remember' => isset($_POST['login_remember']) ? '1' : '0',
                        'username_format' => $_POST['username_format'] ?? 'strict'
                    ];
                    if (admin_save_multiple_settings($pdo, $settings)) {
                        $success_message = 'Security settings saved successfully!';
                    }
                    break;
            }
        }
    } catch (Exception $e) {
        $error_message = 'Error: ' . htmlspecialchars($e->getMessage());
    }
}

// Fetch current settings
$module_status = admin_fetch_module_status($pdo);
$branding_settings = admin_fetch_branding_settings($pdo);
$payment_settings = admin_fetch_payment_settings($pdo);
$email_settings = admin_fetch_email_settings($pdo);
$payout_settings = admin_fetch_payout_settings($pdo);

// Fetch individual settings for other tabs
$general_settings = [
    'website_shortname' => admin_get_setting($pdo, 'website_shortname') ?? 'EasyStream',
    'head_title' => admin_get_setting($pdo, 'head_title') ?? 'EasyStream',
    'backend_email' => admin_get_setting($pdo, 'backend_email') ?? '',
    'backend_email_fromname' => admin_get_setting($pdo, 'backend_email_fromname') ?? 'Webmaster'
];

$seo_settings = [
    'head_title' => admin_get_setting($pdo, 'head_title') ?? '',
    'metaname_description' => admin_get_setting($pdo, 'metaname_description') ?? '',
    'metaname_keywords' => admin_get_setting($pdo, 'metaname_keywords') ?? ''
];

$security_settings = [
    'signup_min_age' => admin_get_setting($pdo, 'signup_min_age') ?? '18',
    'signup_max_age' => admin_get_setting($pdo, 'signup_max_age') ?? '70',
    'signup_min_password' => admin_get_setting($pdo, 'signup_min_password') ?? '5',
    'signup_max_password' => admin_get_setting($pdo, 'signup_max_password') ?? '15',
    'signup_min_username' => admin_get_setting($pdo, 'signup_min_username') ?? '5',
    'signup_max_username' => admin_get_setting($pdo, 'signup_max_username') ?? '15',
    'login_remember' => admin_get_setting($pdo, 'login_remember') === '1',
    'username_format' => admin_get_setting($pdo, 'username_format') ?? 'strict'
];

require_once __DIR__ . '/admin/includes/layout.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($page_title) ?> - EasyStream Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #1a73e8;
            --sidebar-width: 250px;
        }
        body { background: #f8f9fa; }
        .main-content { margin-left: var(--sidebar-width); padding: 20px; }
        .settings-card { background: white; border-radius: 8px; padding: 25px; margin-bottom: 20px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
        .nav-tabs .nav-link { color: #666; }
        .nav-tabs .nav-link.active { color: var(--primary-color); border-bottom: 2px solid var(--primary-color); }
        .form-label { font-weight: 500; margin-bottom: 8px; }
        .form-text { font-size: 0.875rem; color: #6c757d; }
        .color-preview { width: 40px; height: 40px; border-radius: 4px; border: 1px solid #ddd; display: inline-block; vertical-align: middle; margin-left: 10px; }
        .switch { position: relative; display: inline-block; width: 50px; height: 24px; }
        .switch input { opacity: 0; width: 0; height: 0; }
        .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 24px; }
        .slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
        input:checked + .slider { background-color: var(--primary-color); }
        input:checked + .slider:before { transform: translateX(26px); }
        .module-item { padding: 15px; border: 1px solid #e0e0e0; border-radius: 6px; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; }
        .badge-pill { padding: 6px 12px; border-radius: 20px; font-size: 0.75rem; }
    </style>
</head>
<body>
    <?php render_admin_sidebar('settings'); ?>

    <div class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="bi bi-gear-fill"></i> System Settings</h2>
            <a href="admin.php" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left"></i> Back to Dashboard
            </a>
        </div>

        <?php if ($success_message): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="bi bi-check-circle-fill"></i> <?= htmlspecialchars($success_message) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <?php if ($error_message): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-exclamation-triangle-fill"></i> <?= htmlspecialchars($error_message) ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <ul class="nav nav-tabs mb-4" role="tablist">
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'general' ? 'active' : '' ?>" href="?tab=general">General</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'modules' ? 'active' : '' ?>" href="?tab=modules">Modules</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'branding' ? 'active' : '' ?>" href="?tab=branding">Branding</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'payment' ? 'active' : '' ?>" href="?tab=payment">Payments</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'email' ? 'active' : '' ?>" href="?tab=email">Email</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'payout' ? 'active' : '' ?>" href="?tab=payout">Creator Payouts</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'seo' ? 'active' : '' ?>" href="?tab=seo">SEO</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $active_tab === 'security' ? 'active' : '' ?>" href="?tab=security">Security</a>
            </li>
        </ul>

        <div class="tab-content">
            <!-- GENERAL SETTINGS TAB -->
            <?php if ($active_tab === 'general'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-info-circle"></i> General Settings</h4>
                    <form method="POST" action="?tab=general">
                        <input type="hidden" name="action" value="save_general">

                        <div class="mb-3">
                            <label for="website_shortname" class="form-label">Website Name</label>
                            <input type="text" class="form-control" id="website_shortname" name="website_shortname"
                                   value="<?= htmlspecialchars($general_settings['website_shortname']) ?>">
                            <div class="form-text">The short name of your website (e.g., "EasyStream")</div>
                        </div>

                        <div class="mb-3">
                            <label for="head_title" class="form-label">Site Title</label>
                            <input type="text" class="form-control" id="head_title" name="head_title"
                                   value="<?= htmlspecialchars($general_settings['head_title']) ?>">
                            <div class="form-text">The title shown in browser tabs</div>
                        </div>

                        <div class="mb-3">
                            <label for="backend_email" class="form-label">Admin Email</label>
                            <input type="email" class="form-control" id="backend_email" name="backend_email"
                                   value="<?= htmlspecialchars($general_settings['backend_email']) ?>">
                            <div class="form-text">Primary email for admin notifications</div>
                        </div>

                        <div class="mb-3">
                            <label for="backend_email_fromname" class="form-label">Email From Name</label>
                            <input type="text" class="form-control" id="backend_email_fromname" name="backend_email_fromname"
                                   value="<?= htmlspecialchars($general_settings['backend_email_fromname']) ?>">
                            <div class="form-text">Name displayed in outgoing emails</div>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Save General Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <!-- MODULES TAB -->
            <?php if ($active_tab === 'modules'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-toggle-on"></i> Module Management</h4>
                    <p class="text-muted">Enable or disable features on your platform</p>

                    <form method="POST" action="?tab=modules">
                        <input type="hidden" name="action" value="save_modules">

                        <?php foreach ($module_status as $module): ?>
                            <div class="module-item">
                                <div>
                                    <strong><?= htmlspecialchars($module['label']) ?></strong>
                                    <br>
                                    <small class="text-muted">Key: <?= htmlspecialchars($module['key']) ?></small>
                                </div>
                                <div>
                                    <label class="switch">
                                        <input type="checkbox" name="<?= htmlspecialchars($module['key']) ?>"
                                               value="1" <?= $module['enabled'] ? 'checked' : '' ?>>
                                        <span class="slider"></span>
                                    </label>
                                </div>
                            </div>
                        <?php endforeach; ?>

                        <button type="submit" class="btn btn-primary mt-3">
                            <i class="bi bi-save"></i> Save Module Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <!-- BRANDING TAB -->
            <?php if ($active_tab === 'branding'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-palette"></i> Branding & Design</h4>

                    <form method="POST" action="?tab=branding">
                        <input type="hidden" name="action" value="save_branding">

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="site_name" class="form-label">Site Name</label>
                                <input type="text" class="form-control" id="site_name" name="site_name"
                                       value="<?= htmlspecialchars($branding_settings['site_name']) ?>">
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="site_title" class="form-label">Site Title</label>
                                <input type="text" class="form-control" id="site_title" name="site_title"
                                       value="<?= htmlspecialchars($branding_settings['site_title']) ?>">
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="primary_color" class="form-label">Primary Color</label>
                                <div class="d-flex align-items-center">
                                    <input type="color" class="form-control form-control-color" id="primary_color" name="primary_color"
                                           value="<?= htmlspecialchars($branding_settings['primary_color']) ?>">
                                    <input type="text" class="form-control ms-2"
                                           value="<?= htmlspecialchars($branding_settings['primary_color']) ?>" readonly>
                                </div>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="secondary_color" class="form-label">Secondary Color</label>
                                <div class="d-flex align-items-center">
                                    <input type="color" class="form-control form-control-color" id="secondary_color" name="secondary_color"
                                           value="<?= htmlspecialchars($branding_settings['secondary_color']) ?>">
                                    <input type="text" class="form-control ms-2"
                                           value="<?= htmlspecialchars($branding_settings['secondary_color']) ?>" readonly>
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="logo_url" class="form-label">Logo URL</label>
                            <input type="url" class="form-control" id="logo_url" name="logo_url"
                                   value="<?= htmlspecialchars($branding_settings['logo_url']) ?>"
                                   placeholder="https://yoursite.com/logo.png">
                            <div class="form-text">URL to your site logo</div>
                        </div>

                        <div class="mb-3">
                            <label for="favicon_url" class="form-label">Favicon URL</label>
                            <input type="url" class="form-control" id="favicon_url" name="favicon_url"
                                   value="<?= htmlspecialchars($branding_settings['favicon_url']) ?>"
                                   placeholder="https://yoursite.com/favicon.ico">
                            <div class="form-text">URL to your site favicon</div>
                        </div>

                        <div class="mb-3">
                            <label for="footer_text" class="form-label">Footer Text</label>
                            <textarea class="form-control" id="footer_text" name="footer_text" rows="3"><?= htmlspecialchars($branding_settings['footer_text']) ?></textarea>
                            <div class="form-text">Custom text for your site footer</div>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Save Branding Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <!-- PAYMENT TAB -->
            <?php if ($active_tab === 'payment'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-credit-card"></i> Payment Gateway Configuration</h4>

                    <form method="POST" action="?tab=payment">
                        <input type="hidden" name="action" value="save_payment">

                        <div class="mb-4">
                            <label class="form-label">Enabled Payment Methods</label>
                            <select class="form-select" name="payment_methods">
                                <option value="Paypal" <?= $payment_settings['payment_methods'] === 'Paypal' ? 'selected' : '' ?>>PayPal Only</option>
                                <option value="Stripe" <?= $payment_settings['payment_methods'] === 'Stripe' ? 'selected' : '' ?>>Stripe Only</option>
                                <option value="Paypal,Stripe" <?= $payment_settings['payment_methods'] === 'Paypal,Stripe' ? 'selected' : '' ?>>Both PayPal & Stripe</option>
                            </select>
                        </div>

                        <h5 class="mb-3">PayPal Configuration</h5>
                        <div class="mb-3">
                            <label for="paypal_email" class="form-label">PayPal Email</label>
                            <input type="email" class="form-control" id="paypal_email" name="paypal_email"
                                   value="<?= htmlspecialchars($payment_settings['paypal_email']) ?>">
                        </div>

                        <div class="mb-3">
                            <label for="paypal_client_id" class="form-label">PayPal Client ID</label>
                            <input type="text" class="form-control" id="paypal_client_id" name="paypal_client_id"
                                   value="<?= htmlspecialchars($payment_settings['paypal_client_id']) ?>">
                        </div>

                        <div class="mb-3">
                            <label for="paypal_secret" class="form-label">PayPal Secret</label>
                            <input type="password" class="form-control" id="paypal_secret" name="paypal_secret"
                                   value="<?= htmlspecialchars($payment_settings['paypal_secret']) ?>">
                        </div>

                        <div class="mb-4">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="paypal_test" name="paypal_test"
                                       <?= $payment_settings['paypal_test'] ? 'checked' : '' ?>>
                                <label class="form-check-label" for="paypal_test">
                                    Enable PayPal Sandbox/Test Mode
                                </label>
                            </div>
                        </div>

                        <hr class="my-4">

                        <h5 class="mb-3">Stripe Configuration</h5>
                        <div class="mb-3">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="stripe_enabled" name="stripe_enabled"
                                       <?= $payment_settings['stripe_enabled'] ? 'checked' : '' ?>>
                                <label class="form-check-label" for="stripe_enabled">
                                    Enable Stripe Payments
                                </label>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="stripe_publishable_key" class="form-label">Stripe Publishable Key</label>
                            <input type="text" class="form-control" id="stripe_publishable_key" name="stripe_publishable_key"
                                   value="<?= htmlspecialchars($payment_settings['stripe_publishable_key']) ?>">
                        </div>

                        <div class="mb-3">
                            <label for="stripe_secret_key" class="form-label">Stripe Secret Key</label>
                            <input type="password" class="form-control" id="stripe_secret_key" name="stripe_secret_key"
                                   value="<?= htmlspecialchars($payment_settings['stripe_secret_key']) ?>">
                        </div>

                        <div class="mb-3">
                            <label for="stripe_webhook_secret" class="form-label">Stripe Webhook Secret</label>
                            <input type="password" class="form-control" id="stripe_webhook_secret" name="stripe_webhook_secret"
                                   value="<?= htmlspecialchars($payment_settings['stripe_webhook_secret']) ?>">
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Save Payment Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <!-- EMAIL TAB -->
            <?php if ($active_tab === 'email'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-envelope"></i> Email & SMTP Configuration</h4>

                    <form method="POST" action="?tab=email">
                        <input type="hidden" name="action" value="save_email">

                        <div class="mb-3">
                            <label for="mail_type" class="form-label">Mailer Type</label>
                            <select class="form-select" id="mail_type" name="mail_type">
                                <option value="smtp" <?= $email_settings['mail_type'] === 'smtp' ? 'selected' : '' ?>>SMTP</option>
                                <option value="mail" <?= $email_settings['mail_type'] === 'mail' ? 'selected' : '' ?>>PHP Mail</option>
                            </select>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="backend_email" class="form-label">From Email</label>
                                <input type="email" class="form-control" id="backend_email" name="backend_email"
                                       value="<?= htmlspecialchars($email_settings['backend_email']) ?>">
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="backend_email_fromname" class="form-label">From Name</label>
                                <input type="text" class="form-control" id="backend_email_fromname" name="backend_email_fromname"
                                       value="<?= htmlspecialchars($email_settings['backend_email_fromname']) ?>">
                            </div>
                        </div>

                        <h5 class="mt-4 mb-3">SMTP Settings</h5>

                        <div class="row">
                            <div class="col-md-8 mb-3">
                                <label for="mail_smtp_host" class="form-label">SMTP Host</label>
                                <input type="text" class="form-control" id="mail_smtp_host" name="mail_smtp_host"
                                       value="<?= htmlspecialchars($email_settings['mail_smtp_host']) ?>"
                                       placeholder="smtp.gmail.com">
                            </div>

                            <div class="col-md-4 mb-3">
                                <label for="mail_smtp_port" class="form-label">SMTP Port</label>
                                <input type="number" class="form-control" id="mail_smtp_port" name="mail_smtp_port"
                                       value="<?= htmlspecialchars($email_settings['mail_smtp_port']) ?>">
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="mail_smtp_username" class="form-label">SMTP Username</label>
                            <input type="text" class="form-control" id="mail_smtp_username" name="mail_smtp_username"
                                   value="<?= htmlspecialchars($email_settings['mail_smtp_username']) ?>">
                        </div>

                        <div class="mb-3">
                            <label for="mail_smtp_password" class="form-label">SMTP Password</label>
                            <input type="password" class="form-control" id="mail_smtp_password" name="mail_smtp_password"
                                   value="<?= htmlspecialchars($email_settings['mail_smtp_password']) ?>">
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="mail_smtp_prefix" class="form-label">Encryption</label>
                                <select class="form-select" id="mail_smtp_prefix" name="mail_smtp_prefix">
                                    <option value="tls" <?= $email_settings['mail_smtp_prefix'] === 'tls' ? 'selected' : '' ?>>TLS</option>
                                    <option value="ssl" <?= $email_settings['mail_smtp_prefix'] === 'ssl' ? 'selected' : '' ?>>SSL</option>
                                    <option value="" <?= empty($email_settings['mail_smtp_prefix']) ? 'selected' : '' ?>>None</option>
                                </select>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label class="form-label">Authentication</label>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" id="mail_smtp_auth" name="mail_smtp_auth"
                                           <?= $email_settings['mail_smtp_auth'] ? 'checked' : '' ?>>
                                    <label class="form-check-label" for="mail_smtp_auth">
                                        Require SMTP Authentication
                                    </label>
                                </div>
                            </div>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Save Email Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <!-- CREATOR PAYOUTS TAB -->
            <?php if ($active_tab === 'payout'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-cash-coin"></i> Creator Payout Configuration</h4>

                    <form method="POST" action="?tab=payout">
                        <input type="hidden" name="action" value="save_payout">

                        <div class="mb-4">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="creator_payout_enabled" name="creator_payout_enabled"
                                       <?= $payout_settings['creator_payout_enabled'] ? 'checked' : '' ?>>
                                <label class="form-check-label" for="creator_payout_enabled">
                                    <strong>Enable Creator Payouts</strong>
                                </label>
                            </div>
                            <div class="form-text">Allow creators to earn money and receive payouts</div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="creator_payout_percentage" class="form-label">Creator Revenue Share (%)</label>
                                <input type="number" class="form-control" id="creator_payout_percentage" name="creator_payout_percentage"
                                       value="<?= $payout_settings['creator_payout_percentage'] ?>"
                                       min="0" max="100" step="1">
                                <div class="form-text">Percentage of revenue that goes to creators (0-100%)</div>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="minimum_payout_amount" class="form-label">Minimum Payout Amount ($)</label>
                                <input type="number" class="form-control" id="minimum_payout_amount" name="minimum_payout_amount"
                                       value="<?= $payout_settings['minimum_payout_amount'] ?>"
                                       min="0" step="0.01">
                                <div class="form-text">Minimum balance required for payout</div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="payout_schedule" class="form-label">Payout Schedule</label>
                                <select class="form-select" id="payout_schedule" name="payout_schedule">
                                    <option value="weekly" <?= $payout_settings['payout_schedule'] === 'weekly' ? 'selected' : '' ?>>Weekly</option>
                                    <option value="monthly" <?= $payout_settings['payout_schedule'] === 'monthly' ? 'selected' : '' ?>>Monthly</option>
                                    <option value="quarterly" <?= $payout_settings['payout_schedule'] === 'quarterly' ? 'selected' : '' ?>>Quarterly</option>
                                    <option value="manual" <?= $payout_settings['payout_schedule'] === 'manual' ? 'selected' : '' ?>>Manual</option>
                                </select>
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="payout_method" class="form-label">Default Payout Method</label>
                                <select class="form-select" id="payout_method" name="payout_method">
                                    <option value="paypal" <?= $payout_settings['payout_method'] === 'paypal' ? 'selected' : '' ?>>PayPal</option>
                                    <option value="stripe" <?= $payout_settings['payout_method'] === 'stripe' ? 'selected' : '' ?>>Stripe</option>
                                    <option value="bank_transfer" <?= $payout_settings['payout_method'] === 'bank_transfer' ? 'selected' : '' ?>>Bank Transfer</option>
                                </select>
                            </div>
                        </div>

                        <div class="alert alert-info">
                            <i class="bi bi-info-circle"></i>
                            <strong>Note:</strong> Creator payout functionality requires payment gateway integration.
                            Make sure to configure your payment settings before enabling payouts.
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Save Payout Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <!-- SEO TAB -->
            <?php if ($active_tab === 'seo'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-search"></i> SEO & Metadata</h4>

                    <form method="POST" action="?tab=seo">
                        <input type="hidden" name="action" value="save_seo">

                        <div class="mb-3">
                            <label for="head_title" class="form-label">Site Title</label>
                            <input type="text" class="form-control" id="head_title" name="head_title"
                                   value="<?= htmlspecialchars($seo_settings['head_title']) ?>">
                            <div class="form-text">Appears in browser tabs and search results</div>
                        </div>

                        <div class="mb-3">
                            <label for="metaname_description" class="form-label">Meta Description</label>
                            <textarea class="form-control" id="metaname_description" name="metaname_description"
                                      rows="3" maxlength="160"><?= htmlspecialchars($seo_settings['metaname_description']) ?></textarea>
                            <div class="form-text">Brief description for search engines (max 160 characters)</div>
                        </div>

                        <div class="mb-3">
                            <label for="metaname_keywords" class="form-label">Meta Keywords</label>
                            <textarea class="form-control" id="metaname_keywords" name="metaname_keywords"
                                      rows="3"><?= htmlspecialchars($seo_settings['metaname_keywords']) ?></textarea>
                            <div class="form-text">Comma-separated keywords (e.g., "video, streaming, media")</div>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Save SEO Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>

            <!-- SECURITY TAB -->
            <?php if ($active_tab === 'security'): ?>
                <div class="settings-card">
                    <h4 class="mb-4"><i class="bi bi-shield-check"></i> Security & User Settings</h4>

                    <form method="POST" action="?tab=security">
                        <input type="hidden" name="action" value="save_security">

                        <h5 class="mb-3">User Registration</h5>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="signup_min_age" class="form-label">Minimum Age</label>
                                <input type="number" class="form-control" id="signup_min_age" name="signup_min_age"
                                       value="<?= $security_settings['signup_min_age'] ?>" min="13" max="100">
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="signup_max_age" class="form-label">Maximum Age</label>
                                <input type="number" class="form-control" id="signup_max_age" name="signup_max_age"
                                       value="<?= $security_settings['signup_max_age'] ?>" min="13" max="150">
                            </div>
                        </div>

                        <h5 class="mt-4 mb-3">Username Requirements</h5>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="signup_min_username" class="form-label">Minimum Length</label>
                                <input type="number" class="form-control" id="signup_min_username" name="signup_min_username"
                                       value="<?= $security_settings['signup_min_username'] ?>" min="3" max="20">
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="signup_max_username" class="form-label">Maximum Length</label>
                                <input type="number" class="form-control" id="signup_max_username" name="signup_max_username"
                                       value="<?= $security_settings['signup_max_username'] ?>" min="5" max="50">
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="username_format" class="form-label">Username Format</label>
                            <select class="form-select" id="username_format" name="username_format">
                                <option value="strict" <?= $security_settings['username_format'] === 'strict' ? 'selected' : '' ?>>Strict (letters and numbers only)</option>
                                <option value="relaxed" <?= $security_settings['username_format'] === 'relaxed' ? 'selected' : '' ?>>Relaxed (allow special characters)</option>
                            </select>
                        </div>

                        <h5 class="mt-4 mb-3">Password Requirements</h5>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="signup_min_password" class="form-label">Minimum Length</label>
                                <input type="number" class="form-control" id="signup_min_password" name="signup_min_password"
                                       value="<?= $security_settings['signup_min_password'] ?>" min="5" max="20">
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="signup_max_password" class="form-label">Maximum Length</label>
                                <input type="number" class="form-control" id="signup_max_password" name="signup_max_password"
                                       value="<?= $security_settings['signup_max_password'] ?>" min="8" max="100">
                            </div>
                        </div>

                        <h5 class="mt-4 mb-3">Login Settings</h5>

                        <div class="mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="login_remember" name="login_remember"
                                       <?= $security_settings['login_remember'] ? 'checked' : '' ?>>
                                <label class="form-check-label" for="login_remember">
                                    Enable "Remember Me" Feature
                                </label>
                            </div>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save"></i> Save Security Settings
                        </button>
                    </form>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Auto-update color input text when color picker changes
        document.querySelectorAll('input[type="color"]').forEach(input => {
            input.addEventListener('change', function() {
                const textInput = this.parentElement.querySelector('input[type="text"]');
                if (textInput) {
                    textInput.value = this.value;
                }
            });
        });
    </script>
</body>
</html>
