<?php
// FILE PATH: C:\xampp\htdocs\Project\student\results.php
session_start();
require_once "../config/database.php";

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') { 
    header("Location: ../auth/login.php"); 
    exit(); 
}

$student_id = (int)$_SESSION['user_id'];

// 1. FETCH STUDENT DATA & ACCESS STATUS
$stmtUser = $pdo->prepare("SELECT fullname, result_access FROM students WHERE id = ?");
$stmtUser->execute([$student_id]);
$user = $stmtUser->fetch();

if (!$user) { die("Profile not found."); }

// --- FEE RESTRICTION GATEKEEPER ---
if ($user['result_access'] == 0) {
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Access Restricted</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
        <style>
            body { margin: 0; font-family: 'Inter', sans-serif; background: #f8f9fa; }
            .restriction-overlay {
                position: fixed; top: 0; left: 0; width: 100%; height: 100%;
                background: rgba(26, 37, 47, 0.95);
                display: flex; justify-content: center; align-items: center;
                z-index: 10000; padding: 20px;
            }
            .restriction-card {
                background: white; color: #2c3e50;
                padding: 40px; border-radius: 16px;
                max-width: 450px; width: 100%;
                box-shadow: 0 10px 25px rgba(0,0,0,0.2);
                text-align: center;
            }
            .restriction-card i { color: #e74c3c; font-size: 60px; margin-bottom: 20px; }
            .restriction-card h2 { margin: 0 0 15px 0; color: #1a252f; font-size: 24px; }
            .restriction-card p { color: #7f8c8d; line-height: 1.6; font-size: 16px; }
            .btn-back {
                display: inline-block; margin-top: 25px;
                padding: 12px 30px; background: #3498db;
                color: white; text-decoration: none; border-radius: 8px;
                font-weight: bold; transition: 0.3s;
            }
            .btn-back:hover { background: #2980b9; transform: translateY(-2px); }
        </style>
    </head>
    <body>
        <div class="restriction-overlay">
            <div class="restriction-card">
                <i class="fa fa-lock"></i>
                <h2>Results Locked</h2>
                <p>Your academic results are currently restricted due to <strong>outstanding school fees</strong>.</p>
                <p>Please clear your balance at the bursar's office to view your performance report.</p>
                <a href="dashboard.php" class="btn-back"><i class="fa fa-arrow-left"></i> Return to Dashboard</a>
            </div>
        </div>
    </body>
    </html>
    <?php
    exit(); // STOP ALL FURTHER PROCESSING
}

// Fetch settings for the school name in sidebar
$stmtSet = $pdo->query("SELECT school_name FROM system_settings WHERE id = 1");
$settings = $stmtSet->fetch();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Check Results | <?= htmlspecialchars($settings['school_name'] ?? 'Portal') ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { 
            --primary: #3498db; 
            --dark: #1a252f; 
            --sidebar-bg: #1a252f;
            --bg: #f8f9fa; 
            --text-main: #2c3e50; 
            --white: #ffffff; 
            --shadow: 0 4px 12px rgba(0,0,0,0.08);
        }

        body { font-family: 'Inter', 'Segoe UI', sans-serif; margin: 0; background: var(--bg); display: flex; color: var(--text-main); min-height: 100vh; }
        
        /* Sidebar (Same as Dashboard) */
        .sidebar { width: 260px; background: var(--sidebar-bg); height: 100vh; position: fixed; color: white; transition: all 0.3s; z-index: 1000; left: 0; }
        .sidebar-header { padding: 30px 20px; text-align: center; border-bottom: 1px solid rgba(255,255,255,0.1); }
        .sidebar-logo { width: 60px; height: 60px; background: white; border-radius: 50%; padding: 5px; margin-bottom: 10px; }
        .sidebar-nav { padding: 20px 0; }
        .sidebar-nav a { display: flex; align-items: center; padding: 12px 25px; color: #bdc3c7; text-decoration: none; transition: 0.3s; font-size: 14px; border-left: 4px solid transparent; }
        .sidebar-nav a i { margin-right: 12px; width: 20px; text-align: center; }
        .sidebar-nav a:hover, .sidebar-nav a.active { background: rgba(255,255,255,0.05); color: white; border-left: 4px solid var(--primary); }

        /* Main Content */
        .main-content { margin-left: 260px; flex: 1; padding: 40px; display: flex; flex-direction: column; align-items: center; width: 100%; box-sizing: border-box; }
        
        /* Selection Card */
        .selection-card { 
            background: white; 
            padding: 40px; 
            border-radius: 16px; 
            box-shadow: var(--shadow); 
            width: 100%; 
            max-width: 450px; 
            margin-top: 50px;
        }

        h2 { text-align: center; color: var(--text-main); margin-bottom: 25px; font-size: 22px; }
        label { font-weight: 700; color: #34495e; font-size: 13px; text-transform: uppercase; display: block; margin-bottom: 8px; }
        select, button { width: 100%; padding: 14px; margin-bottom: 20px; border-radius: 8px; border: 1px solid #ddd; font-size: 16px; box-sizing: border-box; font-family: inherit; }
        
        button { background: var(--primary); color: white; border: none; cursor: pointer; font-weight: bold; transition: 0.3s; margin-top: 10px; }
        button:hover { background: #2980b9; transform: translateY(-1px); }

        .mobile-toggle { display: none; position: fixed; top: 20px; left: 20px; z-index: 1001; background: var(--primary); color: white; border: none; padding: 10px 15px; border-radius: 5px; cursor: pointer; }

        @media (max-width: 992px) {
            .sidebar { left: -260px; }
            .sidebar.active { left: 0; }
            .main-content { margin-left: 0; padding: 20px; }
            .mobile-toggle { display: block; }
            .selection-card { margin-top: 80px; padding: 30px 20px; }
        }
    </style>
</head>
<body>

    <button class="mobile-toggle" onclick="toggleSidebar()">
        <i class="fa fa-bars"></i>
    </button>

    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <img src="../assets/img/logo.png" class="sidebar-logo" onerror="this.src='https://ui-avatars.com/api/?name=S&background=3498db&color=fff'">
            <h2 style="color:var(--primary); font-size: 16px; margin:0;">STUDENT PORTAL</h2>
            <p style="font-size: 10px; color: #7f8c8d;"><?= htmlspecialchars($settings['school_name'] ?? 'Management System') ?></p>
        </div>
        <nav class="sidebar-nav">
            <a href="dashboard.php"><i class="fa fa-home"></i> Dashboard</a>
            <a href="results.php" class="active"><i class="fa fa-chart-line"></i> My Results</a>
            <a href="attendance.php"><i class="fa fa-calendar-check"></i> Attendance</a>
            <a href="profile.php"><i class="fa fa-user-cog"></i> Profile Settings</a>
            <a href="../auth/logout.php" style="color: #e74c3c;"><i class="fa fa-sign-out-alt"></i> Logout</a>
        </nav>
    </div>

    <div class="main-content">
        <div class="selection-card">
            <div style="text-align: center; margin-bottom: 20px;">
                <i class="fa fa-graduation-cap" style="font-size: 40px; color: var(--primary);"></i>
            </div>
            <h2>Check My Results</h2>
            <p style="text-align: center; color: #7f8c8d; font-size: 14px; margin-top: -15px; margin-bottom: 25px;">Select the criteria to view your performance.</p>
            
            <form action="verify_pin.php" method="GET">
                <label>Academic Session</label>
                <select name="session" required>
                    <option value="2024/2025">2024/2025</option>
                    <option value="2025/2026">2025/2026</option>
                </select>

                <label>Term</label>
                <select name="term" required>
                    <option value="First Term">First Term</option>
                    <option value="Second Term">Second Term</option>
                    <option value="Third Term">Third Term</option>
                </select>

                <button type="submit">Continue to PIN Verification</button>
                <a href="dashboard.php" style="display:block; text-align:center; text-decoration:none; color:#7f8c8d; font-size: 14px; margin-top: 10px;">
                    <i class="fa fa-arrow-left"></i> Cancel and Return
                </a>
            </form>
        </div>
    </div>

    <script>
        function toggleSidebar() {
            document.getElementById('sidebar').classList.toggle('active');
        }
        
        document.addEventListener('click', function(event) {
            const sidebar = document.getElementById('sidebar');
            const btn = document.querySelector('.mobile-toggle');
            if (window.innerWidth <= 992 && !sidebar.contains(event.target) && !btn.contains(event.target)) {
                sidebar.classList.remove('active');
            }
        });
    </script>
</body>
</html>