<?php require_once("includes/initialize.php");
global $database;
// session_start();

date_default_timezone_set('Africa/Lagos');
$user_data = Userdata::find_by_id($session->user_id);
$biodata = Biodata::find_by_id($user_data->formno, $user_data->email);

$dept = $biodata->courses;

if (substr($user_data->formno,2,2)>=20){
      
       Switch(substr($user_data->formno,2,2))
      {
          case '20':$table='form_data';
          break;
          
          case '21': $table='form_data21';
          break;
          
          case '22': $table='form_data22';
      }
      
     // echo $table;
       $biodata = Biodata2::find_by_id($table, $user_data->formno, $user_data->email);
       
        
        $new_dp = DEPARTMENTCLASS::new_admin_dept($biodata->formno);
        
        
          if (!isset($new_dp->department)){
           $biodata->courses ='Nil';   
          }else{
              
              
       $dept = $new_dp->department;
       
       
          }
       $level = $biodata->level;
       
       
      // $dept= $biodata->department;
  
    // if ($dept=='Microbiology (Option)'){
    //     $dept ='(Microbiology (Option)';
    // }
  
        // $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);
   $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);
$pix = "../Oscotech-Wallet/passport/".$pixf;


//print_r($display_course);

}


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

//$session_class = SESSIONCLASS::session_authenticate_second();


$s= find_semester($user_data->std_process_id);
$clevel= find_level($user_data->std_process_id,$level);



   $year = $_SESSION['year'];

 //echo "<br>User: ".  $user_data->std_process_id.' '.$level;

    //   $s= find_semester($user_data->std_process_id);
    //   $clevel= find_level($user_data->std_process_id,$level);
      
      if (($clevel=='HND II') &&(($user_data->std_process_id==4) OR($user_data->std_process_id==5))){
         
          $clevel='HND I';
          
          
      }else if (($clevel=='ND II') &&(($user_data->std_process_id==4) OR($user_data->std_process_id==5))){
            $clevel='ND I';
            
             
      }
    
$semid='';

//echo "<br>Year and Level: ".$year.'  '.$clevel.' Course code: '.$_GET['course_code'];

   $array_semester = SESSIONCLASSNEW::fetch_semester_id($year,$clevel);
   






//print_r( $array_semester);
 
//echo "<br>Matric Number AND Semester: ".$user_data->matricno.' '.$s;


if (isset($_GET['course_code'])) {
   
    if (($clevel=='ND I') OR ($clevel=='HND I')){
        
         $matricno = $user_data->formno;
        
    // }else if ($user_data->matricno!=0){
    //   // echo '<br> Matric Set: '.$user_data->matricno;
    //     $matricno=$user_data->matricno;
    // }else{
       // echo "Matric Number is needed for this registration";
       // redirect_to('eportal.oscotechesaoke.edu.ng/dashboard.php');
    }else if (($clevel=='ND II') OR ($clevel=='HND II')){
        
        if ($user_data->matricno!=0){
          $matricno = $user_data->matricno;
        }else{
             echo "Matric number is expected for year 2 registration";
        }
    }
}
    
    
    
    
    
    
    if (isset($_SESSION['sem_id']))
    {
             $semid = $_SESSION['sem_id'];
             
                if (($semid==1) && (($clevel=='ND I') OR ($clevel=='HND I'))){
                    $s="First";
                }else if  (($semid==2) && (($clevel=='ND I') OR($clevel=='HND I'))){
                    $s="Second";
                    
                }else if (($semid==3) && (($clevel=='ND II') OR($clevel=='HND II'))){
                    $s="First";
                }else if (($semid==4) && (($clevel=='ND II') OR($clevel=='HND II'))){
                    $s="Second";
                }
    }
                
               // echo "Emid: ".$semid.' '. $clevel.' '.$dept;
             $regTable = return_reg_table($semid, $clevel);
    
    
    
    
    
    $code = $_GET['course_code'];
    $date = date('Y-m-d h:i');
    
    
    $RegCourses = explode(',', $code);

    $total = 0;
        foreach ($RegCourses as $course_code) {

            $query = "SELECT * FROM tbl_courses 
            WHERE course_code = '$course_code' AND department = '$dept'
            ";
            $sql = $database->query($query);
            while ($rows = mysqli_fetch_array($sql)) {
                $code = $rows["course_code"];
                $title = $rows["course_title"];
                $status = $rows["course_status"];
                $unit = $rows["course_unit"];
                $total = $total + $unit;
            }
        } $total;
    
  

    if (($clevel =='ND II') && ($s=='Second')) {
        
        
        //echo "I am found here ND II";
        if (($_GET['course_code'] != "")){
            if (($dept == 'Science Laboratory Technology') AND ($total > 50)){
                echo "Maximum Course Unit is 50"; }
        
            else if (($dept == 'Business Administration and Management') AND ($total > 35)) {
                echo "Maximum Course Unit is 35";}
         else {
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO ndii_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            $result = $database->query($query);

            if (isset($result)) {
                
                 $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,9);
                echo 'Year II Registration Successful';//.$courses;
            }
        }}
    } 
    
    else if (($clevel=='ND II') AND ($s=='First')) {
        if (($_GET['course_code'] != "")){
            if (($dept == 'Science Laboratory Technology') AND ($total > 50)){
                echo "Maximum Course Unit is 50"; }
        
            elseif (($dept == 'Business Administration and Management') AND ($total > 35)) {
                echo "Maximum Course Unit is 35";}
         else {
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO ndii_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            $result = $database->query($query);

            if (isset($result)) {
                 $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,8);
                echo 'Year II Registration Successful';//.$courses;
            }
        }}
    }
    
    else if (($clevel =='ND I') AND ($s=='Second')) {
        if (($_GET['course_code'] != "") AND ($total<15)) {
            echo "Sorry, kindly select all the courses ";
        } else {
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO ndi_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            
            
           // $Updatequery="UPDATE ndi_tbl SET ses_sem_id='4' WHERE matricno='$matricno'";
            
            $result = $database->query($query);
            
            if (isset($result)) {
                 $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,6);
                echo 'Year I Registration Successful';//.$courses;
            }
        }
    }
    
       else if (($clevel=='ND I') AND ($s=='First')) {
        if (($_GET['course_code'] != "") AND ($total<15)) {
            echo "Sorry, kindly select all the courses ";
        } else {
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO ndi_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            
            
           // $Updatequery="UPDATE ndi_tbl SET ses_sem_id='4' WHERE matricno='$matricno'";
            
            $result = $database->query($query);
            
            if (isset($result)) {
                 $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,5);
                echo 'Year I Registration Successful';//.$courses;
            }
        }
    }    
    
    else if (($clevel == 'HND I') AND ($s=='Second')) {
        if (($_GET['course_code'] != "") AND ($total<15)) {
            echo "Sorry, kindly select all the courses ";;
        } else {
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO hndi_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            $result = $database->query($query);
            if (isset($result)) {
                
                 $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,6);
                echo 'Year I Registration Successful';
            }
        }
    }else if (($clevel == 'HND I') AND ($s=='First')) {
        if (($_GET['course_code'] != "") AND ($total<15)) {
            echo "Sorry, kindly select all the courses ";;
        } else {
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO hndi_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            $result = $database->query($query);
            if (isset($result)) {
                 $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,5);
                echo 'Year I Registration Successful';
            }
        }
    }
    else if ($clevel == 'PGD I') {
        if (($_GET['course_code'] != "") AND ($total<30)) {
            echo "Sorry Maximum Course Unit is".$total;
        } else {
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO pgdi_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            $result = $database->query($query);
            if (isset($result)) {
                echo 'Registration Successful';
            }
        }
    }
    else if (($clevel == 'HND II') AND ($s=='Second')) {
        if (($_GET['course_code'] != "") AND ($total>30)) {
            echo "Current semester courses cannot be ". $total;
        } else {
            
             
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO hndii_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            $result = $database->query($query);

            if (isset($result)) {
                
              $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,9);
                echo 'Year II Registration Successful';//.$courses;
            }
        }
    }else if (($clevel == 'HND II') AND ($s=='First')) {
        
        //echo "AM in HND II";
        if (($_GET['course_code'] != "") AND ($total>30)) {
            echo "Current semester courses cannot be ". $total;
        } else {
            
             
            $courses = $_GET['course_code'];
            $query = "
			INSERT INTO hndii_tbl (matricno, ses_sem_id, department, courses, regDate) 
			VALUES('$matricno','$semid','$dept', '$courses', '$date')
			";
            $result = $database->query($query);

            if (isset($query) and isset($result)) {
                
                 $updateActivity = SESSIONCLASSNEW::update_to_Second_semester($user_data->formno,8);
                 
                echo 'Year II Registration Successful';//.$courses;
            }else{
                echo 'Duplicate/error while saving the courses';
            }
        }
    }else{
        echo 'Year II Registration not successful';
    }
    


?>