<?php
// admin/results/index.php
session_start();
$projectRoot = dirname(dirname(__DIR__)); 
require_once $projectRoot . "/config/database.php";

// Security Check: Allow both admin and subadmin
if (!isset($_SESSION['role']) || !in_array($_SESSION['role'], ['admin', 'subadmin'])) {
    header("Location: ../../auth/login.php");
    exit();
}

// Fetch Classes for the dropdown
$classes = $pdo->query("SELECT * FROM classes ORDER BY class_name ASC")->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin - View Results</title>
    <link rel="stylesheet" href="../../assets/css/admin_style.css"> 
    <style>
        body { background: #f0f2f5; font-family: 'Segoe UI', sans-serif; margin: 0; padding: 20px; }
        
        .selection-card { 
            background: white; 
            max-width: 550px; 
            margin: 50px auto; 
            padding: 30px; 
            border-radius: 8px; 
            box-shadow: 0 4px 15px rgba(0,0,0,0.1); 
            border-top: 5px solid #2c3e50; 
            box-sizing: border-box;
        }
        
        .form-group { margin-bottom: 20px; }
        .form-group label { display: block; margin-bottom: 8px; font-weight: bold; color: #555; }
        .form-control { width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; box-sizing: border-box; }
        
        .page-title { text-align: center; color: #2c3e50; margin-bottom: 10px; }
        
        /* Button Styling - Exact same as provided */
        .button-group { 
            display: flex; 
            gap: 15px; 
            margin-top: 25px; 
        }
        
        .btn-view { 
            flex: 1; 
            background: #2c3e50; 
            color: white; 
            padding: 14px; 
            border: none; 
            border-radius: 4px; 
            font-size: 14px; 
            font-weight: bold; 
            cursor: pointer; 
            transition: 0.3s; 
            text-align: center;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-view:hover { background: #1a252f; transform: translateY(-2px); }
        .btn-bulk { background: #34495e; }
        .btn-bulk:hover { background: #2c3e50; }

        .back-container { text-align: center; margin-top: 20px; }
        .btn-back { color: #7f8c8d; text-decoration: none; font-size: 14px; font-weight: 600; }
        .btn-back:hover { color: #2c3e50; text-decoration: underline; }

        /* MOBILE RESPONSIVE FIX */
        @media (max-width: 600px) {
            .selection-card { margin: 20px auto; padding: 20px; }
            .button-group { flex-direction: column; }
            .btn-view { width: 100%; }
        }
    </style>
</head>
<body>

<div class="selection-card">
    <h2 class="page-title">View Academic Results</h2>
    <p style="text-align:center; color:#7f8c8d; margin-top:0;">Select parameters to generate reports</p>
    <hr style="border:0; border-top:1px solid #eee; margin-bottom:25px;">

    <form method="GET">
        <div class="form-group">
            <label>Academic Session</label>
            <select name="session" class="form-control" required>
                <option value="">-- Select Session --</option>
                <option value="2023/2024">2023/2024</option>
                <option value="2024/2025">2024/2025</option>
                <option value="2025/2026">2025/2026</option>
            </select>
        </div>

        <div class="form-group">
            <label>Term</label>
            <select name="term" class="form-control" required>
                <option value="">-- Select Term --</option>
                <option value="First Term">First Term</option>
                <option value="Second Term">Second Term</option>
                <option value="Third Term">Third Term</option>
            </select>
        </div>

        <div class="form-group">
            <label>Class</label>
            <select name="class_id" class="form-control" required>
                <option value="">-- Select Class --</option>
                <?php foreach($classes as $c): ?>
                    <option value="<?= $c['id'] ?>"><?= htmlspecialchars($c['class_name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="button-group">
            <button type="submit" 
                    onclick="this.form.action='broadsheet.php'" 
                    class="btn-view">
                📊 Broad Sheet
            </button>

            <button type="submit" 
                    name="action" 
                    value="bulk" 
                    onclick="this.form.action='bulk_view.php'" 
                    class="btn-view btn-bulk">
                📄 All Report Cards
            </button>
        </div>
    </form>

    <div class="back-container">
        <a href="../dashboard.php" class="btn-back">⬅ Back to Admin Dashboard</a>
    </div>
</div>

</body>
</html>