<?php
require_once '../config/database.php';
require_once '../config/auth.php';

requireRole('super_admin');

$id = $_GET['id'] ?? 0;
if (!$id) {
    header('Location: users.php');
    exit;
}

// Tidak bisa menghapus diri sendiri
if ($id == $_SESSION['user_id']) {
    $_SESSION['error'] = 'Tidak dapat menghapus akun sendiri!';
    header('Location: users.php');
    exit;
}

$db = getDB();

try {
    $stmt = $db->prepare("DELETE FROM users WHERE id = ?");
    $stmt->execute([$id]);
    
    $_SESSION['success'] = 'User berhasil dihapus!';
} catch (PDOException $e) {
    $_SESSION['error'] = 'Terjadi kesalahan saat menghapus user!';
}

header('Location: users.php');
exit;