<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
    <title>Session Diagnostic</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .box { background: #f0f8ff; padding: 15px; margin: 10px 0; border-left: 4px solid #0066cc; }
        .error { background: #ffe6e6; border-left-color: #cc0000; }
        .success { background: #e6ffe6; border-left-color: #00cc00; }
        .btn { background: #0066cc; color: white; padding: 10px 20px; border: none; cursor: pointer; margin: 5px; text-decoration: none; display: inline-block; }
    </style>
</head>
<body>
    <h1>🔍 Session Diagnostic Tool</h1>
    
    <div class="box">
        <h3>📋 Current Session Status</h3>
        <p><strong>Session ID:</strong> <?php echo session_id(); ?></p>
        <p><strong>Session Status:</strong> <?php echo (session_status() == PHP_SESSION_ACTIVE) ? '✅ Active' : '❌ Inactive'; ?></p>
        <p><strong>Session Save Path:</strong> <?php echo session_save_path(); ?></p>
        <p><strong>Current Time:</strong> <?php echo date('Y-m-d H:i:s'); ?></p>
    </div>
    
    <div class="box">
        <h3>📦 Session Data</h3>
        <?php if (empty($_SESSION)): ?>
            <p style="color: red;">❌ Session is completely empty</p>
        <?php else: ?>
            <p style="color: green;">✅ Session contains data:</p>
            <pre><?php print_r($_SESSION); ?></pre>
        <?php endif; ?>
    </div>
    
    <div class="box">
        <h3>🧪 Session Tests</h3>
        
        <h4>Test 1: Set a test value</h4>
        <?php
        $_SESSION['test_value'] = 'Session is working at ' . date('H:i:s');
        echo "<p style='color: green;'>✅ Set test value: " . $_SESSION['test_value'] . "</p>";
        ?>
        
        <h4>Test 2: Check if we can maintain session across requests</h4>
        <p>Click this link and see if the session persists:</p>
        <a href="session_diagnostic.php" class="btn">🔄 Reload This Page</a>
        
        <h4>Test 3: Open in new window (like report generation does)</h4>
        <p>This simulates what happens when you click "Generate":</p>
        <a href="session_diagnostic.php" target="_blank" class="btn">🆕 Open in New Window</a>
    </div>
    
    <div class="box">
        <h3>⚙️ PHP Session Configuration</h3>
        <ul>
            <li><strong>session.use_cookies:</strong> <?php echo ini_get('session.use_cookies') ? 'Yes' : 'No'; ?></li>
            <li><strong>session.use_only_cookies:</strong> <?php echo ini_get('session.use_only_cookies') ? 'Yes' : 'No'; ?></li>
            <li><strong>session.use_trans_sid:</strong> <?php echo ini_get('session.use_trans_sid') ? 'Yes' : 'No'; ?></li>
            <li><strong>session.cookie_lifetime:</strong> <?php echo ini_get('session.cookie_lifetime'); ?> seconds</li>
            <li><strong>session.gc_maxlifetime:</strong> <?php echo ini_get('session.gc_maxlifetime'); ?> seconds</li>
        </ul>
    </div>
    
    <div class="box">
        <h3>🍪 Cookie Information</h3>
        <?php if (empty($_COOKIE)): ?>
            <p style="color: red;">❌ No cookies found</p>
        <?php else: ?>
            <p style="color: green;">✅ Cookies found:</p>
            <pre><?php print_r($_COOKIE); ?></pre>
        <?php endif; ?>
    </div>
    
    <div class="box">
        <h3>🎯 Quick Fix Test</h3>
        <p>Let's simulate a login and see if session works:</p>
        
        <?php
        // Simulate login data
        if (isset($_GET['simulate_login'])) {
            $_SESSION['user_id'] = 1;
            $_SESSION['role'] = 1;
            $_SESSION['school_id'] = 1;
            $_SESSION['username'] = 'test_admin';
            echo "<p style='color: green;'>✅ Simulated login data set!</p>";
        }
        ?>
        
        <a href="session_diagnostic.php?simulate_login=1" class="btn">🔑 Simulate Login</a>
        
        <?php if (isset($_SESSION['user_id'])): ?>
            <a href="admin_exam_grade_reports_debug.php" class="btn" target="_blank">🧪 Test Report with Session</a>
        <?php endif; ?>
    </div>
    
    <div class="box">
        <h3>📝 Recommendations</h3>
        <?php if (empty($_SESSION) && !isset($_GET['simulate_login'])): ?>
            <div class="error">
                <h4>❌ Session Problem Confirmed</h4>
                <p><strong>Issue:</strong> Session is empty when accessing this page directly</p>
                <p><strong>Solution:</strong></p>
                <ol>
                    <li>Make sure you're properly logged in on the main reports page</li>
                    <li>Check if session works by clicking the "Simulate Login" button above</li>
                    <li>If that works, the issue is with your login process</li>
                </ol>
            </div>
        <?php else: ?>
            <div class="success">
                <h4>✅ Session Working</h4>
                <p>Session appears to be functioning correctly</p>
            </div>
        <?php endif; ?>
    </div>
    
    <p><a href="admin_reports_printouts.php">← Back to Reports Page</a></p>
</body>
</html>