<?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();
$user_data = Userdata::find_by_id($session->user_id);




$year=$_SESSION['year'];
$id = $session->user_id;
$user_data = Userdata::find_by_id($session->user_id);
 $array_semester=array();



//$session_first = SESSIONCLASS::session_authenticate1();

$user_data = User::find_by_id($session->user_id);



 if (substr($user_data->formno,2,2)>=20){
     
    // echo "mbnmmnmmStudent Process ID: ".$user_data->std_process_id;
     
     Switch(substr($user_data->formno,2,2))
      {
          case '20':$table='form_data';
          break;
          
          case '21': $table='form_data21';
          break;
          
          case '22': $table='form_data22';
      }
      
      
       $biodata = Biodata2::find_by_id($table, $user_data->formno, $user_data->email);
       
      
        $new_dp = DEPARTMENTCLASS::new_admin_dept($biodata->formno);
        
        //echo $new_dp->department;
          if (!isset($new_dp->department)){
           $biodata->courses ='Nil';  
           
           
           
          }else if ($new_dp->department=='Microbiology (Option)'){
      $department= $biodata->courses= $biodata->department='(Microbiology (Option)'; 
       Schoolfees::updateDepartment($biodata->formno, $dept);
      
        
      
          }
       else if ($new_dp->department=='Electronic/Telecommunication (Option)'){
       $department= $biodata->courses= $biodata->department = 'Electronics/Telecommunication (Option)';
       
        Schoolfees::updateDepartment($biodata->formno, $dept);
       
    }else{
        
         $department= $biodata->courses = $new_dp->department;

          }
       $level = $biodata->level;
       
                // $department =$new_dp->department;
  
        // $pixf = substr($biodata->passport, 10);
       $dp = DEPARTMENTCLASS::department_authenticate($biodata->department);
       $pix = "http://admission.oscotechesaoke.edu.ng/passport22/".trim($biodata->passport);
    
     
    
       
  }else{
     
     $biodata = Biodata::find_by_id($user_data->formno, $user_data->email); 


    $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
    $display_course = COURSECLASS::fetch_all_course($biodata->courses, $biodata->programme, $session_class->semester);
    $alldp = DEPARTMENTCLASS::find_all($dp->faculty);
    $pix = "../Oscotech-Wallet/passport/".$pixf;
    $level = $biodata->programme;
    $department = $biodata->courses;
    $formno = $biodata->formno;
}





if (substr($user_data->formno,2,2)>=20){
      
    //   $biodata = Biodata2::find_by_id($user_data->formno, $user_data->email);
       
      
    //   $new_dp = DEPARTMENTCLASS::new_admin_dept($biodata->formno);
        
    //       if (!isset($new_dp->department)){
    //       $biodata->courses ='Nil';   
    //       }else{
    //     $biodata->courses = $new_dp->department;
    //       }
    //  $level = $biodata->level;
       
       
    //   $department =$new_dp->department;
  
    //     // $pixf = substr($biodata->passport, 10);
    //   $dp = DEPARTMENTCLASS::department_authenticate($biodata->department);
    //   $pix = "http://admission.oscotechesaoke.edu.ng/passport/".trim($biodata->passport);
    
     
    
     
    
       
  }else{
     
     $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
     if (substr($user_data->formno,2,2)=='19'){

 $new_dp = DEPARTMENTCLASS::new_admin_dept($user_data->formno);
   $biodata->courses = $new_dp->department;
  $biodata->programme = $biodata->level; 
  
   $dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);
    $pixf = substr($biodata->pix, 11);
   $pix = "../application_account/passport/".$pixf;
   
 }else {


//   $biodata = Biodata::find_by_id($user_data->formno, $user_data->email);
  
$dp = DEPARTMENTCLASS::department_authenticate($biodata->courses);

//print_r($session_class);

//$display_course = COURSECLASS::fetch_all_course($biodata->courses, $biodata->programme, $session_class->semester);
$alldp = DEPARTMENTCLASS::find_all($dp->faculty);
 $pixf = substr($biodata->pix, 11);
$pix = "../Oscotech-Wallet/passport/".$pixf;


//print_r($display_course);

}

    $level = $biodata->programme;
    $department = $biodata->courses;
}












//echo "<br>: ". $user_data->std_process_id.' '.$level;
 $s= find_semester($user_data->std_process_id);
      $clevel= find_report($user_data->std_process_id,$level);
      
    //   echo "<br>Clevel: ".$clevel;
    //   echo "<br>Sem: ".$s;

if (($clevel=='ND I') OR ($clevel=='HND I')){
    
    $prs = '20'.substr($user_data->formno,2,2);
     $se = $prs.'/'.($prs+1);
     
     //$se='2021/2022';
     
}else if (($clevel=='ND II') OR ($clevel=='HND II')){
     $prs = '20'.(substr($user_data->formno,2,2)+1);
     $se = $prs.'/'.($prs+1);
}

//echo "Session: ".$se.'   '.$s;

//$status = SESSIONCLASS::session_authenticate($se, $s);
 //$ses_sem_id = $status->ses_sem_id;

$year = $_SESSION['year'];


$semester= find_semester($user_data->std_process_id);
$level= find_report($user_data->std_process_id,$level);

 $array_semester = SESSIONCLASSNEW::fetch_semester_all_id();
  
  $formno=$user_data->formno;
$matricno=$user_data->matricno;

//echo $level;

// if (($level == 'ND I') OR ($level == 'HND I'))
// { $level = $level."I"; }
$current_session = $session_class->session;

//echo "<br>Session: ".$session_class->session;
$start_session = $user_data->start_session;

    
    if (isset($_GET['ses_sem_id'])){
 
    $ses_sem_id =$_GET['ses_sem_id'];
    
 //   echo "Session: ".$ses_sem_id;
    
    $session_class = SESSIONCLASS::_session($ses_sem_id);
    

//echo "<br>Session: ".$session_class->session;
    }else{
        die('Successfully registered courses not found in data bank');
    }
    
    if ( $ses_sem_id==1)
    {
        $matricno = $user_data->formno;
        $msg = 'FORM NO:';
       
          
        // echo $level; 
        if ($level == 'ND II') { $level = 'ND I'; }
        else if ($level == 'HND II') { $level = 'HND I'; }
        
        
      
    }else if ( $ses_sem_id==2)
    {
        $matricno = $user_data->formno;
        $msg = 'FORM NO:';
        if ($level == 'ND II') { $level = 'ND I'; }
        else if ($level == 'HND II') { $level = 'HND I'; }
        
        
    }else if ( $ses_sem_id==3)
    {
        
        if ($user_data->matricno!=0)  { 
            
             $matricno = $user_data->matricno;
              $msg = 'MATRIC NO:';
            
        }
        
      
    }else if( $ses_sem_id==4)
    {
        $matricno=$user_data->matricno;
        $msg = 'MATRIC NO:';
        
    }else{
         $matricno=$user_data->matricno;
        $msg = 'MATRIC NO:';
    } 
    
    
    
 
$surname = $biodata->surname;
$othernames = $biodata->othernames;

// echo "<script>    alert('$matricno $msg $level'); </script>";

$output = '';


class coursePDF extends FPDF
{
    public $matricno;
    public $formno;
    public $department;
    public $semester;
    public $level;
    public $se;
    public $pix;
    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', 'BI', 11);
        $this->SetTextColor(248,0,0);
        $this->Cell(0, 2, 'You are requested to report to your Faculty Officer immediately and complete your registration formalities to obtain', 0, 1, 'L');
        $this->Ln(3);
        
        $this->Cell(0, 2, ' your Certificate of Registration (C.O.R) within two weeks from the date of printing this course form.', 0, 0, 'L');
         $this->Ln();
        $this->SetFont('Arial', '', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

    function headerTable($database, $matricno,$formno, $level, $ses_sem_id, $department)
    {
        // echo "<script>    alert('$matricno $ses_sem_id $level'); </script>";
    
    //echo "<br>FullGuy: ".$level;
        if ($level == "ND II") {
            
            // echo "<br>".$matricno;
            // echo "<br>".$ses_sem_id;
            $qry = "SELECT * FROM ndi_tbl
        	WHERE matricno ='$formno' and ses_sem_id = '$ses_sem_id'
        	UNION SELECT * FROM ndii_tbl WHERE matricno ='$matricno' and 
        	ses_sem_id = '$ses_sem_id'";
        }else if ($level == "ND I") {
            
           
            $qry = "SELECT courses, regDate FROM ndi_tbl 
			WHERE matricno ='$formno' and ses_sem_id = '$ses_sem_id'
			";
        }else if ($level == "PGD I") {
            $qry = "SELECT courses, regDate FROM pgdi_tbl 
			WHERE matricno ='$matricno' and ses_sem_id = '$ses_sem_id'
			";
        }else if ($level == "HND I") {
            $qry = "SELECT courses, regDate FROM hndi_tbl 
			WHERE matricNo ='$matricno' and ses_sem_id = '$ses_sem_id'
			";
        } else if ($level == "HND II") {
            $qry = "SELECT * FROM hndi_tbl
        	WHERE matricno ='$matricno' and ses_sem_id = '$ses_sem_id'
        	UNION SELECT * FROM hndii_tbl WHERE matricno ='$matricno' and 
        	ses_sem_id = '$ses_sem_id'";
        }

        $result = $database->query($qry);
        // echo "<script> alert('$result Hello $ses_sem_id'); </script>";
        $count = 0;
        while ($row = mysqli_fetch_array($result)) {
            $code = $row{"courses"};
            $date = $row{"regDate"};
        }
        // echo "<script> alert('$date Hello $code'); </script>";
        $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 = $database->query($query);
            while ($rows = mysqli_fetch_array($sql)) {
                $count += 1;
                $code = $rows["course_code"];
                $title = strtoupper($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, $se, 0, 0, "C");
        $this->Ln(20);
    }
}


$pdf = new coursePDF();
$pdf->SetTitle('Reprint Course Form','UTF-8');
$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($pix, 170, 15, 30,30);
$pdf->Ln(1);
$pdf->Cell(70, 10, $msg. ' ' . $matricno, 0, 0, 'L');


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

$pdf->Cell(20, 10, 'Session:' . $se, 0, 0, 'L');
$pdf->Ln(8);
// echo "<script>    alert('$matricno $level $ses_sem_id'); </script>";
$pdf->headerTable($database, $matricno,$formno, $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($surname.'CourseForm Reprint.pdf','I');
?>