<?php require_once("includes/initialize.php");
require_once("../student_portal/includes/user_data.php");
 require_once("includes/session_class.php");
 require_once("includes/department_class.php");
 require_once("includes/course_class.php");
require_once("../student_portal/phpqrcode/qrlib.php");
// global $database;
date_default_timezone_set('Africa/Lagos');

$formno = $_GET['formno'];
$email = $_GET['email'];

$id = $session->user_id;
// $biodata = Biodata::find_by_id($session->user_id);
// $user_data = Userdata::find_by_id($session->user_id);
$biodata = Biodata::find_by_id($formno, $email);
$session_class = SESSIONCLASS::session_authenticate();
$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
$display_course = COURSECLASS::fetch_all_course($biodata->courses, $biodata->programme, $session_class->semester);

$user_data = Userdata::mat($formno, $email);
foreach ($user_data as $mat)
{
    $MatNo = $mat->matricno;
}
// if ($user_data->std_process_id < 5) {
//     $level = $user_data->std_process_id + 1;
//     $update_process = Userdata::update($id, $level);
// }
$level = $biodata->programme;
$department = $biodata->courses;
$pix = substr($biodata->pix, 11);
// echo $level = $biodata->programme;
$semester = $session_class->semester;



$matricno = $biodata->formno;
$ses_sem_id =$session_class->ses_sem_id;
$surname = $biodata->surname;
$othernames = $biodata->othernames;
$session = $session_class->session;
$level = $biodata->programme;
$level = $level . " " . "I";


// $output = $transact_feedback->txt_ref.'- '.$transact_feedback->formno.' '.$transact_feedback->surname .' '.$transact_feedback->othernames.'  Acceptance Fees: PAID';


class coursePDF extends FPDF
{
    public $matricno;
    public $department;
    public $semester;
    public $level;
    public $ses_sem_id;
    
    function Head($department)
    {
// 		$this->department = trim(strtoupper($department));
        $this->Image('../student_portal/images/img1.png', 5, 5);
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(189, 5, 'OSUN STATE COLLEGE OF TECHNOLOGY, ESA-OKE.', 0, 0, 'C');
        $this->Ln();
        $this->SetFont('Arial', 'B', 10);
        $this->Cell(189, 8, 'DEPARTMENT OF ' . trim(strtoupper($department)), 0, 0, 'C');
        // $this->Cell(189,8, $department,0,0,'L');
        $this->Ln();
        $this->SetFont('Times', '', 8);
        $this->Cell(189, 5, 'P.M.B 1011, ESA-OKE, OSUN STATE.', 0, 0, 'C');
        $this->Ln(20);
    }

    function Background()
    {
        $this->Image('images/img1.png', 40, 80, 150, 150);
    }

    function footer()
    {
        $this->SetY(-15);
        $this->SetFont('Times', '', 10);
        $this->Cell(189, 2, 'YOU WILL NOT BE ALLOWED INTO THE EXAMINATION HALL WITHOUT THIS EXAM PASS.', 0, 1, 'C');
        $this->Ln();
        $this->SetFont('Times', '', 8);
        $this->Cell(189, 2, strtoupper('Directorate of ICT, Esa-Oke Copyright 2018. All Rights Reserved.'), 0, 0, 'C');
        $this->Ln();
        $this->SetFont('Arial', '', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

    function headerTable($database, $matricno, $level, $ses_sem_id, $department)
    {

        if ($this->level == "ND II") {
            $qry = "SELECT courses,regDate FROM ndii_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$ses_sem_id'
			";
        } elseif ($level == "ND I") {
            $qry = "SELECT courses, regDate FROM ndi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$ses_sem_id'
			";
        } elseif ($level == "HND I") {
            $qry = "SELECT courses, regDate FROM hndi_tbl 
			WHERE matricNo ='$matricno' and ses_sem_id = '$ses_sem_id'
			";
        } elseif ($level == "HND II") {
            $qry = "SELECT courses, regDate FROM hndii_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$ses_sem_id'
			";
        }

        $result = mysql_query($qry);
        //$result = mysql_query($qry);
        $count = 0;
        while ($row = mysql_fetch_array($result)) {
            //while ($row= mysql_fetch_array($result)){
            $code = $row{"courses"};
            $date = $row{"regDate"};
        }
        $RegCourses = explode(',', $code);
        $this->SetFont('Times', 'BU', 14);
        $this->Cell(189, 20, "STUDENT'S EXAMINATION PASS", 0, 0, 'C');
        $this->Ln(10);
        $this->SetFont('Times', '', 10);
        $this->Cell(189, 10, date("F j, Y  |g:i a"), 0, 1, 'R');
        $this->SetFont('Times', 'B', 10);
        $this->SetFillColor(150, 255, 150);
        $this->Cell(9, 10, 'S/N', 1, 0, 'C', true);
        $this->Cell(32, 10, "Course Codes/Units", 1, 0, "C", true);
        $this->Cell(98, 10, "Course Titles", 1, 0, "C", true);
        $this->Cell(50, 10, "Invigilator's Signature/Date", 1, 1, "C", true);
        $total = 0;
        foreach ($RegCourses as $course_code) {

            $query = "SELECT * FROM tbl_courses 
			WHERE course_code = '$course_code' AND department = '$department'
			";
            $sql = mysql_query($query);
            //while($rows = mysqli_fetch_array($sql,MYSQLI_ASSOC))
            while ($rows = mysql_fetch_array($sql)) {
                $count += 1;
                $code = $rows["course_code"];
                $title = $rows["course_title"];
                $unit = $rows["course_unit"];
                $total = $total + $unit;

                $this->SetFont('Times', 'B', 8);
                $this->Cell(9, 10, $count, 1, 0, 'C');
                $this->Cell(32, 10, $code." / ".$unit, 1, 0,'C');
                $this->Cell(98, 10, $title, 1, 0);
                $this->Cell(50, 10, "", 1, 1, 'C');

            }
        }
        $this->SetFont('Times', 'B', 10);
        $this->Cell(90, 10, 'Total Units:' . $total, 0, 0, 'L');
        $this->Ln(10);
    }

    function body()
    {
        $this->SetFont('Times', 'B', 12);
        $this->SetFillColor(255, 0, 0);
        $this->Cell(276, 5, 'Welcome $surname', 0, 1);
        $this->Cell(50, 5, $matricno, 0, 0, "C");
        $this->Cell(50, 5, $smester, 0, 0, "C");
        $this->Cell(50, 5, $session, 0, 0, "C");
        $this->Ln(20);
    }
}


// $pdf = new coursePDF('L','mm','A4');
$pdf = new coursePDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Head($department);

// $pdf->Background();
$pdf->SetFont('Times', 'BU', 10);
$pdf->SetFillColor(255, 0, 0);
$pdf->Cell(80, 10, 'NAME: ' . trim(strtoupper($surname )). ', ' . trim(strtoupper($othernames)), 0, 0, 'L');
$pdf->Cell(110, 10, trim(strtoupper($semester)) . '  SEMESTER:  ' . $level, 0, 1, 'R');

if ($MatNo!=0){
    $formno=$MatNo;
}else {
$formno = $biodata->formno;
}
$pdf->Cell(70, 10, 'MATRIC No: ' . trim(strtoupper($formno)), 0, 0, 'L');
$pdf->Cell(120, 10, 'Session:' . $session, 0, 0, 'R');
$pdf->Ln(1);
$pdf->Image('../Oscotech-Wallet/passport/' . $pix, 170, 5,30,30);

$pdf->Ln(1);
$pdf->headerTable($database, $matricno, $level, $ses_sem_id, $department);
$pdf->Ln(20);
$pdf->SetFont('Times', 'B', 10);
$pdf->Cell(10, 5, '--------------------------------', 0, 0, 'L');
$pdf->Cell(170, 5, '-------------------------------', 0, 1, 'R');
$pdf->Cell(5, 3, "Student's Signature/Date", 0, 0, 'L');
// $pdf->Cell(175, 3, "Departmental Exam Officer/Date", 0, 0, 'R');
$pdf->Cell(175, 3, "Head of Department/Date", 0, 0, 'R');
$pdf->Ln(20);
$output = $formno.' '.$biodata->surname .' '.$biodata->othernames.'  EXAM PASS';
QRcode::png($output,$formno.'.png');

$pdf->image($formno.'.png',90,230,30,30,'png');
$pdf->Output();
?>