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

$message = '';

// ✅ Toggle On/Off করা
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['toggle_feature'])) {
    $newValue = isset($_POST['enabled']) && $_POST['enabled'] === '1' ? '1' : '0';
    $checkStmt = $conn->prepare("SELECT id FROM site_settings WHERE setting_key = 'agent_self_wallet_edit'");
    $checkStmt->execute();
    $exists = $checkStmt->get_result()->num_rows > 0;

    if ($exists) {
        $stmt = $conn->prepare("UPDATE site_settings SET setting_value = ? WHERE setting_key = 'agent_self_wallet_edit'");
        $stmt->bind_param("s", $newValue);
    } else {
        $stmt = $conn->prepare("INSERT INTO site_settings (setting_key, setting_value) VALUES ('agent_self_wallet_edit', ?)");
        $stmt->bind_param("s", $newValue);
    }
    $stmt->execute();
    $message = $newValue === '1' ? '✅ Feature চালু করা হয়েছে — এখন Agent-রা নিজেই bKash/Nagad Number Update করতে পারবে' : '🔒 Feature বন্ধ করা হয়েছে — শুধু Admin-ই Number Update করতে পারবে';
}

// ✅ বর্তমান Status Load করা
$currentStatus = '0';
$statusResult = $conn->query("SELECT setting_value FROM site_settings WHERE setting_key = 'agent_self_wallet_edit' LIMIT 1");
if ($statusResult && $statusResult->num_rows > 0) {
    $currentStatus = $statusResult->fetch_assoc()['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>Agent Wallet-Edit Toggle</title>
<style>
    body { background: #0a0a0a; color: #fff; font-family: sans-serif; padding: 30px; }
    h2 { color: #ffd54f; margin-bottom: 20px; }
    .msg { padding: 12px 16px; border-radius: 8px; margin-bottom: 20px; font-size: 14px; }
    .msg-ok { background: rgba(34,214,122,.15); border: 1px solid rgba(34,214,122,.3); color: #22D67A; }

    .toggle-card { background: #171420; border: 1px solid #333; border-radius: 14px; padding: 24px; max-width: 500px; }
    .toggle-card .title { font-size: 16px; font-weight: 800; color: #fff; margin-bottom: 8px; }
    .toggle-card .desc { font-size: 12.5px; color: #9691B0; margin-bottom: 20px; line-height: 1.7; }

    .status-badge { display: inline-block; padding: 6px 16px; border-radius: 20px; font-size: 12px; font-weight: 800; margin-bottom: 20px; }
    .status-on { background: rgba(34,214,122,.15); color: #22D67A; }
    .status-off { background: rgba(255,92,122,.15); color: #FF5C7A; }

    .btn-row { display: flex; gap: 10px; }
    .btn { flex: 1; padding: 14px; border-radius: 10px; border: none; font-weight: 800; font-size: 13px; cursor: pointer; }
    .btn-on { background: #22D67A; color: #000; }
    .btn-off { background: #FF5C7A; color: #fff; }
    .btn:disabled { opacity: .4; cursor: not-allowed; }
</style>
</head>
<body>

<h2>🔐 Agent Self-Wallet-Edit Control</h2>

<?php if ($message): ?>
    <div class="msg msg-ok"><?= htmlspecialchars($message) ?></div>
<?php endif; ?>

<div class="toggle-card">
    <div class="title">Agent নিজে bKash/Nagad Number Update করতে পারবে কিনা?</div>
    <div class="desc">
        চালু (ON) থাকলে — প্রতিটা Agent, নিজের Panel থেকে নিজের
        bKash/Nagad Number নিজেই Update করতে পারবে।
        <br><br>
        বন্ধ (OFF) থাকলে — শুধু তুমি (Admin) "Manage Agents" থেকে
        Number Update করতে পারবে, Agent নিজে পারবে না।
    </div>

    <div class="status-badge <?= $currentStatus === '1' ? 'status-on' : 'status-off' ?>">
        বর্তমান অবস্থা: <?= $currentStatus === '1' ? 'চালু (ON)' : 'বন্ধ (OFF)' ?>
    </div>

    <form method="POST">
        <input type="hidden" name="toggle_feature" value="1">
        <div class="btn-row">
            <button type="submit" name="enabled" value="1" class="btn btn-on" <?= $currentStatus === '1' ? 'disabled' : '' ?>>চালু করো</button>
            <button type="submit" name="enabled" value="0" class="btn btn-off" <?= $currentStatus === '0' ? 'disabled' : '' ?>>বন্ধ করো</button>
        </div>
    </form>
</div>

</body>
</html>
