<?php
require_once __DIR__ . '/config/config.php';
session_start();

// proteksi admin
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    die("Akses ditolak!");
}

// ambil id
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    die("ID user tidak valid");
}

// cegah admin hapus dirinya sendiri (opsional tapi disarankan)
if (isset($_SESSION['id_user']) && $_SESSION['id_user'] == $id) {
    die("Tidak bisa menghapus akun yang sedang login");
}

// cek data user
$cek = $conn->prepare("SELECT id_user FROM users WHERE id_user = ?");
$cek->bind_param("i", $id);
$cek->execute();
$cek->store_result();

if ($cek->num_rows == 0) {
    die("Data user tidak ditemukan");
}

// hapus user
$hapus = $conn->prepare("DELETE FROM users WHERE id_user = ?");
$hapus->bind_param("i", $id);

if ($hapus->execute()) {
    header("Location: users.php?hapus=1");
    exit;
} else {
    die("Gagal menghapus user");
}
