<?php
session_start();
if (!isset($_SESSION['unohs'])) {
    header("Location: index.php");
    exit();
}
include "conn.php";

// ─────────────────────────────────────────────
// site_settings নামের ছোট্ট table (না থাকলে নিজে থেকেই তৈরি হবে)
// ─────────────────────────────────────────────
$conn->query("
    CREATE TABLE IF NOT EXISTS site_settings (
        setting_key VARCHAR(50) PRIMARY KEY,
        setting_value VARCHAR(255)
    )
");

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nagad = trim($_POST['main_nagad_number'] ?? '');
    $bkash = trim($_POST['main_bkash_number'] ?? '');

    $stmt = $conn->prepare("
        INSERT INTO site_settings (setting_key, setting_value) VALUES ('main_nagad_number', ?)
        ON DUPLICATE KEY UPDATE setting_value = ?
    ");
    $stmt->bind_param("ss", $nagad, $nagad);
    $stmt->execute();
    $stmt->close();

    $stmt2 = $conn->prepare("
        INSERT INTO site_settings (setting_key, setting_value) VALUES ('main_bkash_number', ?)
        ON DUPLICATE KEY UPDATE setting_value = ?
    ");
    $stmt2->bind_param("ss", $bkash, $bkash);
    $stmt2->execute();
    $stmt2->close();

    $message = '✅ Main Nagad/bKash Number সংরক্ষণ হয়েছে।';
}

$current = ['main_nagad_number' => '', 'main_bkash_number' => ''];
$result = $conn->query("SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('main_nagad_number','main_bkash_number')");
while ($row = $result->fetch_assoc()) {
    $current[$row['setting_key']] = $row['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Main Wallet Settings - RAJ365 Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="admin-gold-theme.css">
    <style>
        body { background: #0A0A0A; padding: 20px; }
        .form-box input { background: #171512; border: 1px solid #3A3226; color: #F5EFDC; border-radius: 10px; padding: 10px 14px; width: 100%; margin-bottom: 14px; }
    </style>
</head>
<body>
<div class="container" style="max-width:560px;">
    <h3 style="color:#D4AF37;">💰 Main Wallet Number (Fallback)</h3>
    <p style="color:#8A8270; font-size:12.5px;">যেসব User কোনো Agent-এর সরাসরি Downline না, তারা এই Number দেখবে</p>

    <?php if ($message): ?>
        <div class="alert" style="background:#171512; border:1px solid #D4AF37; color:#F5EFDC;"><?= $message ?></div>
    <?php endif; ?>

    <form method="POST" class="form-box">
        <label style="color:#8A8270; font-size:12px;">Main Nagad Number</label>
        <input type="text" name="main_nagad_number" placeholder="01XXXXXXXXX" value="<?= htmlspecialchars($current['main_nagad_number']) ?>">

        <label style="color:#8A8270; font-size:12px;">Main bKash Number</label>
        <input type="text" name="main_bkash_number" placeholder="01XXXXXXXXX" value="<?= htmlspecialchars($current['main_bkash_number']) ?>">

        <button type="submit" class="btn" style="background:#D4AF37; color:#1A1400; font-weight:700; width:100%;">Save করো</button>
    </form>

    <a href="dashboard.php" style="color:#D4AF37;">← Dashboard-এ ফিরে যান</a>
</div>
</body>
</html>
