<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require('../fpdf186/fpdf.php');
require_once '../models/db_connection.php';
// require_once '../models/student_model.php';
// require_once '../models/exam_model.php';
// $exam = new Exam($conn);
// $student = new Student($conn);
// if(isset($_GET['id'])){
// $studentRes = $exam->generateResults($_GET['id']);
// $studentDat = $student->getSingleStudent($_GET['id']);
// $studentDat = $studentDat[0];
    
// }

// var_dump($studentRes);
// die('atleast i have reached');

// die();
class PDF extends FPDF
{
    // Page header
    function Header()
    {
        // Logo and company name
        $this->SetFont('Arial','B',14);
        $this->Cell(0,10,'KIMARA VOCATIONAL TRAINING ',0,1,'C');
        $this->SetFont('Arial','B',12);
        $this->Cell(0,10,'Exam Results ',0,1,'C');
        
        // Report date
        $this->SetFont('Arial','',10);
        $this->Cell(0,10,'Report Date: '.date('Y-m-d'),0,1,'C');
        
        // Line break
        $this->Ln(10);
    }

    // Page footer
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }

    // Load data
    function LoadData($file)
    {
        // Read file lines
        $lines = file($file);
        $data = array();
        foreach($lines as $line)
            $data[] = explode(',',trim($line));
        return $data;
    }

    // Table
    function ImprovedTable($header, $data)
    {
        // Column widths
        $w = array(8, 54, 55, 15, 15,15,23);
        // Header
        for($i=0;$i<count($header);$i++)
            $this->Cell($w[$i],7,$header[$i],1,0,'C');
        $this->Ln();
        // Data
        $x = 0;
        foreach($data as $row)
        {
            $this->Cell($w[0],6,++$x,'LR');
            $this->Cell($w[1],6,$row[0],'LR');
            $this->Cell($w[2],6,$row[1],'LR');
            $this->Cell($w[3],6,$row[2],'LR',0,'R');
            $this->Cell($w[4],6,$row[3],'LR',0,'R');
            $this->Cell($w[5],6,number_format($row[4]),'LR',0,'R');
            $this->Cell($w[6],6,$row[5],'LR',0,'R');
            $this->Ln();
        }
        // Closing line
        $this->Cell(array_sum($w),0,'','T');
    }
}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',10);




// Table data
$header = array('#', 'Enrollement ID', 'Fullname', 'Theory', 'Practical', 'Average', 'Remarks');
$remarks = "none";
if (isset($_GET['id'])) {
    $getstudent = mysqli_query($conn, "SELECT * FROM students,student_exams WHERE students.student_id = student_exams.student AND student_exams.id = $_GET[id]");
    $fetch = mysqli_fetch_array($getstudent);
    $average = (($fetch['practical'] + $fetch['theory']) / 200) * 100;

if($average >= 70 ){
    $remarks = 'Passed';
}else{
    $remarks="Failed";
}
}
$data = array(  
    array($fetch['en_id'],$fetch['fullname'],$fetch['theory'],$fetch['practical'],$average ,$remarks),
);


// Print table
$pdf->ImprovedTable($header, $data);

// Output PDF
$pdf->Output();
?>
