<?php 
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']))
{
    $dept = $_POST['depart'];
    $lev = $_POST['level'];
    $prog = $_POST['programme'];
    $ses = $_POST['session'];
    
    $yr = 19;
    
    $retform = ONLINEPAY::restrictformno($dept,$lev,$prog);
    $dptcode = ONLINEPAY:: getdeptcode($dept);
    
    foreach ($retform as $r){
        
        $dept1 =$r->department;
        $level1 =$r->level;
        $formno = $r->formno;
    }
    
    if($dept != "" || $lev != "" || $prog != "")
    {
        //$get_prog = ONLINEPAY:: searchbydptprog($dept,$lev,$prog);
    $get_prog = ONLINEPAY:: searchbydptprog($dept1,$level1,$formno);
    }
    if ($lev == 'ND' && $prog == 'PU')
    {
        $progcode = 1;
        $levcode = 1;
        $msg = 'National Diploma On Full-Time Programme '.$dept." ".$dptcode->deptcode;
    }
    elseif ($lev == 'ND' && $prog == 'DP')
    {
        $progcode = '2';
        $levcode = '1';
        $msg = 'National Diploma On Daily Part-Time Programme '.$dept;
    }elseif ($lev == 'HND' && $prog == 'FT')
    {
        $progcode = '1';
        $levcode = '2';
        $msg = 'Higher National Diploma On Full-Time Programme '.$dept;
    }elseif ($lev == 'HND' && $prog == 'DP')
    {
        $progcode = '2';
        $levcode = '2';
        $msg = 'Higher National Diploma On Daily Part-Time Programme '.$dept;
    }
    
    //  $dptcode = ONLINEPAY:: getdeptcode($dept);
    
}
$count = 0;
$a_mat = ONLINEPAY:: auto_mat($dept,$lev,$prog);
foreach ($a_mat as $a){
    $sat = $a->matricno;
    
}
$sat = substr($sat, -4);
echo $sat;
$start_matno = $sat;
foreach ($dptcode as $code){
   $dcode =$code->deptcode;
   // $format = $yr.$progcode.$code->deptcode.$levcode;
}
//echo "guy ".$dcode;
$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">ND</option>
	                    <option value="HND">HND</option>		
					</select>
                </td>
                <td>
    				<select name="programme" id="programme" class="form-control action" style="width:120px" required>
    					<option value="">Select Programme</option>
    					<option value="DP">Daily-PartTime</option>
                        <option value="FT">Full-Time(HND)</option>
                        <option value="PU">Full-Time(ND)</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);
   // $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;
                   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>';
    					
    					$update = ONLINEPAY::update_matricno('student_activity_level',$mat,$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>