<?php
// ═══════════════════════════════════════════════════════════
// ban_user.php
// User-কে Ban/Unban করার কাজ করে (Login.php-এ verified status==1
// check অনুযায়ী — status অন্য কিছু হলে Login block হয়ে যায়)।
// ═══════════════════════════════════════════════════════════
session_start();
if (!isset($_SESSION['unohs'])) {
    header("Location: index.php");
    exit();
}
include "conn.php";

if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['user_id']) || !isset($_POST['new_status'])) {
    header("Location: manage_users.php?msg=invalid");
    exit();
}

$userId    = intval($_POST['user_id']);
$newStatus = intval($_POST['new_status']); // 1 = Active/Unban, 0 = Banned
$search    = isset($_POST['search']) ? trim($_POST['search']) : '';

if ($userId <= 0 || !in_array($newStatus, [0, 1], true)) {
    header("Location: manage_users.php?msg=invalid");
    exit();
}

$stmt = $conn->prepare("UPDATE shonu_subjects SET status = ? WHERE id = ?");
$stmt->bind_param("ii", $newStatus, $userId);

if ($stmt->execute()) {
    $msg = $newStatus === 1 ? 'unbanned' : 'banned';
} else {
    $msg = 'error';
}
$stmt->close();
$conn->close();

$redirect = "manage_users.php?msg=" . $msg;
if ($search !== '') {
    $redirect .= "&search=" . urlencode($search);
}
header("Location: " . $redirect);
exit();
