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

if (session_status() === PHP_SESSION_NONE) { session_start(); }

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

$success = "";

// Handle Approval Action
if (isset($_GET['approve_class']) && isset($_GET['subject_id'])) {
    $c_id = $_GET['approve_class'];
    $s_id = $_GET['subject_id'];
    
    // Updated to set both status and is_approved for broadsheet compatibility
    $stmt = $pdo->prepare("UPDATE results SET status = 'approved', is_approved = 1 
                           WHERE class_id = ? AND subject_id = ? AND term = ? AND session = ?");
    $stmt->execute([$c_id, $s_id, $current_term, $current_session]);
    $success = "Results for the selected subject have been published!";
}

// Fetch all subjects that have scores waiting for approval
$sql = "SELECT DISTINCT r.class_id, r.subject_id, c.class_name, s.subject_name, r.status
        FROM results r
        JOIN classes c ON r.class_id = c.id
        JOIN subjects s ON r.subject_id = s.id
        WHERE r.term = ? AND r.session = ?
        ORDER BY c.class_name ASC, s.subject_name ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute([$current_term, $current_session]);
$pending_results = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Approve Results | <?php echo $school_name; ?></title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f4f7f6; padding: 20px; margin: 0; }
        .container { background: white; padding: 30px; border-radius: 12px; max-width: 1000px; margin: auto; box-shadow: 0 10px 25px rgba(0,0,0,0.05); }
        
        h2 { color: #2c3e50; margin-top: 0; }
        .back-link { text-decoration: none; color: #3498db; font-weight: bold; display: inline-block; margin-bottom: 20px; font-size: 14px; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { padding: 15px; text-align: left; border-bottom: 1px solid #edf2f7; }
        th { background-color: #2c3e50; color: white; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
        
        .btn-approve { background: #27ae60; color: white; padding: 8px 16px; text-decoration: none; border-radius: 6px; font-size: 13px; font-weight: bold; transition: 0.3s; display: inline-block; }
        .btn-approve:hover { background: #219150; transform: translateY(-1px); }
        
        .status-badge { padding: 5px 10px; border-radius: 20px; font-size: 11px; font-weight: bold; display: inline-block; }
        .pending { background: #fff3cd; color: #856404; }
        .approved { background: #d4edda; color: #155724; }

        .alert-success { background: #d4edda; color: #155724; padding: 15px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #c3e6cb; }

        /* MOBILE RESPONSIVE TABLE */
        @media (max-width: 768px) {
            .container { padding: 15px; }
            table, thead, tbody, th, td, tr { display: block; }
            thead tr { position: absolute; top: -9999px; left: -9999px; }
            tr { border: 1px solid #e2e8f0; margin-bottom: 15px; border-radius: 10px; padding: 10px; background: #fff; }
            td { border: none; position: relative; padding-left: 50%; text-align: right; font-size: 14px; }
            td:before { 
                position: absolute; left: 15px; width: 45%; padding-right: 10px; 
                white-space: nowrap; text-align: left; font-weight: bold; color: #718096;
                content: attr(data-label); 
            }
            .btn-approve { width: 100%; text-align: center; box-sizing: border-box; }
        }
    </style>
</head>
<body>

<div class="container">
    <a href="../dashboard.php" class="back-link">← Back to Dashboard</a>
    
    <div style="display: flex; justify-content: space-between; align-items: flex-start; flex-wrap: wrap;">
        <div>
            <h2>Result Approval Panel</h2>
            <p style="color: #718096;">Reviewing scores for: <strong><?php echo $current_term; ?> (<?php echo $current_session; ?>)</strong></p>
        </div>
        <div style="background: #edf2f7; padding: 8px 15px; border-radius: 6px; font-size: 12px; font-weight: bold; color: #4a5568;">
            PORTAL: <?php echo strtoupper($_SESSION['role']); ?>
        </div>
    </div>

    <?php if($success): ?>
        <div class="alert-success">
            <i class="fa fa-check-circle"></i> <?php echo $success; ?>
        </div>
    <?php endif; ?>

    <table>
        <thead>
            <tr>
                <th>Class</th>
                <th>Subject</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($pending_results as $row): ?>
            <tr>
                <td data-label="Class"><strong><?php echo $row['class_name']; ?></strong></td>
                <td data-label="Subject"><?php echo $row['subject_name']; ?></td>
                <td data-label="Status">
                    <span class="status-badge <?php echo $row['status']; ?>">
                        <?php echo strtoupper($row['status']); ?>
                    </span>
                </td>
                <td data-label="Action">
                    <?php if($row['status'] == 'pending'): ?>
                        <a href="?approve_class=<?php echo $row['class_id']; ?>&subject_id=<?php echo $row['subject_id']; ?>" 
                           class="btn-approve" 
                           onclick="return confirm('Are you sure you want to publish these results to students?')">
                           Approve & Publish
                        </a>
                    <?php else: ?>
                        <span style="color: #27ae60; font-weight: bold;">
                            <i class="fa fa-check-double"></i> Published
                        </span>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            
            <?php if(count($pending_results) == 0): ?>
                <tr>
                    <td colspan="4" style="text-align:center; padding: 40px; color: #a0aec0;">
                        No results have been uploaded yet for this term.
                    </td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

</body>
</html>