<?php require_once("includes/initialize.php");
 require_once("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");
// require('rotation.php');
// global $database;
date_default_timezone_set('Africa/Lagos');

//$id = $session->user_id;
// $id = $_GET['id'];
// $name = $_SESSION['names'];
// $name2 = $_SESSION['names2'];
// for ($i=0; $i<=sizeof($name); $i++)
// {
    
//   $formno =$name[$i];
//  $email =$name2[$i];
// }




$formno = $_GET['formno'];
$email = $_GET['email'];
//echo $formno = explode(',', $formno);
//echo $email = explode(',', $email);

// $biodata = Biodata::find_by_id($session->user_id);
// $user_data = Userdata::find_by_id($id);
// $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
$biodata = Biodata::find_by_id($formno, $email);
// echo $biodata->programme;
$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::find_by_id($session->user_id);
// 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';

// $output = $biodata->formno.' '.$biodata->surname .' '.$biodata->othernames.'  EXAM PASS';
// QRcode::png($output,$biodata->formno.'.png');

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(5);
        $this->SetFont('Times', 'BU', 14);
        $this->Cell(189, 20, 'STUDENT MID-TEST PASS', 0, 0, 'C');
        $this->Ln(20);
        
        
        // $this->SetFont('Arial','B',50);
        // $this->SetTextColor(255,192,203);
        // $this->RotatedText(35,190,'O S C O T E C H, E S A - O K E',45);
    }

    function Background()
    {
        // $this->Image('images/img1.png', 40, 80, 150, 150);
    }
    // function RotatedText($x, $y, $txt, $angle)
    // {
    //     //Text rotated around its origin
    //     $this->Rotate($angle,$x,$y);
    //     $this->Text($x,$y,$txt);
    //     $this->Rotate(0);
    // }

    function footer()
    {
        $this->SetY(-15);
        $this->SetFont('Times', '', 10);
        $this->Cell(189, 2, 'YOU WILL NOT BE ALLOWED INTO THE HALL WITHOUT THIS TEST 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, 'FORM NO: ' . trim(strtoupper($matricno)), 0, 0, 'C');
        // $this->Cell(189, 20, 'STUDENT TEST PASS', 0, 0, 'C');
        $this->Ln(10);
        $this->SetFont('Times', '', 10);
        $this->Cell(189, 10, date("j F, 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, "Lecturer'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', '', 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(5);
    }

    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');
// $pdf->Cell(70, 10, 'MATRIC No: ' . trim(strtoupper($matricno)), 0, 0, 'L');
$pdf->Cell(180, 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();
$pdf->SetFont('Times', 'B', 10);
// $pdf->Cell(10, 5, '--------------------------------', 0, 0, 'L');
$pdf->Cell(189, 5, '----------------------------------------------', 0, 1, 'C');
// $pdf->Cell(5, 3, "Student's Signature/Date", 0, 0, 'L');
$pdf->Cell(189, 3, "Head of Department/Signature and Date", 0, 0, 'C');
$pdf->Ln(20);
$pdf->Cell(189, 5, '---------------------------------------------------', 0, 1, 'C');
$pdf->Cell(189, 3, "Faculty Officer's Signature/Stamp and Date", 0, 0, 'C');
$pdf->Ln(20);

// $pdf->image($biodata->formno.'.png',90,230,30,30,'png');//QR-CODE
$pdf->Output();//}
?>