<?php
// modules/[nama_file].php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Path BENAR untuk file di folder modules/
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/app.php';

checkAuth();

$conn = getConnection();

if (!hasRole('admin')) {
    header('Location: ../index.php');
    exit;
}

$id = $_GET['id'] ?? 0;
$status = $_GET['status'] ?? 0;

if ($id && $id != $_SESSION['user_id']) { // Prevent self-deactivation
    $conn = getConnection();
    $conn->query("UPDATE users SET is_active = $status WHERE id = $id");
    
    // Get user info for log
    $result = $conn->query("SELECT u.*, e.full_name FROM users u JOIN employees e ON u.employee_id = e.id WHERE u.id = $id");
    $user = $result->fetch_assoc();
    
    $action = $status ? 'mengaktifkan' : 'menonaktifkan';
    logActivity('status_update', 'users', $id, "$action user: {$user['full_name']}");
}

header('Location: manage_user.php');
exit;
?>