<?php
require('../session.php');

// Pastikan koneksi ada
if (!isset($conn)) {
    die('Koneksi database tidak tersedia');
}

// Ambil & validasi parameter
$id     = isset($_GET['postID']) ? (int)$_GET['postID'] : 0;
$status = isset($_GET['status']) ? (int)$_GET['status'] : 1;
$tipe   = isset($_GET['tipe']) ? (int)$_GET['tipe'] : 1;

if ($id <= 0) {
    header("Location: {$urlweb}/user.php?msg=invalid");
    exit;
}

/*
Status contoh:
1 = Aktif
2 = Suspend / Blok
*/
$status_baru = ($status == 1) ? 2 : 1;

// Cek apakah user ada
$cek = $conn->prepare("SELECT cuid FROM tb_user WHERE cuid = ?");
$cek->bind_param("i", $id);
$cek->execute();
$cek->store_result();

if ($cek->num_rows == 0) {
    header("Location: {$urlweb}/user.php?msg=notfound");
    exit;
}
$cek->close();

// Update status user
$stmt = $conn->prepare("UPDATE tb_user SET status = ? WHERE cuid = ?");
$stmt->bind_param("ii", $status_baru, $id);

if ($stmt->execute()) {

    // Redirect sesuai tipe
    if ($tipe === 1) {
        header("Location: {$urlweb}/view.php?postID={$id}&msg=success");
    } else {
        header("Location: {$urlweb}/user.php?msg=success");
    }

} else {

    header("Location: {$urlweb}/user.php?msg=failed");

}

$stmt->close();
exit;
