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

// Pastikan hanya admin yang bisa hapus
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php"); exit;
}

// Cek apakah ada ID yang dikirim
if (isset($_GET['id'])) {
    $id = $_GET['id'];

    // Cegah admin menghapus dirinya sendiri
    // Asumsi: ID admin yang sedang login disimpan di $_SESSION['id_user']
    if (isset($_SESSION['id_user']) && $id == $_SESSION['id_user']) {
        echo "<script>alert('Anda tidak bisa menghapus akun sendiri!'); window.location='manage_users.php';</script>";
        exit;
    }

    $stmt = $pdo->prepare("DELETE FROM users WHERE id_user = ?");
    
    if ($stmt->execute([$id])) {
        echo "<script>alert('User berhasil dihapus'); window.location='manage_users.php';</script>";
    } else {
        echo "<script>alert('Gagal menghapus user'); window.location='manage_users.php';</script>";
    }
} else {
    header("Location: manage_users.php");
}