<?php
session_name('teacher_session');
session_start();
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'];

$section = isset($_GET['section']) ? filter_var($_GET['section'], FILTER_SANITIZE_STRING) : '';
$class = isset($_GET['class']) ? filter_var($_GET['class'], FILTER_SANITIZE_STRING) : '';
$subject = isset($_GET['subject']) ? filter_var($_GET['subject'], FILTER_SANITIZE_STRING) : '';
$session = isset($_GET['session']) ? filter_var($_GET['session'], FILTER_SANITIZE_STRING) : '';
$term = isset($_GET['term']) ? filter_var($_GET['term'], FILTER_SANITIZE_STRING) : '';




// 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 Content
$html = "
<style>
    table { width: 100%; border-collapse: collapse; font-size: 12px; }
    th, td { padding: 8px; text-align: left; border: 1px solid #ddd; }
    th { background-color: #007bff; color: white; }
    tr:nth-child(even) { background-color: #f9f9f9; }
    .text-center { text-align: center; }
</style>
<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>
    <strong> $subject Scoresheet: $class - $section</strong>
</div>
<div class='text-center'>
    <strong>Term:</strong> $currentTermName  || <strong>Session:</strong> $currentSessionName 
</div>
<hr>";
    $html .= "<div class='card mt-3'>
                <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>Class Ex. & Projects (5)</th>
                            <th>Assignment (5)</th>
                            <th>Exam (70)</th>
                            <th>Total (100)</th>
                        </tr>
                        </thead>
                        <tbody>";
   
                while ($row = $result_data->fetch_assoc()) {
                $html .= '<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>';
            }   
    $html .= "</tbody></table></div></div>";


// Generate PDF
$mpdf = new Mpdf();
$mpdf->SetHeader($school_name . '||'.$subject . '-'.'Scoresheet');
$mpdf->SetFooter('Generated on ' . date("M d, Y H:i A") . '||Page {PAGENO}');
$mpdf->WriteHTML($html);
$mpdf->Output($subject.'_'.'Scoresheet'.'_'. $class.'_'. $section . '.pdf', 'D');
?>
