<?php 
ob_start();
session_start();
include ('../includes/connection.php'); 
$class_id = $_GET['Id_1'];
$subject_id = $_GET['Id_2'];
$year = $_GET['Id_3'];
$exam_id = $_GET['Id_4'];
 
$sql = "
  SELECT
    exam_id,
    class_id,
    subject_id,
    year_of_study,
    SUM(CASE WHEN grade = 'A' THEN 1 ELSE 0 END) AS A_count,
    SUM(CASE WHEN grade = 'B' THEN 1 ELSE 0 END) AS B_count,
    SUM(CASE WHEN grade = 'C' THEN 1 ELSE 0 END) AS C_count,
    SUM(CASE WHEN grade = 'D' THEN 1 ELSE 0 END) AS D_count,
    SUM(CASE WHEN grade = 'E' THEN 1 ELSE 0 END) AS E_count
  FROM marks_srms  WHERE subject_id = '$subject_id' AND exam_id = '$exam_id' AND class_id = '$class_id' AND year_of_study = '$year'
  GROUP BY exam_id, class_id, subject_id
  ORDER BY exam_id, class_id, subject_id
";

$result1 = mysqli_query($conn, $sql); 
  
 
$query = "
    SELECT 
        grade, COUNT(*) as total 
    FROM marks_srms 
    WHERE subject_id = '$subject_id' AND class_id = '$class_id' AND year_of_study = '$year'
    GROUP BY grade
";

$result = mysqli_query($conn, $query);

// Initialize all grades to 0
$grades = ['A'=>0, 'B'=>0, 'C'=>0, 'D'=>0, 'E'=>0, 'S'=>0];

while($row = mysqli_fetch_assoc($result)) {
    $grade = strtoupper($row['grade']); // ensure grade is uppercase
    if (array_key_exists($grade, $grades)) {
        $grades[$grade] = $row['total'];
    }
}    
$query= mysqli_query($conn,"SELECT * FROM exam_srms,class_srms where exam_srms.class_id=class_srms.class_id AND exam_srms.exam_id='$exam_id'")or die(mysqli_error());
$class1 = mysqli_fetch_array($query); 
 
 $html .= '<center><h3><b>THE UNITED REPUBLIC OF TANZANIA <br> MINISTRY OF SCIENCE AND TECHNOLOGY</b><br>SIKIRARI SCONDARY SCHOOL<br><u class="dotted-line">'.$class1['exam_name'].' EXAMINATION RESULT FOR '.$class1['class_name'].' - <b>'.$nam['year_of_study'].'</b></u></h3></center>'; 
  
 
$html = '';
$html .= '
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
            font-size: 12px;
        }
        th, td {
            border: 1px solid black;
            padding: 6px; 
        }
        .dotted-line {
            border-bottom: 1px dotted black;
            display: inline-block;
            padding-bottom: 2px;
        }
    </style>
';

$html .= '<center><h3><b>THE UNITED REPUBLIC OF TANZANIA <br> 
MINISTRY OF SCIENCE AND TECHNOLOGY</b><br>SIKIRARI SECONDARY SCHOOL<br>
<u class="dotted-line">'.$class1['exam_name'].' EXAMINATION REPORT FOR '.$class1['class_name'].' - <b>'.$year.'</b></u>
</h3></center>'; 

$html .= '<table>
            <thead>
              <tr>
                <th>Grade</th>
                <th>Total</th>
              </tr>
            </thead>
            <tbody>';

foreach($grades as $grade => $count){
    $html .= '<tr><td>'.$grade.'</td><td>'.$count.'</td></tr>';
}

$html .= '</tbody></table>';

 $html .= '<table>
    <thead>
      <tr>
        <th>Exam ID</th><th>Class ID</th><th>Subject ID</th>
        <th>A</th><th>B</th><th>C</th><th>D</th><th>E</th>
      </tr>
    </thead>
    <tbody>';
    while ($r = mysqli_fetch_assoc($result1)){
     $html .= ' <tr>  
        <td> '.$r['subject_id'] .'</td>
        <td>'.$r['A_count'] .'</td>
        <td>'.$r['B_count'] .'</td>
        <td>'.$r['C_count'] .'</td>
        <td>'.$r['D_count'] .'</td>
        <td>'.$r['E_count'] .'</td>
      </tr>';
    }
    $html .= '</tbody>
  </table>'; 

// Additional remarks table
$html .= '<br><table>
            <tr>
                <td colspan="6"><b><u>Class Teacher Comments</u></b><br><br><br></b><br><br><br></b><br><br><br></b></b><br><br><br></b></b><br><br><br></b></b><br><br></b></b><br><br><br></b><br><br><br></b><br><br><br></td>
            </tr>
            <tr>
                <td colspan="6">Signed: <br> Class Teacher Name: ____________________________________Sign:_______________ Head of School Signature ________________ </td>
            </tr>
          </table>';

$html .= '<p><b>ACADEMIC COMMENT:</b> _______________________________________________________________________________________<br>______________________________________________________________________________________</p>';

require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;

$dompdf = new Dompdf(); 
$dompdf->loadHtml($html); 
$dompdf->setPaper('A4', 'portrait'); 
$dompdf->render();
$dompdf->stream($class1['class_name'] . '.pdf');
?>