<?php
session_start();

// Include database connection
require_once 'layouts/dbconn.php';

if (isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
    
    // Get missing role data from database
    $stmt = $conn->prepare("
        SELECT u.role_id, ur.role_name 
        FROM users u
        LEFT JOIN user_roles ur ON u.role_id = ur.role_id
        WHERE u.user_id = ?
    ");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($user = $result->fetch_assoc()) {
        // Add missing session variables
        $_SESSION['role_id'] = $user['role_id'];
        $_SESSION['role_name'] = $user['role_name'] ?? 'Customer';
        $_SESSION['login_time'] = time();
        
        echo "<h3>Session Fixed!</h3>";
        echo "<p>Added missing role data to session:</p>";
        echo "<pre>";
        print_r($_SESSION);
        echo "</pre>";
        
        echo "<p><a href='index.php'>Go to Admin Dashboard</a></p>";
    } else {
        echo "<p>User not found in database</p>";
    }
    $stmt->close();
} else {
    echo "<p>No active session found</p>";
}
?>