<?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">Website 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>
<style>
   body, .main-panel, .content-wrapper {
  background-color: #0d1117 !important;
  color: #e6edf3 !important;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.card {
  background-color: #161b22 !important;
  border: 1px solid #30363d !important;
  color: #e6edf3 !important;
  box-shadow: 0 0 10px rgba(56, 139, 253, 0.3);
}

h4.font-weight-bold.text-dark {
  color: #58a6ff !important;
}

label.form-label {
  color: #c9d1d9 !important;
  font-weight: 600;
}

input.form-control,
input.form-control:focus,
input[type="file"],
input[type="file"]:focus {
  background-color: #0d1117 !important;
  color: #e6edf3 !important;
  border: 1px solid #30363d !important;
  box-shadow: none !important;
}

input[type="radio"] {
  accent-color: #58a6ff;
}

.btn-primary {
  background-color: #2f81f7 !important;
  border-color: #2f81f7 !important;
  color: #fff !important;
}

.btn-primary:hover {
  background-color: #1f6feb !important;
  border-color: #1f6feb !important;
}

/* Alert info */
.alert-info {
  background-color: #1f2a38 !important;
  color: #58a6ff !important;
  border: 1px solid #30363d !important;
}

/* Image containers */
img {
  border-radius: 6px;
}

/* Spacing fixes for labels and inputs */
.mb-3 {
  margin-bottom: 1rem !important;
}

</style>

</html>
