<?php
session_name('super_admin_session');
if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');
session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);
session_start();
include('includes/dbconnection.php');

if (!isset($_SESSION['sid'])) {
    header('location:logout.php');
    exit();
}

$student_id = $_POST['student_id'] ?? '';
$session_id = $_POST['session_id'] ?? '';
$term_id = $_POST['term_id'] ?? '';
$result_type = $_POST['result_type'] ?? 'Endterm'; // default to Endterm

if (!$student_id || !$session_id || !$term_id) {
    echo "<p class='text-danger'>Missing data.</p>";
    exit();
}

// Decide which score fields to show based on result_type
$isMidterm = ($result_type === 'Midterm');

if ($isMidterm) {
    // For midterm
    $stmt = $con->prepare("
        SELECT subject, 
               test_score, test_score2,class_performance, weekly_assessment,
               test_absent, test2_absent, class_performance_absent, weekly_assessment_absent,
               (test_score + test_score2 + class_performance + weekly_assessment) AS total
        FROM result2 
        WHERE student_id = ? AND session_id = ? AND term_id = ?
          AND status_test = 'Submitted to Admin'
    ");
} else {
    // End of term
    $stmt = $con->prepare("
        SELECT subject, 
               test_score, test_score2, class_performance, weekly_assessment, exam_score, total_score,
               test_absent, test2_absent, class_performance_absent, weekly_assessment_absent, exam_absent
        FROM results 
        WHERE student_id = ? AND session_id = ? AND term_id = ?
          AND status = 'Submitted to Admin'
    ");
}

$stmt->bind_param("iii", $student_id, $session_id, $term_id);
$stmt->execute();
$result = $stmt->get_result();
$results = $result->fetch_all(MYSQLI_ASSOC);

if ($results) {
    echo '<table class="table table-bordered">';
    echo '<thead><tr>';
    echo '<th>Subjects</th>';

    if ($isMidterm) {
        echo '<th>1st C.A Test</th>';
        echo '<th>2nd C.A Test</th>';
        echo '<th>Class Ex. & Projects</th>';
    } else {
        echo '<th>C.A Test</th>';
        echo '<th>Class Ex. & Projects</th>';
    }

    echo '<th>Assignment</th>';

    if ($isMidterm) {
        echo '<th>Total (30)</th>';
    } else {
        echo '<th>Exam</th>';
        echo '<th>Total (100)</th>';
    }

    echo '</tr></thead><tbody>';

    foreach ($results as $row) {
        echo "<tr>";
        echo "<td>" . htmlspecialchars($row['subject']) . "</td>";

        if ($isMidterm) {
            // Test 1
            $test1_display = (!empty($row['test_absent'])) 
                ? '<span class="text-danger font-weight-bold">AB</span>' 
                : (int)$row['test_score'];
            $test1_value = (!empty($row['test_absent'])) ? 0 : (int)$row['test_score'];

            // Test 2
            $test2_display = (!empty($row['test2_absent'])) 
                ? '<span class="text-danger font-weight-bold">AB</span>' 
                : (int)$row['test_score2'];
            $test2_value = (!empty($row['test2_absent'])) ? 0 : (int)$row['test_score2'];

            // Class Performance (5 marks in midterm)
            $class_perf_display = (!empty($row['class_performance_absent'])) 
                ? '<span class="text-danger font-weight-bold">AB</span>' 
                : (int)$row['class_performance'];
            $class_perf_value = (!empty($row['class_performance_absent'])) ? 0 : (int)$row['class_performance'];

            echo "<td>$test1_display</td>";
            echo "<td>$test2_display</td>";
            echo "<td>$class_perf_display</td>";
        } else {
            // Single C.A Test (10 marks in end of term)
            $test1_display = (!empty($row['test_absent'])) 
                ? '<span class="text-danger font-weight-bold">AB</span>' 
                : (int)$row['test_score'];
            $test1_value = (!empty($row['test_absent'])) ? 0 : (int)$row['test_score'];

            // Class Ex. & Projects (15 marks in end of term)
            $class_perf_display = (!empty($row['class_performance_absent'])) 
                ? '<span class="text-danger font-weight-bold">AB</span>' 
                : (int)$row['class_performance'];
            $class_perf_value = (!empty($row['class_performance_absent'])) ? 0 : (int)$row['class_performance'];

            echo "<td>$test1_display</td>";
            echo "<td>$class_perf_display</td>";
        }

        // Assignment (common in both)
        $weekly_assess_display = (!empty($row['weekly_assessment_absent'])) 
            ? '<span class="text-danger font-weight-bold">AB</span>' 
            : (int)$row['weekly_assessment'];
        $weekly_assess_value = (!empty($row['weekly_assessment_absent'])) ? 0 : (int)$row['weekly_assessment'];

        echo "<td>$weekly_assess_display</td>";

        if ($isMidterm) {
            // Total = C.A1 + C.A2 + Class Perf + Assignment
            $total = $test1_value + $test2_value + $class_perf_value + $weekly_assess_value;
            echo "<td>" . ($total > 0 ? $total : '') . "</td>";
        } else {
            // Exam
            $exam_display = (!empty($row['exam_absent'])) 
                ? '<span class="text-danger font-weight-bold">AB</span>' 
                : (int)$row['exam_score'];
            $exam_value = (!empty($row['exam_absent'])) ? 0 : (int)$row['exam_score'];

            echo "<td>$exam_display</td>";

            // Total = CA + Class Perf + Assignment + Exam
            $total = $test1_value + $class_perf_value + $weekly_assess_value + $exam_value;
            echo "<td>" . ($total > 0 ? $total : '') . "</td>";
        }

        echo "</tr>";
    }

    echo '</tbody></table>';
} else {
    echo '<p class="text-danger">No results available.</p>';
}

?>
