<?php
session_start();
if (!isset($_SESSION['unohs'])) {
    header("location:index.php?msg=unauthorized");
    exit;
}
date_default_timezone_set("Asia/Kolkata");

include 'conn.php';

function debugMessage($message) {
    echo "<pre style='background-color: #222; color: #0f0; padding: 10px; font-size: 16px;'>$message</pre>";
}

if (!$conn) {
    die("❌ Database Connection Failed: " . mysqli_connect_error());
}

// ✅ Handle Form Submission (Secure Method)
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $min_recharge = $_POST['min_recharge'];
    $min_withdrawal = $_POST['min_withdrawal'];
    $register_bonus = $_POST['register_bonus'];
    $app_download = $_POST['app_download'];
    $allowbet = isset($_POST['allowbet']) ? (int)$_POST['allowbet'] : 0;

    // 🔁 New Fields
    $telegram_active = isset($_POST['telegram_active']) ? (int)$_POST['telegram_active'] : 0;
    $telegram_link = $_POST['telegram_link'];
    $web_name = $_POST['web_name'];

    // 📌 Get existing values for logo paths
    $query = "SELECT * FROM web_setting WHERE id = 1";
    $result = mysqli_query($conn, $query);
    $settings = mysqli_fetch_assoc($result);

    $web_logo = $settings['web_logo'];
    if (isset($_FILES['web_logo']) && $_FILES['web_logo']['error'] == 0) {
        $fileName = basename($_FILES['web_logo']['name']);
        $web_logo = '../logos/' . $fileName;
        move_uploaded_file($_FILES['web_logo']['tmp_name'], $web_logo);

        // 🟢 ALSO COPY TO FRONTEND PATH with fixed name
        $frontendPath = $_SERVER['DOCUMENT_ROOT'] . "/assets/png/logo-e926b199.png";
        copy($web_logo, $frontendPath);
    }

    $favicon_logo = $settings['favicon_logo'];
    if (isset($_FILES['favicon_logo']) && $_FILES['favicon_logo']['error'] == 0) {
        $fileName = basename($_FILES['favicon_logo']['name']);
        $favicon_logo = '../logos/' . $fileName;
        move_uploaded_file($_FILES['favicon_logo']['tmp_name'], $favicon_logo);
    }

    // 🔄 Final Update Query with All Fields
    $updateQuery = "UPDATE web_setting SET 
        min_recharge = ?, 
        min_withdrawal = ?, 
        register_bonus = ?, 
        app_download = ?, 
        allowbet = ?, 
        telegram_active = ?, 
        telegram_link = ?, 
        web_name = ?, 
        web_logo = ?, 
        favicon_logo = ? 
        WHERE id = 1";

    $stmt = mysqli_prepare($conn, $updateQuery);
    mysqli_stmt_bind_param(
        $stmt,
        "iiisisssss",
        $min_recharge,
        $min_withdrawal,
        $register_bonus,
        $app_download,
        $allowbet,
        $telegram_active,
        $telegram_link,
        $web_name,
        $web_logo,
        $favicon_logo
    );

    if (mysqli_stmt_execute($stmt)) {
        $msg = "✅ Settings updated successfully!";
    } else {
        $msg = "❌ Error updating settings: " . mysqli_error($conn);
        debugMessage($msg);
    }
}

// 📌 Fetch Current Settings
$query = "SELECT * FROM web_setting WHERE id = 1";
$result = mysqli_query($conn, $query);
$settings = mysqli_fetch_assoc($result);

if (!$settings) {
    debugMessage("⚠️ Settings Fetch Nahi Hui, Database Check Karo.");
}
?>





<?php include 'header.php'; ?>


<div class="main-panel">
    <div class="content-wrapper">
        <div class="row justify-content-center">
            <div class="col-lg-10 col-md-12">
                <div class="card shadow-sm p-4">
                    <h4 class="font-weight-bold text-dark mb-4">Update Settings</h4>

                    <?php if (isset($msg)) : ?>
                        <div class="alert alert-info"><?php echo $msg; ?></div>
                    <?php endif; ?>

                    <form action="" method="POST" enctype="multipart/form-data">
                        <div class="row">

                            <!-- Left Section -->
                            <div class="col-md-6">
                                <!-- Allow Bet -->
                                <div class="mb-3">
                                    <label class="form-label">Allow Bet:</label><br>
                                    <label>
                                        <input type="radio" name="allowbet" value="1" <?= ($settings['allowbet'] == 1) ? 'checked' : ''; ?>> Bet Allowed
                                    </label>
                                    <label class="ms-3">
                                        <input type="radio" name="allowbet" value="0" <?= ($settings['allowbet'] == 0) ? 'checked' : ''; ?>> Bet Not Allowed
                                    </label>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Minimum Recharge:</label>
                                    <input type="number" name="min_recharge" class="form-control" value="<?= $settings['min_recharge']; ?>" required>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Minimum Withdrawal:</label>
                                    <input type="number" name="min_withdrawal" class="form-control" value="<?= $settings['min_withdrawal']; ?>" required>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Register Bonus:</label>
                                    <input type="number" name="register_bonus" class="form-control" value="<?= $settings['register_bonus']; ?>" required>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">App Download URL:</label>
                                    <input type="url" name="app_download" class="form-control" value="<?= $settings['app_download']; ?>" required>
                                </div>
                            </div>

                            <!-- Right Section -->
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label class="form-label">Telegram Active:</label><br>
                                    <label>
                                        <input type="radio" name="telegram_active" value="1" <?= ($settings['telegram_active'] == 1) ? 'checked' : ''; ?>> On
                                    </label>
                                    <label class="ms-3">
                                        <input type="radio" name="telegram_active" value="0" <?= ($settings['telegram_active'] == 0) ? 'checked' : ''; ?>> Off
                                    </label>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Telegram Link:</label>
                                    <input type="url" name="telegram_link" class="form-control" value="<?= $settings['telegram_link']; ?>">
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Website Name:</label>
                                    <input type="text" name="web_name" class="form-control" value="<?= $settings['web_name']; ?>">
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Website Logo:</label>
                                    <input type="file" name="web_logo" class="form-control">
                                    <?php if (!empty($settings['web_logo'])): ?>
                                        <div class="mt-2 border rounded p-2" style="width: 100px; height: 100px;">
                                            <img src="<?= $settings['web_logo']; ?>" alt="Logo" style="width: 100%; height: 100%; object-fit: contain;">
                                        </div>
                                    <?php endif; ?>
                                </div>

                                <div class="mb-3">
                                    <label class="form-label">Favicon Logo:</label>
                                    <input type="file" name="favicon_logo" class="form-control">
                                    <?php if (!empty($settings['favicon_logo'])): ?>
                                        <div class="mt-2 border rounded p-2" style="width: 60px; height: 60px;">
                                            <img src="<?= $settings['favicon_logo']; ?>" alt="Favicon" style="width: 100%; height: 100%; object-fit: contain;">
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>

                        <!-- Submit -->
                        <button type="submit" class="btn btn-primary w-100 mt-3">Update Settings</button>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>


</html>
