<?php
session_start();

// Initialize redirect parameters
$redirectParams = "";
echo "hii";
// Attempt to retrieve machine code before destroying session
if (isset($_SESSION['machine_id'])) {
    $machine_id = intval($_SESSION['machine_id']);
    
    // Path to configuration file
    $configFile = '../include/conf_property01.php';
    
    if (file_exists($configFile)) {
        // Include config to get database connection ($conn)
        include($configFile);
        
        if (isset($conn) && $conn) {
            // Query the resource_code for the current machine
            $sql = "SELECT resource_code FROM station_detail_table WHERE id = $machine_id LIMIT 1";
            $result = mysqli_query($conn, $sql);
            
            if ($result && mysqli_num_rows($result) > 0) {
                $row = mysqli_fetch_assoc($result);
                $resource_code = $row['resource_code'];
                $redirectParams = "?code=" . urlencode($resource_code);
            }
        }
    }
}

// Unset all session variables
$_SESSION = [];

// Destroy the session
session_destroy();

// Optional: prevent back-button access
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");

// Redirect to login page with the machine code if available
header("Location: index.php" . $redirectParams);
exit;
?>