<?php
session_start();
include "conn.php"; // Database Connection

// --- 1. HANDLE FORM SUBMISSION ---
$msg = "";
if (isset($_POST['update_settings'])) {
    $winRate = intval($_POST['win_rate']);
    
    // Update Query
    $sql = "UPDATE chicken_game_control SET control_value = '$winRate' WHERE control_name = 'win_percentage'";
    
    if ($conn->query($sql) === TRUE) {
        $msg = "<div class='alert success'>Settings Updated Successfully! New Win Rate: $winRate%</div>";
    } else {
        $msg = "<div class='alert error'>Error updating record: " . $conn->error . "</div>";
    }
}

// --- 2. FETCH CURRENT DATA ---
$currentVal = 50; // Default
$sqlGet = "SELECT control_value FROM chicken_game_control WHERE control_name = 'win_percentage'";
$result = $conn->query($sqlGet);
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $currentVal = $row['control_value'];
}

// Calculate Difficulty Label
$statusLabel = "Balanced";
$statusColor = "#f1c40f"; // Yellow

if($currentVal < 30) { 
    $statusLabel = "🔥🔥 HARDCORE PROFIT (Users Lose)"; 
    $statusColor = "#e74c3c"; // Red
}
elseif($currentVal > 70) { 
    $statusLabel = "🤑 EASY WIN (Users Win)"; 
    $statusColor = "#2ecc71"; // Green
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chicken Road Control Center</title>
    <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@500;700;900&display=swap" rel="stylesheet">
    <script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
    <style>
        :root {
            --bg-dark: #0f121b;
            --card-bg: #1c2030;
            --accent: #6c5ce7;
            --text: #a4b0be;
            --white: #ffffff;
        }

        * { box-sizing: border-box; font-family: 'Rajdhani', sans-serif; }

        body {
            background-color: var(--bg-dark);
            color: var(--text);
            margin: 0;
            display: flex;
            min-height: 100vh;
        }

        /* SIDEBAR */
        .sidebar {
            width: 250px;
            background: #151922;
            padding: 20px;
            border-right: 1px solid #333;
            display: flex; flex-direction: column; gap: 20px;
        }
        .logo { font-size: 24px; color: #fff; font-weight: 900; letter-spacing: 1px; margin-bottom: 20px; }
        .logo span { color: #f1c40f; }
        
        .menu-item {
            padding: 15px; border-radius: 8px; color: #777; font-weight: 700; cursor: pointer; transition: 0.3s;
            display: flex; align-items: center; gap: 10px;
        }
        .menu-item:hover, .menu-item.active { background: var(--accent); color: #fff; }

        /* CONTENT */
        .main-content { flex: 1; padding: 40px; }

        .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; }
        .page-title { font-size: 32px; color: #fff; font-weight: 800; }

        /* CARD STYLE */
        .card {
            background: var(--card-bg);
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
            border: 1px solid #2c3e50;
            max-width: 800px;
            margin: 0 auto;
        }

        .card-header { border-bottom: 1px solid #333; padding-bottom: 20px; margin-bottom: 20px; }
        .card-title { font-size: 22px; color: #fff; font-weight: 700; }

        /* SLIDER SECTION */
        .control-group { margin-bottom: 30px; }
        .label-row { display: flex; justify-content: space-between; margin-bottom: 10px; font-size: 18px; font-weight: 700; }
        
        input[type=range] {
            -webkit-appearance: none; width: 100%; height: 15px; border-radius: 10px; background: #0f121b; outline: none;
            box-shadow: inset 0 2px 5px rgba(0,0,0,0.5);
        }
        input[type=range]::-webkit-slider-thumb {
            -webkit-appearance: none; appearance: none; width: 35px; height: 35px; border-radius: 50%; 
            background: var(--accent); cursor: pointer; border: 4px solid #fff; box-shadow: 0 0 20px var(--accent);
        }

        /* PRESET BUTTONS */
        .presets { display: flex; gap: 10px; margin-top: 20px; }
        .btn-pre {
            flex: 1; padding: 12px; border: none; border-radius: 8px; font-weight: 800; cursor: pointer; transition: 0.2s;
            background: #2f3640; color: #fff; border: 1px solid #444;
        }
        .btn-pre:hover { transform: translateY(-3px); }
        .btn-pre.danger { background: rgba(231, 76, 60, 0.2); color: #e74c3c; border-color: #e74c3c; }
        .btn-pre.safe { background: rgba(46, 204, 113, 0.2); color: #2ecc71; border-color: #2ecc71; }

        /* SUBMIT BUTTON */
        .btn-save {
            width: 100%; padding: 18px; border: none; border-radius: 10px; margin-top: 30px;
            background: linear-gradient(45deg, #6c5ce7, #a29bfe); color: #fff; font-size: 20px; font-weight: 900;
            cursor: pointer; text-transform: uppercase; letter-spacing: 2px; box-shadow: 0 10px 20px rgba(108, 92, 231, 0.4);
            transition: 0.2s;
        }
        .btn-save:hover { transform: scale(1.02); }

        /* ALERTS */
        .alert { padding: 15px; border-radius: 8px; margin-bottom: 20px; font-weight: bold; text-align: center; }
        .success { background: rgba(46, 204, 113, 0.2); color: #2ecc71; border: 1px solid #2ecc71; }
        .error { background: rgba(231, 76, 60, 0.2); color: #e74c3c; border: 1px solid #e74c3c; }

        /* METER */
        .meter-box { text-align: center; margin-bottom: 30px; }
        .big-val { font-size: 60px; font-weight: 900; color: #fff; display: block; line-height: 1; margin-bottom: 10px;}
        .status-txt { font-size: 16px; font-weight: 700; letter-spacing: 1px; padding: 5px 15px; border-radius: 20px; background: #000; }

    </style>
</head>
<body>

    <div class="sidebar">
        <div class="logo">ADMIN <span>PANEL</span></div>
        <div class="menu-item active">🎮 Game Control</div>
        <div class="menu-item">💰 User Balance</div>
        <div class="menu-item">📊 Profit Report</div>
        <div class="menu-item">⚙️ Settings</div>
    </div>

    <div class="main-content">
        <div class="header">
            <div class="page-title">Chicken Road Controller</div>
            <div style="font-weight:bold; color:#f1c40f;">Admin: SuperUser</div>
        </div>

        <?php echo $msg; ?>

        <div class="card">
            <div class="card-header">
                <div class="card-title">Win Rate & Logic Controller</div>
                <p style="font-size:12px; margin-top:5px;">Control the global winning probability for all users.</p>
            </div>

            <div class="meter-box">
                <span class="big-val" id="displayVal"><?php echo $currentVal; ?>%</span>
                <span class="status-txt" style="color: <?php echo $statusColor; ?>; border: 1px solid <?php echo $statusColor; ?>">
                    <?php echo $statusLabel; ?>
                </span>
            </div>

            <form method="POST">
                <div class="control-group">
                    <div class="label-row">
                        <span style="color: #e74c3c;">Profit Mode (Users Lose)</span>
                        <span style="color: #2ecc71;">User Win Mode</span>
                    </div>
                    
                    <input type="range" name="win_rate" id="slider" min="0" max="100" value="<?php echo $currentVal; ?>" oninput="updateUI(this.value)">
                    
                    <div class="presets">
                        <button type="button" class="btn-pre danger" onclick="setSlider(20)">High Profit (20%)</button>
                        <button type="button" class="btn-pre" onclick="setSlider(50)">Balanced (50%)</button>
                        <button type="button" class="btn-pre safe" onclick="setSlider(80)">Give Away (80%)</button>
                    </div>
                </div>

                <button type="submit" name="update_settings" class="btn-save">Update Game Logic</button>
            </form>
        </div>
    </div>

    <script>
        const slider = document.getElementById('slider');
        const displayVal = document.getElementById('displayVal');

        function updateUI(val) {
            displayVal.innerText = val + "%";
            // Dynamic Color Change
            if(val < 30) displayVal.style.color = "#e74c3c"; // Red
            else if(val > 70) displayVal.style.color = "#2ecc71"; // Green
            else displayVal.style.color = "#f1c40f"; // Yellow
        }

        function setSlider(val) {
            slider.value = val;
            updateUI(val);
        }

        // Init Color
        updateUI(slider.value);
    </script>

</body>
</html>