<?php 
error_reporting(1);
require_once("includes/initialize.php");
global $database;
$alldept = ONLINEPAY::showdept();
$allsession = ONLINEPAY:: showsession();
// College Martic Number Format : year|programmecode|departmentcode|Levelcode|4digit'e.g 0001'

if (isset($_POST['btnsearch']))
{
   // Kindly update level and programme respectively before generation
   $dept = $_POST['depart'];
    $lev = $_POST['level'];
    $prog = $_POST['programme'];
//   echo $ses = $_POST['session'];

     $pr = trim(substr($prog,0,2));
    
    $yr = 22;
    
    $guy =  ONLINEPAY::restrictB();
    
     $retform = ONLINEPAY::restrictformno($dept,$lev,$prog);
     
     //print_r($retform);
     $dptcode = ONLINEPAY:: getdeptcode($dept);
    
  
    //print_r($dptcode);
    foreach ($retform as $r){
        
      $dept1 =$r->department;
      $level1 =$r->level;
      $formno = $r->formno;
    $email = $r->email;
    }
    
    if($dept != "" || $lev != "" || $prog != "")
    {
        //$get_prog = ONLINEPAY:: searchbydptprog($dept,$lev,$prog);
        
       //echo $dept1. "   ".$level1."    ".$formno;
        
    $get_prog = ONLINEPAY:: searchbydptprog($dept1,$level1,$formno);
    }
    if ($lev == 'ND I' && $pr == 'PU')
    {
        $progcode = 1;
        $levcode = 1;
        $msg = 'National Diploma On Full-Time Programme '.$dept." ".$dptcode->deptcode;
    }
    elseif ($lev == 'ND I' && $pr == 'PT')
    {
        $progcode = '2';
        $levcode = '1';
        $msg = 'National Diploma On Daily Part-Time Programme '.$dept;
    }elseif ($lev == 'HND I' && $pr == 'FT')
    {
        $progcode = '1';
        $levcode = '2';
        $msg = 'Higher National Diploma On Full-Time Programme '.$dept;
    }elseif ($lev == 'HND I' && $pr == 'PT')
    {
     
        $progcode = '2';
        $levcode = '2';
        $msg = 'Higher National Diploma On Daily Part-Time Programme '.$dept;
    }
    
    
    if ( ($dept1 =='(Applied Science (Microbiology Option))') OR ($dept1=='Applied Science (Chemistry Option)') OR ($dept1=='Applied Science (Physics with Electronics Option)') OR ($dept1=='Applied Science (Biochemistry Option)') OR ($dept1=='Applied Science (Environmental Biology Option)')){
         $dptcodeOption = ONLINEPAY::getdeptOption($dept1);
         
        $levcode = $dptcodeOption->matListCode;
     }
    echo $dept1." Level Code: ".$levcode;
    
    
     // $dptcode = ONLINEPAY:: getdeptcode($dept);
    
}
$count = 0;

//echo "<br> Fetch Last mat".$dept." ".$lev." ".$prog."<br>";
$a_mat = ONLINEPAY:: auto_mat($dept,$lev,$prog);

//print_r($a_mat);

foreach ($a_mat as $a){
  $sat = $a->matricno;
    
}

echo "Last Mat: ".$sat;

$sat = substr($sat, -4);

$start_matno = $sat;
//foreach ($dptcode as $code){
  echo "DPT Code: ". $dcode =$dptcode->deptcode;
       // echo  $format = $yr.$progcode.$code->deptcode.$levcode;
//}
//echo "guy ".$dcode;
$yr=22;
 $format = $yr.$progcode.$dcode.$levcode;
?>

<html>
 <head>
  <title>Admin-GenerateMatric</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
        <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
        <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.flash.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
        <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.2/css/buttons.dataTables.min.css"/>
 </head>
 <body>
  <div class="container box">
   <h2 align="center">MATRICULATION LIST</h2>
   <h3 align="center">DEPARTMENT OF </h3>
   <h3 align="center"></h3>
   <br />
   <form method="post" action="" role="form" enctype="multipart/form-data">                        	
    	<div class="form-group input-group" align="center">
    	<table class="table table-bordered table-hover">
        	<tr>
        	    <td>
                    <select name = "depart" id="depart" class="form-control action" style="width:120px" required>
                        <option value="">Select Department...</option>
                        <?php foreach($alldept as $dept){?>
                        <option value="<?php echo $dept->department;?>"><?php 
                        $dprepare= strtolower($dept->department);
                        echo ucwords($dprepare);?></option> <?php }?>
                    </select>
                </td>
                <td>
                	<select name="level" id="level" class="form-control action" style="width:120px" required>
						<option value="">Select Level</option>
						<option value="ND I">ND I</option>
	                    <option value="HND I">HND I</option>		
					</select>
                </td>
                <td>
    				<select name="programme" id="programme" class="form-control action" style="width:120px" required>
    					<option value="">Select Programme</option>
    					<option value="PT22">Daily-PartTime</option>
                        <option value="FT22">Full-Time(HND)</option>
                        <option value="PU22">Full-Time(ND)</option>
    				</select>
    			</td>
    			
    			<!--<td>-->
    			<!--	<select name="session" id="session" class="form-control action" style="width:120px" required>-->
    			<!--		<option value="">Select Session</option>-->
    			<!--		<option value="18">2018/2019</option>-->
       <!--                 <option value="19">2019/2020</option>-->
                      
    			<!--	</select>-->
    			<!--</td>-->
            	<td>
                    <span class="input-group-btn">
                        <input type="submit" class="btn btn-danger" name="btnsearch" value="Search" id="btnsearch" title="Search Student">
                    </span>
                </td>
         	</tr>
        </table>
        </div>
    </form></br>
    
   <? //$n = 0; //$matno = ("%04d", $n);
   
   $yr=22;
      $format = $yr.$progcode.$dptcode->deptcode.$levcode; ?>
    
    <div class="panel panel-primary chat-panel">
        <div class="panel-heading ">
            <? echo $msg;?>
        </div><br/>
   <div class="table-responsive">
    <table id="applicant_table" class="table table-bordered table-striped">
     <thead>
      <tr>
        <th width="5%">S/N</th>  
        <th width="10%">FORM NO</th>  
        <th width="20%">SURNAME</th>  
        <th width="30%">OTHER NAMES</th>  
        <th width="45%">DEPARTMENT</th>
        <th width="20%">EMAIL</th>
        <th width="10%">LEVEL</th>
        <th width="10%">MATRIC NUMBER</th>
      </tr>
     </thead>
     <?php
        try
            { 
                if(empty($get_prog)){throw new Exception ();}
                else{
                foreach ($retform as $rows)
                {
                    $matno = str_pad($start_matno+1, 4, 0, STR_PAD_LEFT);
                    ++$start_matno;
             
                    $mat = $format.$matno;
                $_SESSION['mat']=$mat;
                   echo  '<tr>'
                            .'<td>'.++$count.'</td>'
        					.'<td>'.$rows->formno.'</td>'
        					.'<td>'.strtoupper($rows->surname).'</td>'
        					.'<td>'.strtoupper($rows->othernames).'</td>'
        					.'<td>'.$rows->department.'</td>'
        					.'<td>'.$rows->email.'</td>'
        					.'<td>'.$rows->level.'</td>'
        					.'<td>' .$mat.'</td>'
    					.'</tr>';
   			
    $mattoenter=$_SESSION['mat'];
    
   $update = ONLINEPAY::update_matricno('student_activity_level',$mattoenter,$rows->formno,$rows->email);
	

                }
                
                }
            }catch (exception $e){ }
        
     
     
     ?>
    </table>
   </div>
   </div>
  </div>
  <br />
  <br />
 </body>
</html>


<script type="text/javascript" language="javascript" >
 $(document).ready(function(){
     $('#applicant_table').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    });
});
 
</script>