<?php
session_start();
include 'includes/db.php';

if (isset($_SESSION['user_id'])) {
    $conn->query("UPDATE users SET eingeloggt = 0 WHERE id = " . (int)$_SESSION['user_id']);
}

$user_id = $_SESSION['user_id'] ?? 0;
$ip = $_SERVER['REMOTE_ADDR'];

$stmt = $conn->prepare("INSERT INTO login_history (user_id, ip_address, action) VALUES (?, ?, 'logout')");
$stmt->bind_param("is", $user_id, $ip);
$stmt->execute();

$_SESSION = [];

session_destroy();
header("Location: login.php");
exit;

?>
