<?php require_once("./includes/initialize.php");
global $database;
date_default_timezone_set('Africa/Lagos');

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

// $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 = '';


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('images/icon.png', 10, 15);
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(189, 5, 'OSUN STATE COLLEGE OF TECHNOLOGY, ESA-OKE.', 0, 0, 'C');
        $this->Ln();
        $this->SetFont('Arial', 'B', 12);
        $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', '', 12);
        $this->Cell(189, 5, 'P.M.B 1011, ESA-OKE, OSUN STATE.', 0, 0, 'C');
        $this->Ln(20);
    }

    function Background()
    {
        $this->Image('images/img2.png', 0, 0, 210, 300);
    }

    function footer()
    {
        $this->SetY(-15);
        $this->SetFont('Times', '', 10);
        $this->Cell(189, 2, '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") {
            $table = 'ndii_tbl';
            $reg = ONLINEPAY:: reg_students($table,$department,$id);
        } elseif ($level == "ND I") {
            $table = 'ndii_tbl';
            $reg = ONLINEPAY:: reg_students($table,$department,$id);
        } elseif ($level == "HND I") {
            $table = 'ndii_tbl';
            $reg = ONLINEPAY:: reg_students($table,$department,$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);
        $count = 0;
        foreach($reg as $pay)
        // while ($row = mysql_fetch_array($result)) {
        {
            $code = $row{"courses"};
            $date = $row{"regDate"};
        }
        $RegCourses = explode(',', $code);
        $this->SetFont('Times', 'BU', 14);
        $this->Cell(189, 20, 'COURSE REGISTRATION FORM', 0, 0, 'C');
        $this->Ln(10);
        $this->SetFont('Times', '', 12);
        $this->Cell(189, 10, 'Reg.Date: ' . $date, 0, 1, 'R');
        $this->SetFont('Times', 'B', 12);
        $this->SetFillColor(150, 255, 150);
        $this->Cell(20, 10, 'S/N', 1, 0, 'C', true);
        $this->Cell(30, 10, "Course Codes", 1, 0, "C", true);
        $this->Cell(120, 10, "Course Titles", 1, 0, "C", true);
        $this->Cell(19, 10, "Units", 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', '', 12);
                $this->Cell(20, 10, $count, 1, 0, 'C');
                $this->Cell(30, 10, $code, 1, 0);
                $this->Cell(120, 10, $title, 1, 0);
                $this->Cell(19, 10, $unit, 1, 1, 'C');

            }
        }
        $this->SetFont('Times', 'B', 14);
        $this->Cell(170, 10, 'Total Units:' . $total, 0, 0, 'R');
        $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();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Head($department);

$pdf->Background();
$pdf->SetFont('Times', 'BU', 12);
$pdf->SetFillColor(255, 0, 0);
$pdf->Cell(120, 10, 'Name: ' . $surname . ', ' . $othernames, 0, 1, 'R');
$pdf->Ln(1);
// $pdf->Image('../Oscotech-Wallet/passport/' . $pix, 170, 15);
$pdf->Ln(1);
$pdf->Cell(70, 10, 'FORM No: ' . $matricno, 0, 0, 'L');

$pdf->Cell(60, 10, $semester . '  Semester:  ' . $level, 0, 0, 'L');

$pdf->Cell(20, 10, 'Session:' . $session, 0, 0, 'L');
$pdf->Ln(8);
$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, "HOD's Signature and Date", 0, 0, 'L');
$pdf->Cell(175, 3, "Bursar's Signature and Date", 0, 0, 'R');
$pdf->Ln(20);
$pdf->Cell(10, 5, '---------------------------------------------', 0, 0, 'L');
$pdf->Cell(170, 5, '---------------------------------------------', 0, 1, 'R');
$pdf->Cell(5, 3, "Academics Officer's Signature and Date", 0, 0, 'L');
$pdf->Cell(175, 3, "Faculty Officer's Signature and Date", 0, 0, 'R');

$pdf->Output();
?>