<?php
//Developer: core tech web designTechnology
//Whatsapp: +2349162671360
//Telegram: @jeditech

session_start();
include('../include/config.php');

// Check if user is admin
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    header('Location: ../login.php');
    exit();
}

// Verify admin status
$stmt = $pdo->prepare("SELECT is_admin FROM users WHERE id = :user_id");
$stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user || $user['is_admin'] != 1) {
    header('Location: ../user/dashboard.php');
    exit();
}

// Handle Settings Update
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_settings'])) {
    $fields = [
        'web_url', 'logo_url', 'web_name', 'web_email', 'currency',
        'smtp_host', 'smtp_port', 'smtp_username', 'smtp_password', 'smtp_encryption', 'smtp_from_email', 'smtp_from_name',
        'web_telegram', 'support_url', 'channel_url', 
        'web_facebook', 'web_x', 'web_instagram', 'web_tiktok', 'web_youtube',
        'refer_percent', 'min_redeem',
        'telegram_bot_token', 'telegram_admin_chat_id',
        'announcement', 'telegram_group_url'
    ];
    
    $values = [];
    foreach ($fields as $field) {
        $values[$field] = trim($_POST[$field] ?? '');
    }

    $values['smtp_port'] = (int)($values['smtp_port'] ?? 587);
    $values['telegram_notifications_enabled'] = isset($_POST['telegram_notifications_enabled']) ? 1 : 0;
    
    try {
        $sql = "UPDATE settings SET 
            web_url = ?, logo_url = ?, web_name = ?, web_email = ?, currency = ?,
            smtp_host = ?, smtp_port = ?, smtp_username = ?, smtp_password = ?, smtp_encryption = ?, smtp_from_email = ?, smtp_from_name = ?,
            web_telegram = ?, support_url = ?, channel_url = ?,
            web_facebook = ?, web_x = ?, web_instagram = ?, web_tiktok = ?, web_youtube = ?,
            refer_percent = ?, min_redeem = ?,
            telegram_bot_token = ?, telegram_admin_chat_id = ?, telegram_notifications_enabled = ?,
            announcement = ?, telegram_group_url = ?
            WHERE id = 1";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute([
            $values['web_url'], $values['logo_url'], $values['web_name'], $values['web_email'], $values['currency'],
            $values['smtp_host'], $values['smtp_port'], $values['smtp_username'], $values['smtp_password'], $values['smtp_encryption'], $values['smtp_from_email'], $values['smtp_from_name'],
            $values['web_telegram'], $values['support_url'], $values['channel_url'],
            $values['web_facebook'], $values['web_x'], $values['web_instagram'], $values['web_tiktok'], $values['web_youtube'],
            $values['refer_percent'], $values['min_redeem'],
            $values['telegram_bot_token'], $values['telegram_admin_chat_id'], $values['telegram_notifications_enabled'],
            $values['announcement'], $values['telegram_group_url']
        ]);
        
        $_SESSION['success'] = "Settings updated successfully!";
    } catch(PDOException $e) {
        $_SESSION['error'] = "Error updating settings: " . $e->getMessage();
    }
    
    header('Location: manage-settings.php');
    exit();
}

// Handle Payment Settings Update
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_payment_settings'])) {
    $payment_fields = [
        'stripe_public_key', 'stripe_private_key', 'stripe_webhook_secret', 'stripe_min',
        'paystack_public_key', 'paystack_secret_key',
        'nowpayment_api', 'nowpayment_ipn', 'nowpayment_min',
        'crypto_api_key', 'crypto_merchant_id',
        'btc_minimum', 'eth_minimum', 'usdt_trc20_minimum', 'usdt_erc20_minimum'
    ];
    
    $values = [];
    foreach ($payment_fields as $field) {
        $values[$field] = trim($_POST[$field] ?? '');
    }
    
    try {
        $sql = "UPDATE settings SET 
            stripe_public_key = ?, stripe_private_key = ?, stripe_webhook_secret = ?, stripe_min = ?,
            paystack_public_key = ?, paystack_secret_key = ?,
            nowpayment_api = ?, nowpayment_ipn = ?, nowpayment_min = ?,
            crypto_api_key = ?, crypto_merchant_id = ?,
            btc_minimum = ?, eth_minimum = ?, usdt_trc20_minimum = ?, usdt_erc20_minimum = ?
            WHERE id = 1";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute([
            $values['stripe_public_key'], $values['stripe_private_key'], $values['stripe_webhook_secret'], $values['stripe_min'],
            $values['paystack_public_key'], $values['paystack_secret_key'],
            $values['nowpayment_api'], $values['nowpayment_ipn'], $values['nowpayment_min'],
            $values['crypto_api_key'], $values['crypto_merchant_id'],
            $values['btc_minimum'], $values['eth_minimum'], $values['usdt_trc20_minimum'], $values['usdt_erc20_minimum']
        ]);
        
        $_SESSION['success'] = "Payment settings updated successfully!";
    } catch(PDOException $e) {
        $_SESSION['error'] = "Error updating payment settings: " . $e->getMessage();
    }
    
    header('Location: manage-settings.php#payment');
    exit();
}

// Fetch current settings
$settings = $pdo->query("SELECT * FROM settings WHERE id = 1")->fetch(PDO::FETCH_ASSOC);

$page_title = WEB_NAME . " | Manage Settings";
?>
<!DOCTYPE html>
<html lang="en" class="light">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        .tab-button {
            transition: all 0.3s ease;
        }
        .tab-button.active {
            background: linear-gradient(to right, #2563eb, #1d4ed8);
            color: white;
        }
    </style>
    
    <link rel="shortcut icon" type="image/png" href="<?php echo $site_data['web_url']; ?>/public/favicons/favicon.png">
</head>
<body class="bg-gradient-to-br from-slate-50 to-slate-100 min-h-screen p-4 lg:p-8">

<div class="max-w-6xl mx-auto space-y-6">
    
    <!-- Header -->
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-3xl font-bold text-slate-900">Website Settings</h1>
            <p class="text-slate-600 mt-1">Configure your website settings and integrations</p>
        </div>
        <a href="dashboard.php" class="px-4 py-2 bg-slate-200 hover:bg-slate-300 rounded-lg transition-colors">
            <i class="fas fa-arrow-left mr-2"></i>Back to Dashboard
        </a>
    </div>

    <!-- Alert Messages -->
    <?php if (isset($_SESSION['success'])): ?>
        <div class="p-4 bg-green-50 border border-green-200 rounded-lg">
            <div class="flex items-center">
                <i class="fas fa-check-circle text-green-600 mr-3"></i>
                <span class="text-green-800 font-medium"><?php echo $_SESSION['success']; unset($_SESSION['success']); ?></span>
            </div>
        </div>
    <?php endif; ?>

    <?php if (isset($_SESSION['error'])): ?>
        <div class="p-4 bg-red-50 border border-red-200 rounded-lg">
            <div class="flex items-center">
                <i class="fas fa-times-circle text-red-600 mr-3"></i>
                <span class="text-red-800 font-medium"><?php echo $_SESSION['error']; unset($_SESSION['error']); ?></span>
            </div>
        </div>
    <?php endif; ?>

    <!-- Tabs -->
    <div class="bg-white rounded-xl shadow p-2 flex gap-2 overflow-x-auto">
        <button onclick="showTab('general')" id="tab-general" class="tab-button active px-4 py-2 rounded-lg text-sm font-semibold whitespace-nowrap">
            <i class="fas fa-cog mr-2"></i>General
        </button>
        <button onclick="showTab('social')" id="tab-social" class="tab-button px-4 py-2 rounded-lg text-sm font-semibold whitespace-nowrap text-slate-700">
            <i class="fas fa-share-alt mr-2"></i>Social Media
        </button>
        <button onclick="showTab('payment')" id="tab-payment" class="tab-button px-4 py-2 rounded-lg text-sm font-semibold whitespace-nowrap text-slate-700">
            <i class="fas fa-credit-card mr-2"></i>Payment
        </button>
        <button onclick="showTab('telegram')" id="tab-telegram" class="tab-button px-4 py-2 rounded-lg text-sm font-semibold whitespace-nowrap text-slate-700">
            <i class="fab fa-telegram mr-2"></i>Telegram
        </button>
        <button onclick="showTab('modules')" id="tab-modules" class="tab-button px-4 py-2 rounded-lg text-sm font-semibold whitespace-nowrap text-slate-700">
            <i class="fas fa-toggle-on mr-2"></i>Modules
        </button>
    </div>

    <!-- General Settings -->
    <div id="content-general" class="tab-content">
        <div class="bg-white rounded-2xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-slate-900 mb-6">
                <i class="fas fa-cog text-blue-600 mr-2"></i>General Settings
            </h2>
            
            <form method="POST" class="space-y-6">
                <input type="hidden" name="update_settings" value="1">
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Website Name *</label>
                        <input type="text" name="web_name" value="<?php echo htmlspecialchars($settings['web_name']); ?>" required
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Website Email *</label>
                        <input type="email" name="web_email" value="<?php echo htmlspecialchars($settings['web_email']); ?>" required
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div class="md:col-span-2">
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Website URL *</label>
                        <input type="url" name="web_url" value="<?php echo htmlspecialchars($settings['web_url']); ?>" required
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div class="md:col-span-2">
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Logo URL *</label>
                        <input type="url" name="logo_url" value="<?php echo htmlspecialchars($settings['logo_url']); ?>" required
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Currency *</label>
                        <input type="text" name="currency" value="<?php echo htmlspecialchars($settings['currency']); ?>" required
                               placeholder="e.g., NGN, USD"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Referral Percentage (%)</label>
                        <input type="number" name="refer_percent" value="<?php echo htmlspecialchars($settings['refer_percent']); ?>" 
                               min="0" max="100" step="0.01"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">Minimum Redeem Amount</label>
                        <input type="number" name="min_redeem" value="<?php echo htmlspecialchars($settings['min_redeem']); ?>" 
                               min="0" step="0.01"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>

                <!-- SMTP Email Settings -->
                <div class="border-t border-slate-200 pt-5 mt-5">
                    <h3 class="text-sm font-bold text-slate-800 mb-4"><i class="fas fa-envelope text-purple-600 mr-2"></i>SMTP Email Settings</h3>
                    <p class="text-xs text-slate-500 mb-4">These values are used by signup, password reset, contact form, and bulk emails across the site. Leave empty to fall back to the server mail function.</p>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">SMTP Host</label>
                            <input type="text" name="smtp_host" value="<?php echo htmlspecialchars($settings['smtp_host'] ?? ''); ?>"
                                   placeholder="smtp.hostinger.com"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">SMTP Port</label>
                            <input type="number" name="smtp_port" value="<?php echo htmlspecialchars($settings['smtp_port'] ?? 587); ?>"
                                   min="1" max="65535"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">SMTP Username</label>
                            <input type="text" name="smtp_username" value="<?php echo htmlspecialchars($settings['smtp_username'] ?? ''); ?>"
                                   placeholder="info@yourdomain.com"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">SMTP Password</label>
                            <input type="password" name="smtp_password" value="<?php echo htmlspecialchars($settings['smtp_password'] ?? ''); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">SMTP Encryption</label>
                            <select name="smtp_encryption" class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                                <option value="tls" <?php echo (($settings['smtp_encryption'] ?? 'tls') === 'tls') ? 'selected' : ''; ?>>TLS</option>
                                <option value="ssl" <?php echo (($settings['smtp_encryption'] ?? 'tls') === 'ssl') ? 'selected' : ''; ?>>SSL</option>
                                <option value="" <?php echo (($settings['smtp_encryption'] ?? 'tls') === '') ? 'selected' : ''; ?>>None</option>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">From Email</label>
                            <input type="email" name="smtp_from_email" value="<?php echo htmlspecialchars($settings['smtp_from_email'] ?? ''); ?>"
                                   placeholder="noreply@yourdomain.com"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div class="md:col-span-2">
                            <label class="block text-sm font-semibold text-slate-700 mb-2">From Name</label>
                            <input type="text" name="smtp_from_name" value="<?php echo htmlspecialchars($settings['smtp_from_name'] ?? ''); ?>"
                                   placeholder="Your Site Name"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                    </div>
                </div>

                <!-- Announcement & Telegram -->
                <div class="border-t border-slate-200 pt-5 mt-5">
                    <h3 class="text-sm font-bold text-slate-800 mb-4"><i class="fas fa-bullhorn text-orange-500 mr-2"></i>Dashboard Notice & Telegram</h3>
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Dashboard Announcement (scrolling text)</label>
                            <textarea name="announcement" rows="3" placeholder="e.g. Welcome to SMS PRIDE! All services working perfectly..."
                                      class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500 text-sm"><?php echo htmlspecialchars($settings['announcement'] ?? ''); ?></textarea>
                            <p class="text-xs text-slate-500 mt-1">This text scrolls on the user dashboard. Leave empty for default message.</p>
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Telegram Group Link</label>
                            <input type="url" name="telegram_group_url" value="<?php echo htmlspecialchars($settings['telegram_group_url'] ?? ''); ?>" 
                                   placeholder="https://t.me/yourgroup"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                            <p class="text-xs text-slate-500 mt-1">Shows a "Join Telegram" card on user dashboard. Leave empty to hide.</p>
                        </div>
                    </div>
                </div>

                <button type="submit" class="w-full md:w-auto px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 hover:from-blue-700 hover:to-blue-800 text-white font-semibold rounded-lg transition-all">
                    <i class="fas fa-save mr-2"></i>Save General Settings
                </button>
            </form>
        </div>
    </div>

    <!-- Social Media Settings -->
    <div id="content-social" class="tab-content hidden">
        <div class="bg-white rounded-2xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-slate-900 mb-6">
                <i class="fas fa-share-alt text-blue-600 mr-2"></i>Social Media & Support Links
            </h2>
            
            <form method="POST" class="space-y-6">
                <input type="hidden" name="update_settings" value="1">
                
                <!-- Hidden fields for general settings to prevent overwriting -->
                <input type="hidden" name="web_url" value="<?php echo htmlspecialchars($settings['web_url']); ?>">
                <input type="hidden" name="logo_url" value="<?php echo htmlspecialchars($settings['logo_url']); ?>">
                <input type="hidden" name="web_name" value="<?php echo htmlspecialchars($settings['web_name']); ?>">
                <input type="hidden" name="web_email" value="<?php echo htmlspecialchars($settings['web_email']); ?>">
                <input type="hidden" name="currency" value="<?php echo htmlspecialchars($settings['currency']); ?>">
                <input type="hidden" name="refer_percent" value="<?php echo htmlspecialchars($settings['refer_percent']); ?>">
                <input type="hidden" name="min_redeem" value="<?php echo htmlspecialchars($settings['min_redeem']); ?>">
                <input type="hidden" name="telegram_bot_token" value="<?php echo htmlspecialchars($settings['telegram_bot_token']); ?>">
                <input type="hidden" name="telegram_admin_chat_id" value="<?php echo htmlspecialchars($settings['telegram_admin_chat_id']); ?>">
                <?php if($settings['telegram_notifications_enabled']): ?>
                <input type="hidden" name="telegram_notifications_enabled" value="1">
                <?php endif; ?>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fab fa-telegram text-blue-500 mr-2"></i>Telegram Username
                        </label>
                        <input type="text" name="web_telegram" value="<?php echo htmlspecialchars($settings['web_telegram']); ?>"
                               placeholder="@username"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fas fa-headset text-blue-500 mr-2"></i>Support URL
                        </label>
                        <input type="url" name="support_url" value="<?php echo htmlspecialchars($settings['support_url']); ?>"
                               placeholder="https://t.me/support"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fab fa-telegram-plane text-blue-500 mr-2"></i>Channel URL
                        </label>
                        <input type="url" name="channel_url" value="<?php echo htmlspecialchars($settings['channel_url']); ?>"
                               placeholder="https://t.me/channel"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fab fa-facebook text-blue-600 mr-2"></i>Facebook URL
                        </label>
                        <input type="url" name="web_facebook" value="<?php echo htmlspecialchars($settings['web_facebook']); ?>"
                               placeholder="https://facebook.com/page"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fab fa-x-twitter text-slate-900 mr-2"></i>X (Twitter) URL
                        </label>
                        <input type="url" name="web_x" value="<?php echo htmlspecialchars($settings['web_x']); ?>"
                               placeholder="https://x.com/profile"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fab fa-instagram text-pink-600 mr-2"></i>Instagram URL
                        </label>
                        <input type="url" name="web_instagram" value="<?php echo htmlspecialchars($settings['web_instagram']); ?>"
                               placeholder="https://instagram.com/profile"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fab fa-tiktok text-slate-900 mr-2"></i>TikTok URL
                        </label>
                        <input type="url" name="web_tiktok" value="<?php echo htmlspecialchars($settings['web_tiktok']); ?>"
                               placeholder="https://tiktok.com/@profile"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>

                    <div>
                        <label class="block text-sm font-semibold text-slate-700 mb-2">
                            <i class="fab fa-youtube text-red-600 mr-2"></i>YouTube URL
                        </label>
                        <input type="url" name="web_youtube" value="<?php echo htmlspecialchars($settings['web_youtube']); ?>"
                               placeholder="https://youtube.com/channel"
                               class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>

                <button type="submit" class="w-full md:w-auto px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 hover:from-blue-700 hover:to-blue-800 text-white font-semibold rounded-lg transition-all">
                    <i class="fas fa-save mr-2"></i>Save Social Media Settings
                </button>
            </form>
        </div>
    </div>

    <!-- Payment Settings -->
    <div id="content-payment" class="tab-content hidden">
        <div class="bg-white rounded-2xl shadow-lg p-6">
            <h2 class="text-xl font-bold text-slate-900 mb-6">
                <i class="fas fa-credit-card text-blue-600 mr-2"></i>Payment Gateway Settings
            </h2>
            
            <form method="POST" class="space-y-8">
                <input type="hidden" name="update_payment_settings" value="1">
                
                <!-- Stripe Settings -->
                <div class="border-b border-slate-200 pb-6">
                    <h3 class="text-lg font-bold text-slate-900 mb-4">
                        <i class="fab fa-stripe text-purple-600 mr-2"></i>Stripe Configuration
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Public Key</label>
                            <input type="text" name="stripe_public_key" value="<?php echo htmlspecialchars($settings['stripe_public_key']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-purple-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Private Key</label>
                            <input type="text" name="stripe_private_key" value="<?php echo htmlspecialchars($settings['stripe_private_key']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-purple-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Webhook Secret</label>
                            <input type="text" name="stripe_webhook_secret" value="<?php echo htmlspecialchars($settings['stripe_webhook_secret']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-purple-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Minimum Amount</label>
                            <input type="number" name="stripe_min" value="<?php echo htmlspecialchars($settings['stripe_min']); ?>" step="0.01"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-purple-500">
                        </div>
                    </div>
                </div>

                <!-- Paystack Settings -->
                <div class="border-b border-slate-200 pb-6">
                    <h3 class="text-lg font-bold text-slate-900 mb-4">
                        <i class="fas fa-wallet text-green-600 mr-2"></i>Paystack Configuration
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Public Key</label>
                            <input type="text" name="paystack_public_key" value="<?php echo htmlspecialchars($settings['paystack_public_key']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-green-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Secret Key</label>
                            <input type="text" name="paystack_secret_key" value="<?php echo htmlspecialchars($settings['paystack_secret_key']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-green-500">
                        </div>
                    </div>
                </div>

                <!-- NOWPayments Settings -->
                <div class="border-b border-slate-200 pb-6">
                    <h3 class="text-lg font-bold text-slate-900 mb-4">
                        <i class="fab fa-bitcoin text-orange-600 mr-2"></i>NOWPayments Configuration
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">API Key</label>
                            <input type="text" name="nowpayment_api" value="<?php echo htmlspecialchars($settings['nowpayment_api']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-orange-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">IPN Secret</label>
                            <input type="text" name="nowpayment_ipn" value="<?php echo htmlspecialchars($settings['nowpayment_ipn']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-orange-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Minimum Amount</label>
                            <input type="number" name="nowpayment_min" value="<?php echo htmlspecialchars($settings['nowpayment_min']); ?>" step="0.01"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-orange-500">
                        </div>
                    </div>
                </div>

                <!-- Crypto Settings -->
                <div class="border-b border-slate-200 pb-6">
                    <h3 class="text-lg font-bold text-slate-900 mb-4">
                        <i class="fas fa-coins text-blue-600 mr-2"></i>Crypto Configuration
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">API Key</label>
                            <input type="text" name="crypto_api_key" value="<?php echo htmlspecialchars($settings['crypto_api_key']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Merchant ID</label>
                            <input type="text" name="crypto_merchant_id" value="<?php echo htmlspecialchars($settings['crypto_merchant_id']); ?>"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                    </div>
                </div>

                <!-- Minimum Amounts -->
                <div>
                    <h3 class="text-lg font-bold text-slate-900 mb-4">
                        <i class="fas fa-chart-line text-green-600 mr-2"></i>Minimum Transaction Amounts
                    </h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">BTC Minimum</label>
                            <input type="number" name="btc_minimum" value="<?php echo htmlspecialchars($settings['btc_minimum']); ?>" step="0.000001"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-orange-500">
                        </div>
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">ETH Minimum</label>
                            <input type="number" name="eth_minimum" value="<?php echo htmlspecialchars($settings['eth_minimum']); ?>" step="0.0001"
                                   class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div><function_calls>
<invoke name="artifacts">
<parameter name="command">update</parameter>
<parameter name="id">manage_settings</parameter>
<parameter name="old_str">                        <div>
<label class="block text-sm font-semibold text-slate-700 mb-2">ETH Minimum</label>
<input type="number" name="eth_minimum" value="<?php echo htmlspecialchars($settings['eth_minimum']); ?>" step="0.0001"
                                class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
</div>
<div></parameter>
<parameter name="new_str">                        <div>
<label class="block text-sm font-semibold text-slate-700 mb-2">ETH Minimum</label>
<input type="number" name="eth_minimum" value="<?php echo htmlspecialchars($settings['eth_minimum']); ?>" step="0.0001"
                                class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
</div>
<div>
<label class="block text-sm font-semibold text-slate-700 mb-2">USDT TRC20 Minimum</label>
<input type="number" name="usdt_trc20_minimum" value="<?php echo htmlspecialchars($settings['usdt_trc20_minimum']); ?>" step="0.01"
                                class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-green-500">
</div>
<div>
<label class="block text-sm font-semibold text-slate-700 mb-2">USDT ERC20 Minimum</label>
<input type="number" name="usdt_erc20_minimum" value="<?php echo htmlspecialchars($settings['usdt_erc20_minimum']); ?>" step="0.01"
                                class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-green-500">
</div>
</div>
</div>
<button type="submit" class="w-full md:w-auto px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 hover:from-blue-700 hover:to-blue-800 text-white font-semibold rounded-lg transition-all">
                <i class="fas fa-save mr-2"></i>Save Payment Settings
            </button>
        </form>
    </div>
</div>

<!-- Telegram Settings -->
<div id="content-telegram" class="tab-content hidden">
    <div class="bg-white rounded-2xl shadow-lg p-6">
        <h2 class="text-xl font-bold text-slate-900 mb-6">
            <i class="fab fa-telegram text-blue-600 mr-2"></i>Telegram Bot Configuration
        </h2>
        
        <form method="POST" class="space-y-6">
            <input type="hidden" name="update_settings" value="1">
            
            <!-- Hidden fields for other settings -->
            <input type="hidden" name="web_url" value="<?php echo htmlspecialchars($settings['web_url']); ?>">
            <input type="hidden" name="logo_url" value="<?php echo htmlspecialchars($settings['logo_url']); ?>">
            <input type="hidden" name="web_name" value="<?php echo htmlspecialchars($settings['web_name']); ?>">
            <input type="hidden" name="web_email" value="<?php echo htmlspecialchars($settings['web_email']); ?>">
            <input type="hidden" name="currency" value="<?php echo htmlspecialchars($settings['currency']); ?>">
            <input type="hidden" name="refer_percent" value="<?php echo htmlspecialchars($settings['refer_percent']); ?>">
            <input type="hidden" name="min_redeem" value="<?php echo htmlspecialchars($settings['min_redeem']); ?>">
            <input type="hidden" name="web_telegram" value="<?php echo htmlspecialchars($settings['web_telegram']); ?>">
            <input type="hidden" name="support_url" value="<?php echo htmlspecialchars($settings['support_url']); ?>">
            <input type="hidden" name="channel_url" value="<?php echo htmlspecialchars($settings['channel_url']); ?>">
            <input type="hidden" name="web_facebook" value="<?php echo htmlspecialchars($settings['web_facebook']); ?>">
            <input type="hidden" name="web_x" value="<?php echo htmlspecialchars($settings['web_x']); ?>">
            <input type="hidden" name="web_instagram" value="<?php echo htmlspecialchars($settings['web_instagram']); ?>">
            <input type="hidden" name="web_tiktok" value="<?php echo htmlspecialchars($settings['web_tiktok']); ?>">
            <input type="hidden" name="web_youtube" value="<?php echo htmlspecialchars($settings['web_youtube']); ?>">
            
            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-semibold text-slate-700 mb-2">Bot Token</label>
                    <input type="text" name="telegram_bot_token" value="<?php echo htmlspecialchars($settings['telegram_bot_token']); ?>"
                           placeholder="1234567890:ABCdefGHIjklMNOpqrsTUVwxyz"
                           class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    <p class="text-xs text-slate-500 mt-1">Get this from @BotFather on Telegram</p>
                </div>

                <div>
                    <label class="block text-sm font-semibold text-slate-700 mb-2">Admin Chat ID</label>
                    <input type="text" name="telegram_admin_chat_id" value="<?php echo htmlspecialchars($settings['telegram_admin_chat_id']); ?>"
                           placeholder="123456789"
                           class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                    <p class="text-xs text-slate-500 mt-1">Your Telegram user ID for receiving notifications</p>
                </div>

                <label class="flex items-center p-4 border border-slate-300 rounded-lg cursor-pointer hover:bg-slate-50">
                    <input type="checkbox" name="telegram_notifications_enabled" 
                           <?php echo $settings['telegram_notifications_enabled'] ? 'checked' : ''; ?> 
                           class="mr-3">
                    <div>
                        <div class="text-sm font-semibold text-slate-700">Enable Telegram Notifications</div>
                        <div class="text-xs text-slate-500">Receive real-time notifications for important events</div>
                    </div>
                </label>
            </div>

            <div class="p-4 bg-blue-50 border border-blue-200 rounded-lg">
                <h4 class="text-sm font-semibold text-blue-900 mb-2">How to set up:</h4>
                <ol class="text-xs text-blue-800 space-y-1 list-decimal list-inside">
                    <li>Create a bot using @BotFather on Telegram</li>
                    <li>Copy the bot token and paste it above</li>
                    <li>Get your Chat ID from @userinfobot</li>
                    <li>Start a conversation with your bot</li>
                    <li>Enable notifications to receive updates</li>
                </ol>
            </div>

            <button type="submit" class="w-full md:w-auto px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 hover:from-blue-700 hover:to-blue-800 text-white font-semibold rounded-lg transition-all">
                <i class="fas fa-save mr-2"></i>Save Telegram Settings
            </button>
        </form>
    </div>
</div>
</div>
<!-- Module Toggle Settings -->
<div id="content-modules" class="tab-content hidden">
    <div class="bg-white rounded-2xl shadow-lg p-6">
        <h2 class="text-xl font-bold text-slate-900 mb-2">
            <i class="fas fa-toggle-on text-blue-600 mr-2"></i>Module Toggles
        </h2>
        <p class="text-slate-500 text-sm mb-6">Enable or disable modules. Disabled modules will be hidden from the user dashboard and side menu.</p>
        
        <form method="POST" action="ajax/update_settings.php" id="moduleToggleForm">
            <input type="hidden" name="action" value="save_module_toggles">
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                <?php
                $modules = [
                    'module_virtual_numbers' => ['Virtual Numbers', 'fa-comment-sms', 'text-blue-600'],
                    'module_smm'            => ['Social Boost (SMM)', 'fa-share-nodes', 'text-indigo-600'],
                    'module_buy_data'       => ['Buy Data', 'fa-wifi', 'text-green-600'],
                    'module_buy_airtime'    => ['Buy Airtime', 'fa-phone-volume', 'text-red-600'],
                    'module_cable_tv'       => ['Cable TV', 'fa-tv', 'text-yellow-600'],
                    'module_electricity'    => ['Electricity', 'fa-bolt-lightning', 'text-orange-500'],
                    'module_buy_giftcard'   => ['Buy Gift Cards', 'fa-gift', 'text-pink-600'],
                    'module_sell_giftcard'  => ['Sell Gift Cards', 'fa-tags', 'text-purple-600'],
                    'module_virtual_cards'  => ['Virtual Cards', 'fa-credit-card', 'text-indigo-500'],
                    'module_marketplace'    => ['Marketplace', 'fa-store', 'text-teal-600'],
                    'module_sell_account'   => ['Sell Account / Become Seller', 'fa-shop', 'text-pink-500'],
                    'module_referral'       => ['Referral System', 'fa-users', 'text-cyan-600'],
                    'module_withdraw'       => ['Withdraw', 'fa-money-bill-transfer', 'text-emerald-600'],
                    'module_buy_logs'       => ['Buy Logs', 'fa-file-lines', 'text-amber-600'],
                ];
                foreach ($modules as $key => $info):
                    $is_on = intval($settings[$key] ?? 1);
                ?>
                <div class="flex items-center justify-between p-4 border border-slate-200 rounded-xl hover:border-blue-200 transition-colors">
                    <div class="flex items-center gap-3">
                        <div class="w-10 h-10 rounded-lg bg-slate-50 flex items-center justify-center <?php echo $info[2]; ?>">
                            <i class="fa-solid <?php echo $info[1]; ?>"></i>
                        </div>
                        <span class="text-sm font-semibold text-slate-700"><?php echo $info[0]; ?></span>
                    </div>
                    <label class="relative inline-flex items-center cursor-pointer">
                        <input type="checkbox" name="<?php echo $key; ?>" value="1" <?php echo $is_on ? 'checked' : ''; ?> class="sr-only peer">
                        <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-100 rounded-full peer peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-600"></div>
                    </label>
                </div>
                <?php endforeach; ?>
            </div>

            <div class="mt-6">
                <button type="submit" class="w-full md:w-auto px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 hover:from-blue-700 hover:to-blue-800 text-white font-semibold rounded-lg transition-all">
                    <i class="fas fa-save mr-2"></i>Save Module Settings
                </button>
            </div>
        </form>
    </div>
</div>

<script>
// Module toggle form - AJAX submit
document.getElementById('moduleToggleForm')?.addEventListener('submit', function(e) {
    e.preventDefault();
    const formData = new FormData(this);
    fetch('ajax/update_settings.php', {
        method: 'POST',
        body: formData
    })
    .then(r => r.json())
    .then(data => {
        if (data.status === '1') {
            // Show success
            const alert = document.createElement('div');
            alert.className = 'fixed top-4 right-4 z-50 p-4 bg-green-50 border border-green-200 rounded-lg shadow-lg';
            alert.innerHTML = '<div class="flex items-center"><i class="fas fa-check-circle text-green-600 mr-3"></i><span class="text-green-800 font-medium">' + data.msg + '</span></div>';
            document.body.appendChild(alert);
            setTimeout(() => alert.remove(), 3000);
        } else {
            alert('Error: ' + data.msg);
        }
    })
    .catch(err => alert('Network error'));
});
</script>

<script>
function showTab(tabName) {
    // Hide all tab contents
    const contents = document.querySelectorAll('.tab-content');
    contents.forEach(content => content.classList.add('hidden'));
    
    // Remove active class from all buttons
    const buttons = document.querySelectorAll('.tab-button');
    buttons.forEach(button => button.classList.remove('active'));
    
    // Show selected tab content
    document.getElementById(`content-${tabName}`).classList.remove('hidden');
    
    // Add active class to selected button
    document.getElementById(`tab-${tabName}`).classList.add('active');
}

// Handle hash navigation
window.addEventListener('DOMContentLoaded', () => {
    const hash = window.location.hash.substring(1);
    if (hash && ['general', 'social', 'payment', 'telegram', 'modules'].includes(hash)) {
        showTab(hash);
    }
});
</script>
</body>
</html>