<?php
// admin/view_submissions.php
session_start();
$projectRoot = dirname(__DIR__);
require_once $projectRoot . '/config/database.php';
require_once $projectRoot . '/config/config.php';

// --- UPDATED AUTH CHECK ---
// Grant access to both Admin and Sub-Admin
if (!isset($_SESSION['role']) || !in_array($_SESSION['role'], ['admin', 'subadmin'])) {
    header("Location: ../auth/login.php");
    exit();
}

try {
    $query = "SELECT 
                r.class_id, 
                c.class_name, 
                r.subject_name, 
                r.session, 
                r.term, 
                r.is_approved,
                COUNT(r.id) as record_count
              FROM results r
              LEFT JOIN classes c ON r.class_id = c.id
              GROUP BY r.class_id, r.subject_name, r.session, r.term, r.is_approved
              ORDER BY r.session DESC, r.class_id ASC";
    
    $submissions = $pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Submissions | <?php echo ucfirst($_SESSION['role']); ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #1a252f; --accent: #3498db; --bg: #f0f2f5; --success: #27ae60; --warning: #f39c12; }
        
        body { font-family: 'Inter', 'Segoe UI', sans-serif; background: var(--bg); margin: 0; padding: 15px; }
        .container { background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); max-width: 1100px; margin: auto; }
        
        .header { display: flex; justify-content: space-between; align-items: center; border-bottom: 2px solid #f1f1f1; padding-bottom: 20px; margin-bottom: 20px; flex-wrap: wrap; gap: 15px; }
        
        table { width: 100%; border-collapse: collapse; }
        th { background: var(--primary); color: white; padding: 12px; text-align: left; font-size: 14px; text-transform: uppercase; letter-spacing: 0.5px; }
        td { padding: 15px 12px; border-bottom: 1px solid #eee; font-size: 14px; }
        
        .badge { padding: 6px 12px; border-radius: 20px; font-size: 11px; font-weight: 700; text-transform: uppercase; display: inline-block; }
        .status-pending { background: #fff3cd; color: #856404; border: 1px solid #ffeeba; }
        .status-approved { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        
        .btn-action { text-decoration: none; background: var(--accent); color: white; padding: 8px 16px; border-radius: 6px; font-size: 13px; font-weight: 600; transition: 0.2s; display: inline-flex; align-items: center; gap: 6px; }
        .btn-action:hover { background: #2980b9; transform: translateY(-1px); }
        .btn-back { text-decoration: none; color: #7f8c8d; font-weight: bold; font-size: 14px; }

        /* --- RESPONSIVE CSS --- */
        @media screen and (max-width: 768px) {
            .container { padding: 15px; }
            .header { flex-direction: column; text-align: center; }
            
            table, thead, tbody, th, td, tr { display: block; }
            thead tr { position: absolute; top: -9999px; left: -9999px; }
            tr { border: 1px solid #ddd; border-radius: 10px; margin-bottom: 15px; background: #fff; padding: 5px; }
            td { border: none; border-bottom: 1px solid #f8f9fa; position: relative; padding-left: 50%; text-align: right; }
            td:last-child { border-bottom: none; }
            
            /* Use the data-label for mobile headers */
            td::before {
                content: attr(data-label);
                position: absolute;
                left: 15px;
                width: 45%;
                padding-right: 10px;
                white-space: nowrap;
                text-align: left;
                font-weight: 700;
                color: var(--primary);
            }
            .btn-action { width: 100%; justify-content: center; box-sizing: border-box; }
        }
    </style>
</head>
<body>

<div class="container">
    <div class="header">
        <div>
            <h1 style="margin:0; font-size: 24px;">Score Submissions</h1>
            <p style="color: #7f8c8d; margin: 5px 0 0 0;">Role: <strong><?= strtoupper($_SESSION['role']) ?></strong></p>
        </div>
        <a href="dashboard.php" class="btn-back"><i class="fa fa-arrow-left"></i> Dashboard</a>
    </div>

    <table>
        <thead>
            <tr>
                <th>Session/Term</th>
                <th>Class</th>
                <th>Subject</th>
                <th>Students</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php if (count($submissions) > 0): ?>
                <?php foreach ($submissions as $row): ?>
                <tr>
                    <td data-label="Session/Term"><?= htmlspecialchars($row['session']) ?> <br><small><?= htmlspecialchars($row['term']) ?></small></td>
                    <td data-label="Class"><?= htmlspecialchars($row['class_name']) ?></td>
                    <td data-label="Subject"><strong><?= htmlspecialchars($row['subject_name']) ?></strong></td>
                    <td data-label="Students"><?= $row['record_count'] ?> records</td>
                    <td data-label="Status">
                        <?php if ($row['is_approved']): ?>
                            <span class="badge status-approved"><i class="fa fa-check-circle"></i> Approved</span>
                        <?php else: ?>
                            <span class="badge status-pending"><i class="fa fa-clock"></i> Pending</span>
                        <?php endif; ?>
                    </td>
                    <td data-label="Action">
                        <a href="approve_results.php?class_id=<?= $row['class_id'] ?>&session=<?= urlencode($row['session']) ?>&term=<?= urlencode($row['term']) ?>&subject=<?= urlencode($row['subject_name']) ?>" class="btn-action">
                            <i class="fa fa-tasks"></i> Manage
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="6" style="text-align: center; padding: 60px; color: #95a5a6;">
                        <i class="fa fa-folder-open fa-3x" style="display:block; margin-bottom:15px; color:#bdc3c7;"></i>
                        No scores have been submitted yet.
                    </td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

</body>
</html>