<?php
/**
 * withdraw_ban_toggle.php — Admin এখান থেকে ON/OFF করবে।
 * এটাও সম্পূর্ণ নতুন, স্বতন্ত্র File — কোনো Existing Admin
 * File-কে Modify করে না।
 */
session_start();
if (!isset($_SESSION['unohs'])) {
    header("Location: index.php");
    exit();
}
include "conn.php";

$message = '';

// ✅ Security: Prepared Statement দিয়ে SQL Injection প্রতিরোধ
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'toggle_ban') {
        $userId = intval($_POST['user_id']);
        $banStatus = intval($_POST['ban_status']); // 1 = Ban করা, 0 = Ban সরানো
        $reason = trim($_POST['ban_reason'] ?? '');
        $customMsg = trim($_POST['ban_message'] ?? '');

        $stmt = $conn->prepare("UPDATE shonu_subjects SET withdraw_ban = ?, ban_reason = ?, ban_message = ? WHERE id = ?");
        $stmt->bind_param("issi", $banStatus, $reason, $customMsg, $userId);
        if ($stmt->execute() && $stmt->affected_rows >= 0) {
            $message = $banStatus
                ? "✅ User (ID: $userId)-এর Withdraw Ban করা হয়েছে।"
                : "✅ User (ID: $userId)-এর Withdraw Ban সরিয়ে দেওয়া হয়েছে।";
        }
        $stmt->close();
    }
}

// ── User খোঁজা ──
$searchResults = [];
if (isset($_GET['search']) && !empty($_GET['search'])) {
    $search = '%' . $_GET['search'] . '%';
    $stmt = $conn->prepare("SELECT id, mobile, withdraw_ban, ban_reason, ban_message FROM shonu_subjects WHERE mobile LIKE ? OR id = ? LIMIT 20");
    $searchId = is_numeric($_GET['search']) ? intval($_GET['search']) : 0;
    $stmt->bind_param("si", $search, $searchId);
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) { $searchResults[] = $row; }
    $stmt->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Withdraw Ban Toggle - 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; }
        input, textarea { background: #0A0A0A; border: 1px solid #3A3226; color: #F5EFDC; border-radius: 8px; padding: 10px 14px; width: 100%; margin-bottom: 10px; }
        .user-row { background: #171512; border: 1px solid #241C42; border-radius: 12px; padding: 14px; margin-bottom: 10px; }
        .badge-on { background: rgba(232,50,60,.15); color: #E8323C; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; }
        .badge-off { background: rgba(31,185,121,.15); color: #1FB979; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; }
        .toggle-btn { border: none; border-radius: 8px; padding: 8px 16px; font-weight: 700; }
    </style>
</head>
<body>
<div class="container" style="max-width:640px;">
    <h3 style="color:#D4AF37;">🚫 Withdraw Ban Toggle</h3>
    <p style="color:#8A8270; font-size:12.5px;">User Search করে, Withdraw Ban ON/OFF করো</p>

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

    <form method="GET" style="margin-bottom:20px;">
        <input type="text" name="search" placeholder="Mobile Number বা User ID লেখো" value="<?= htmlspecialchars($_GET['search'] ?? '') ?>">
        <button type="submit" style="background:#D4AF37; color:#1A1400; border:none; border-radius:8px; padding:10px; width:100%; font-weight:700;">খুঁজো</button>
    </form>

    <?php foreach ($searchResults as $u): ?>
        <div class="user-row">
            <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:10px;">
                <span style="color:#F5EFDC; font-weight:700;"><?= htmlspecialchars($u['mobile']) ?> (ID: <?= $u['id'] ?>)</span>
                <span class="<?= $u['withdraw_ban'] ? 'badge-on' : 'badge-off' ?>">
                    <?= $u['withdraw_ban'] ? 'Withdraw Banned' : 'Normal' ?>
                </span>
            </div>

            <form method="POST">
                <input type="hidden" name="action" value="toggle_ban">
                <input type="hidden" name="user_id" value="<?= $u['id'] ?>">
                <input type="hidden" name="ban_status" value="<?= $u['withdraw_ban'] ? 0 : 1 ?>">

                <?php if (!$u['withdraw_ban']): ?>
                    <input type="text" name="ban_reason" placeholder="Ban-এর কারণ (শুধু Admin দেখবে, ঐচ্ছিক)">
                    <textarea name="ban_message" placeholder="User-কে যা দেখাবে (খালি রাখলে Default Message দেখাবে)" rows="2"></textarea>
                    <button type="submit" class="toggle-btn" style="background:#E8323C; color:#fff; width:100%;">Ban করো (ON)</button>
                <?php else: ?>
                    <p style="color:#8A8270; font-size:12px;">কারণ: <?= htmlspecialchars($u['ban_reason'] ?: '(দেওয়া হয়নি)') ?></p>
                    <button type="submit" class="toggle-btn" style="background:#1FB979; color:#fff; width:100%;">Ban সরাও (OFF)</button>
                <?php endif; ?>
            </form>
        </div>
    <?php endforeach; ?>

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