<?php
include('includes/session1.php');
error_reporting();
include('includes/dbconnection.php');

require_once('vendor/autoload.php');

use Mpdf\Mpdf;
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$school_name = $_ENV['SCHOOL_NAME'];
$school_address = $_ENV['SCHOOL_ADDRESS'];


// Assuming the subject teacher is viewing results for a specific subject
$subject = $_GET['subject'];  // Get the subject from URL parameter
$class = $_GET['class'];  // Get the class from URL parameter
$section = $_GET['section'];

$session = $_GET['session'];  // Get the session from URL parameter
$term = $_GET['term'];  // Get the term from URL parameter

// Fetch current session and term
$sessionRow = mysqli_fetch_assoc(mysqli_query($con, "SELECT id, session_name FROM school_sessions WHERE id = '$session' ORDER BY id DESC LIMIT 1"));
$currentSessionId = $sessionRow['id'];
$currentSessionName = $sessionRow['session_name'];

$termRow = mysqli_fetch_assoc(mysqli_query($con, "SELECT id, term_name FROM school_terms WHERE id = '$term' AND session_id = '$currentSessionId' ORDER BY id DESC LIMIT 1"));
$currentTermId = $termRow['id'];
$currentTermName = $termRow['term_name'];



// Fetch results for this subject
$stmt = $con->prepare("
     SELECT s.studentName, s.studentno, r.test_score, r.test_score2, r.class_performance, r.weekly_assessment, r.exam_score, r.total_score
        FROM results r
        JOIN students s ON r.student_id = s.id
        WHERE r.class = ? AND r.section = ? AND r.subject = ? AND r.status = 'Approved' AND r.term_id = $currentTermId AND r.session_id = $currentSessionId
    ");
$stmt->bind_param("sss", $class, $section, $subject);
$stmt->execute();
$result_data = $stmt->get_result();


?>

<!-- HTML to display the results -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Score Sheet</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        @media print {
            body {
                -webkit-print-color-adjust: exact;
            }
        }
    </style>
</head>

<body>


    <div class="container mt-5">
        <div class='text-center'>
            <img src='company/logo.jpeg' alt='logo' width='70' height='70'><br>
            <h1><?= $school_name ?></h1>
            <b><i><?= $school_address ?></i></b><br>
            <hr>
            <h4 class='text-center'><?= $subject ?> Scoresheet: <?= $class ?> - <?= $section ?></h4>
        </div>
        <div class='text-center'>
            <strong>Term:</strong> <?= $currentTermName ?> || <strong>Session:</strong> <?= $currentSessionName ?>
        </div>
    </div>

    <div class="container">
        <div class="card mt-3">
            <div class="card-header bg-primary text-white">
                <h5><?= htmlspecialchars($subject); ?> - Results</h5>
            </div>
            <div class="card-body">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>Admission No.</th>
                            <th>Student Name</th>
                            <th>1st Test (10)</th>
                            <th>2nd Test (10)</th>
                            <th>Ex. & Projects (5)</th>
                            <th>Assignment (5)</th>
                            <th>Exam (70)</th>
                            <th>Total (100)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $result_data->fetch_assoc()): ?>
                            <tr>
                                <td><?= htmlspecialchars($row['studentno']); ?></td>
                                <td><?= htmlspecialchars($row['studentName']); ?></td>
                                <td><?= htmlspecialchars($row['test_score']); ?></td>
                                <td><?= htmlspecialchars($row['test_score2']); ?></td>
                                <td><?= htmlspecialchars($row['class_performance']); ?></td>
                                <td><?= htmlspecialchars($row['weekly_assessment']); ?></td>
                                <td><?= htmlspecialchars($row['exam_score']); ?></td>
                                <td><?= htmlspecialchars($row['total_score']); ?></td>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <script>
        window.print();
    </script>
</body>

</html>