<?php
include 'includes/session.php'; // Include your session and database connection

if (isset($_POST['id'])) {
    $userId = $_POST['id'];

    // Update the user's status to 'Disconnected'
    $sql = "UPDATE users SET status = 'Disconnected' WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $userId); // Use prepared statements for security

    if ($stmt->execute()) {
        
         $update_logout_query = "
    UPDATE login_history 
    SET logged_out = NOW(), reason = 'Disconnected for Black List App' 
    WHERE id = (
        SELECT id 
        FROM login_history 
        WHERE user_id = $userId 
        ORDER BY logged_in DESC 
        LIMIT 1
    )";
	    $conn->query($update_logout_query);
	    
	    
	    
	    $delete_runnning_apps="DELETE from running_apps WHERE user_id = $userId";
	    $conn->query($delete_runnning_apps);


        echo "Success"; // Send a success message back to the AJAX call
    } else {
        echo "Error: " . $stmt->error;
    }

    $stmt->close();
    $conn->close();
} else {
    echo "No user ID provided.";
}
?>
