<?php 
ob_start();
session_start();
include ('../includes/connection.php');  
$id= $_GET['id'];  
$exam_id= $_GET['id_4'];  
$name = mysqli_query($conn,"SELECT student_srms.year_of_study,student_srms.p_mobile,student_srms.student_gender,student_srms.student_profile,marks_srms.exam_id,marks_srms.student_roll_no,marks_srms.class_id,student_srms.student_name,marks_srms.exam_id FROM `marks_srms`,`student_srms` WHERE marks_srms.student_roll_no=student_srms.student_roll_no AND marks_srms.student_roll_no = '$id' AND marks_srms.exam_id = '$exam_id'");
$nam = mysqli_fetch_array($name); 
$class_id=$nam['class_id']; 
$student_roll_no=$nam['student_roll_no'];
$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);
$class_id=$class1['class_id'];
 
 $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 .= ' <style>
    table, tr,th, td {
    border: 1px solid black;
    border-collapse: collapse;
    padding: 6px;
    width 200px; 
} 
 
</style>';  
 $remark = mysqli_query($conn,"SELECT marks,point FROM marks_srms WHERE exam_id='$exam_id' and class_id='$class_id' AND  student_roll_no = '$id';");
 $remark_g = mysqli_fetch_array($remark); 
 $t_marks=$remark_g['t_marks']; 
  $t_point=$remark_g['t_point'];

    
   // Fetch marks for the student
      $marks_query = "SELECT * FROM marks_srms WHERE exam_id='$exam_id' and class_id='$class_id' AND student_roll_no = '$student_roll_no' AND identity_sub !='BAM' AND identity_sub !='A-COM' AND identity_sub !='CIV' ORDER BY marks DESC LIMIT 3";
      $marks_result = $conn->query($marks_query); 

      $total_marks = 0;
      $total_grade_points = 0;

      while ($mark = $marks_result->fetch_assoc()) {
          $total_marks += $mark['marks'];
          $total_grade_points += $mark['point'];  
      }
   // Determine Division
      if ($total_grade_points >= 3 && $total_grade_points <= 9) {
        $division = "I";
        } elseif ($total_grade_points >= 10 && $total_grade_points <= 12) {
            $division = "II";
        } elseif ($total_grade_points >= 13 && $total_grade_points <= 17) {
            $division = "III";
        } elseif ($total_grade_points >= 18 && $total_grade_points <= 19) {
            $division = "IV";
        } else {
            $division = "0";
        } 
        $html .= '
            <table width="100%">
            <tr>  
            <td><b>NAME OF STUDENT</b></td>
            <td><b>CLASS</b></td> 
            <td><b>P-MOBILE</b></td> 
            <td><b>GENDER</b></td> 
            </tr>
            <tr>  
            <td>'.$nam['student_name'].'</td>
            <td>'.$class1['class_name'].'</td> 
            <td>'.$nam['p_mobile'].'</td> 
            <td>'.$nam['student_gender'].'</td> 
            </tr>   
          </table>  ';
          $html .= '
            <table width="100%"> 
            <tr>  
            <td>TOTAL MARKS :  <b style="color: orange;">'.$total_marks.' </b> </td>
            <td>TOTAL GRADE POINT: <b style="color: orange;"> '.$total_grade_points.' </b></td> 
            <td>DIVISION: <b style="color: orange;"> '.$division.'</b></td> 
            </tr> 
          </table> ';
$html .= ' <table width="100%">
            <thead>
              <tr>
                 <th>#</th>
                  <th>SUBJECT NAME</th>
                  <th>MARKS</th> 
                  <th>GRADE</th>
                  <th>POINT</th> 
                  <th>REMARK</th> 
              </tr>
            </thead>
            <tbody>';  
            $i = 1; 
            $selecte5_1 = mysqli_query($conn,"SELECT general_subject.sub_id,general_subject.subject_name,marks_srms.marks,marks_srms.grade,marks_srms.student_roll_no,marks_srms.class_id,marks_srms.pass_marks,marks_srms.subject_id,marks_srms.point,marks_srms.exam_id FROM general_subject,marks_srms WHERE general_subject.sub_id=marks_srms.subject_id AND marks_srms.class_id='$class_id' AND marks_srms.student_roll_no = '$id' AND marks_srms.exam_id = '$exam_id'");
             while($row = mysqli_fetch_array($selecte5_1)){  
              $pass_mark=$row['pass_marks'];  
        $html .= ' 
        <tr> 
          <td>'.$i++.' </td>
          <td>'.$row['subject_name'].'</td> 
          <td class="text-center" style="color: green;">'.$row['marks'].'</td> 
          <td class="text-center">'.$row['grade'].'</td> 
          <td class="text-center">'.$row['point'].'</td> 
          <td class="text-center">'.$row['pass_marks'].'</td>
        </tr>';     
             }             
 
                           
$html .= '                   
        <tr> 
          <td colspan="6">Class Teacher Remarks <br><br><br></td> 
        </tr>
        <tr> 
          <td colspan="6">Signed <br> Class Teacher_______________Head of school____________Parent_______________ 
          </td> 
  </tr>  
  </table> '; 
$html .= '<p><b >ACADEMIC COMMENT:</b> _______________________________________________________________________________________';

require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf(); 
$dompdf->loadHtml($html); 
$dompdf->setPaper('A4', 'portrait'); 
$dompdf->render();
$dompdf->stream(' '. $student_roll_no . '.pdf');
?>