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

$message = '';

// ── Ban দেওয়া ──
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'ban') {
    $userId = intval($_POST['user_id']);
    $requiredAmount = floatval($_POST['required_amount']);

    $stmt = $conn->prepare("UPDATE shonu_subjects SET withdraw_banned = 1, required_deposit_amount = ?, deposited_since_ban = 0 WHERE id = ?");
    $stmt->bind_param("di", $requiredAmount, $userId);
    if ($stmt->execute() && $stmt->affected_rows > 0) {
        $message = "✅ User (ID: $userId)-কে Withdraw Ban করা হয়েছে। ৳" . number_format($requiredAmount, 2) . " Deposit করলে Ban খুলে যাবে।";
    } else {
        $message = "❌ এই User খুঁজে পাওয়া যায়নি।";
    }
    $stmt->close();
}

// ── Ban সরিয়ে দেওয়া (Manual) ──
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'unban') {
    $userId = intval($_POST['user_id']);
    $stmt = $conn->prepare("UPDATE shonu_subjects SET withdraw_banned = 0, required_deposit_amount = 0, deposited_since_ban = 0 WHERE id = ?");
    $stmt->bind_param("i", $userId);
    $stmt->execute();
    $stmt->close();
    $message = "✅ User (ID: $userId)-এর Withdraw Ban সরিয়ে দেওয়া হয়েছে।";
}

// ── User খোঁজা ──
$searchResults = [];
if (isset($_GET['search']) && !empty($_GET['search'])) {
    $search = $conn->real_escape_string($_GET['search']);
    $result = $conn->query("
        SELECT id, mobile, withdraw_banned, required_deposit_amount, deposited_since_ban
        FROM shonu_subjects
        WHERE mobile LIKE '%$search%' OR id = '$search'
        LIMIT 20
    ");
    while ($row = $result->fetch_assoc()) { $searchResults[] = $row; }
}

// ── বর্তমানে Ban করা সব User ──
$bannedUsers = [];
$result = $conn->query("SELECT id, mobile, required_deposit_amount, deposited_since_ban FROM shonu_subjects WHERE withdraw_banned = 1");
while ($row = $result->fetch_assoc()) { $bannedUsers[] = $row; }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Withdraw Ban Control - 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; }
        .card-box { background: #171512; border: 1px solid rgba(212,175,55,.25); border-radius: 14px; padding: 18px; margin-bottom: 18px; }
        input { background: #0A0A0A; border: 1px solid #3A3226; color: #F5EFDC; border-radius: 8px; padding: 10px 14px; width: 100%; margin-bottom: 10px; }
        button { border: none; border-radius: 8px; padding: 10px 16px; font-weight: 700; }
        .user-row { background: #0A0A0A; border: 1px solid #241C42; border-radius: 10px; padding: 12px 14px; margin-bottom: 8px; }
        .badge-banned { background: rgba(232,50,60,.15); color: #E8323C; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; }
    </style>
</head>
<body>
<div class="container" style="max-width:640px;">
    <h3 style="color:#D4AF37;">🚫 Withdraw Ban Control</h3>
    <p style="color:#8A8270; font-size:12.5px;">নির্দিষ্ট User-এর Withdraw বন্ধ করো, নির্দিষ্ট পরিমাণ Deposit না করা পর্যন্ত</p>

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

    <div class="card-box">
        <h5 style="color:#D4AF37; margin-bottom:12px;">User খুঁজো</h5>
        <form method="GET">
            <input type="text" name="search" placeholder="Mobile Number বা User ID লেখো" value="<?= htmlspecialchars($_GET['search'] ?? '') ?>">
            <button type="submit" style="background:#D4AF37; color:#1A1400; width:100%;">খুঁজো</button>
        </form>

        <?php foreach ($searchResults as $u): ?>
            <div class="user-row">
                <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:8px;">
                    <span style="color:#F5EFDC; font-weight:700;">
                        <?= htmlspecialchars($u['mobile']) ?> (ID: <?= $u['id'] ?>)
                        <?php if ($u['withdraw_banned']): ?><span class="badge-banned">Withdraw Banned</span><?php endif; ?>
                    </span>
                </div>
                <?php if (!$u['withdraw_banned']): ?>
                    <form method="POST" style="display:flex; gap:8px;">
                        <input type="hidden" name="action" value="ban">
                        <input type="hidden" name="user_id" value="<?= $u['id'] ?>">
                        <input type="number" step="0.01" name="required_amount" placeholder="কত টাকা Deposit করলে Ban খুলবে" required style="margin-bottom:0;">
                        <button type="submit" style="background:#E8323C; color:#fff; white-space:nowrap;">Ban করো</button>
                    </form>
                <?php else: ?>
                    <div style="color:#8A8270; font-size:12px; margin-bottom:8px;">
                        প্রয়োজন: ৳<?= number_format($u['required_deposit_amount'], 2) ?> —
                        এ পর্যন্ত জমা: ৳<?= number_format($u['deposited_since_ban'], 2) ?>
                    </div>
                    <form method="POST">
                        <input type="hidden" name="action" value="unban">
                        <input type="hidden" name="user_id" value="<?= $u['id'] ?>">
                        <button type="submit" style="background:#1FB979; color:#fff; width:100%;">Manually Ban সরাও</button>
                    </form>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>

    <div class="card-box">
        <h5 style="color:#D4AF37; margin-bottom:12px;">বর্তমানে Ban করা সব User</h5>
        <?php if (empty($bannedUsers)): ?>
            <p style="color:#8A8270;">এই মুহূর্তে কেউ Ban করা নেই।</p>
        <?php else: ?>
            <?php foreach ($bannedUsers as $u): ?>
                <div class="user-row">
                    <span style="color:#F5EFDC;"><?= htmlspecialchars($u['mobile']) ?> (ID: <?= $u['id'] ?>)</span>
                    <div style="color:#8A8270; font-size:12px;">
                        প্রয়োজন: ৳<?= number_format($u['required_deposit_amount'], 2) ?> —
                        জমা হয়েছে: ৳<?= number_format($u['deposited_since_ban'], 2) ?>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

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