<?php
 error_reporting(0);
// set_time_limit(105000000);

require_once("includes/initialize2.php");

session_start();

$alldept = ONLINEPAY2::showdept();

//print_r($alldept);

// date_default_timezone_set('Africa/Lagos');
// $dprepare ="";
//$allsession = ONLINEPAY2::showsession();

$allsession = ONLINEPAY::showsession();

$tag = $_GET['type'];
if ($tag=='admission_tranzact'){
    $tag = 'admission_online_payment';
}
$_SESSION['tablename'] =$tag;







 $output=''; $output2='';$prog='';


// //if(isset($_POST['fac']) || ($_POST['dpt']))
 if (isset($_POST['search']))
 {
 $portal =$_POST['portal'];
 $fac =$_POST['fac'];
 $dept = $_POST['depart'];
 $session = $_POST['sess'];
 $level = $_POST['lev'];
 $_SESSION['amount']=array();
 $_SESSION['amount']=$amount='9100.00';
//  $view = $_POST['view'];
  $prog = $_POST['prog'];
  
  $start_date = date('Y-m-d', strtotime($_POST['start_date']));
  $end_date = date('Y-m-d', strtotime($_POST['end_date']));
  
  
  $_SESSION['portal']= $portal;
  $_SESSION['fac']= $fac;
  $_SESSION['dept']= $dept;
  $_SESSION['sess']= $session;
  $_SESSION['lev']= $level;
  $_SESSION['view']= $view;
  $_SESSION['prog']= $prog;
  $_SESSION['start']= $start_date;
  $_SESSION['end']= $end_date;
  
 $table = $_POST['ptype'];
   
  
 $count = 0; $total_sum=0; $brief='';
 if (isset($dept) && ($dept=='all')){
     
     //echo "Admin form: ".$tag;
     $allsession= ONLINEPAY2::allfee($tag,$amount);
    $brief=$dept.' Departments';
 
    
 }else if ($fac !=''){
     
      $allsession= ONLINEPAY2::showsessionfaculty($tag, $fac, $level, $session);
    $brief='Faculty of '. $fac;
 
     
 }else if (($prog!='') && ($level !='') && ($session!='')){
 
 $allsession= ONLINEPAY2::showsessionprog($tag, $prog, $level, $session);
 if (($prog=='PU') OR ($prog=='FT')){
     $dept='Full Time';
     
 }else if ($prog=='DP'){
  $dept='Daily Part Time';
 
 }
 $brief=$dept.' Payments';
 
 }else if (($prog !='') && ($level !='')){
    
    //  $table = $_SESSION['tablename'];
    //  $prog = $_POST['prog'];
    //  $level = $_POST['lev'];
      $allsession= ONLINEPAY2::searchbyprogramme($tag, $prog, $level);
      
      $brief='All '.$prog.' Payments';
 }
 else if ((isset($start_date) && $start_date !='') AND (isset($end_date) && ($end_date !=''))){
 
 $allsession= ONLINEPAY2::searchbydate($tag, $amount, $start_date, $end_date);
 $brief='Admission Form Payment Between '.$start_date .' AND '.$end_date;
 }

 else{
 
 $allsession= ONLINEPAY::showsessiondetails($tag, $dept, $level, $session);
 $brief=$dept.' Department';
 }
 //print_r($allsession);
 
 
 
 $output= ' <table id="bursar" class="table table-striped table-bordered">
             <thead>
                                     <tr>
                                     <th>S/N</th>  
                                         <th>FORM NO</th>
                                         <th>PROG. TYPE</th>  
                                         <th>TRAN. REF</th>
                                         <th>SURNAME</th>     
                                         <th>OTHER NAME</th> 
                                         
                                         <th>DEPARTMENT</th>
                                         <th>LEVEL</th> 
                                         
                                         <th>AMOUNT</th>
                                         <th>DATE/TIME</th>                         
                                     </tr>
                                 </thead>
         ';

$_SESSION['allsession']=$allsession;
  foreach ($allsession as $student){
    
                        if (isset($portal) AND ($portal==1)){
                        $output='';
                        $output='No record found';
 							    
                        }else if (isset($student->formno)){
                            
                            if ((substr($student->formno,0,2)=='DP') and ($student->level=='ND')) {
		                     $programtype="ND DPT";
		                   
		                   }else if (substr($student->formno,0,2)=='PU'){
		                    $programtype="ND PU";
		                    
		                      
		                    }else if (substr($student->formno,0,2)=='FT'){
		                    $programtype="HND FT";
		                    
		                    }else{
		                       $programtype="HND DPT"; 
		                    }
                            $student_number =$student->formno;
                            $dept=$student->department;
                            
                            
                        }
                        
                        if (isset($portal) AND ($portal==1)){
 							    $student_surname =$student['surname'];
                        }else if (isset($student->surname)) {$student_surname =$student->surname;}
                        
                         if (isset($portal) AND ($portal==1)){
 							    $student_othername =$student['othername'];
                        }else if (isset($student->othernames)){$student_othername =$student->othernames;}
                        
                        
            //             if (isset($student->status)){
 							    // $student_status =$student->status;
            //             }else {$student_status ='Approved Successfully';}
                        
                        if (isset($portal) AND ($portal==1)){
 							    $student_ref =$student['txn_ref'];
                        }else {$student_ref =$student->txt_ref;}
                        
                         if (isset($portal) AND ($portal==1)){
 							    $student_datetime =$student['date_time'];
                        }else if (isset($student->datetime)){$student_datetime =$student->datetime;}
                        
                        if (isset($portal) AND ($portal==1)){
 							    $student_amount =8500.00;
 							    //$student['amount'];
                        }else if (isset($student->amount)){$student_amount =$student->amount;}
                        
                         if (isset($portal) AND ($portal==1)){
 							    $student_level =$student['level'];
                        }else if (isset($student->level)){$student_level =$student->level;}
                        
                        
                        $total_sum +=8500;
                     $output .='<tr>'
 							.'<td>'.++$count.'</td>'
 							.'<td>'.$student_number.'</td>'
 							
 							.'<td>'.$programtype.'</td>'
 							.'<td>'.$student_ref.'</td>'
 							.'<td>'.strtoupper($student_surname).'</td>'
 							.'<td>'.strtoupper($student_othername).'</td>'
 							.'<td>'.strtoupper($dept).'</td>'
 							.'<td>'.$student_level.'</td>'
 						
 							.'<td>'.'8500.00'.'</td>'
 							.'<td>'.$student_datetime.'</td>'
 						.'</tr>'; 
                     } 
                     
                     $output .='<tr><td colspan="10"><center><h3>Brief Summary: '.ucwords($brief).' </h3></center></td></tr>';
                    $output .=' <tr>
                                     <td colspan="3">Total Number of Students</td> <td colspan="2">'.$count.'</td> 
                                         <th colspan="3">Total receivable</th>  <td colspan="2">'.$total_sum.'</td>
                                                                  
                                     </tr>';
                 $output.='</table>';
                
                
 } else {
   $output= "<center><h4>No record to display</h4></center>";
   
 }

        
                echo $output2;
                

        
?>


 
 
   <!--$start_date=$_GET['start'];-->
   <!--    $end_date=$_GET['end'];-->
   <!--    $portal=$_GET['portal'];-->
   <!--     $tag=$_GET['tag'];-->
<?php

//  if (isset($_POST['export_excel'])){
    
    
    
//     $start_date= $_SESSION['start_date'];
//      $end_date= $_SESSION['end_date'];
//      $portal=$_SESSION['portal'];
     
// //  if (($start_date !='') AND ($end_date !='')){
 
//   if ($portal==1){
// //     // echo "Portal 1: ".$tag." ". $start_date." ". $end_date;
    
//       $tag="tranzact_t";
// //     //  $portal']=$portal;
//       $allsession= ONLINEPAY::searchbydateportal1($tag, $start_date, $end_date);
     
     
//   }else{
     
//       $portal=2;
     
//         $tag = $_SESSION['tablename'];
// //     //   $tag = $_GET['tag'];
// //     //  
// //     //   $_SESSION['tablename'] =$tag;


// //     //  echo "Portal 2: ".$tag." ". $start_date." ". $end_date;
//   $allsession= ONLINEPAY::searchbydate($tag, $start_date, $end_date);
 
// // //   print_r($allsession);
//  }
// // // $brief='Between '.$start_date .' AND '.$end_date;
// //  }
 
 
    

// //     // if (isset($_SESSION['allsession'])){
        
// //     //     $tudent=$_SESSION['allsession'];
        
// //     // }
    
// //     if (isset($portal)){
    
//         if ($portal==1){
//             $portal='PORTAL1';
//         }else if ($portal==2){
//             $portal='PORTAL2';
//         }
// //     }
// //   // print_r($student);
    
    

//       header("Content-Type: text/csv");
//       header("Content-Disposition: attachment; filename=$portal".".csv");
//       header("Cache-Control: no-cache, no-store, must-revalidate");
//       header("Pragma: no-cache");
//       header("Expires: 0");
//       ob_end_clean();
//       $output = fopen("php://output", "w");
     
   
// //     // fputcsv($output, array($portal.' BETWEEN '.$start_date. ' AND '.$end_date));
//      fputcsv($output, array("SN","FORM NO","PROGRAMME TYPE","TRANSACTION REF.","SURNAME","OTHERNAMES","DEPARTMENT","LEVEL","AMOUNT","PAYMENT DATE"));
 
// //     foreach($allsession as $studentdata){
// //         if ($portal=='PORTAL1'){
            
            
// //         }else if ($portal=='PORTAL2'){
// //             $sn++;
// //             $formno = $studentdata->formno;
// //             $transref=$studentdata->txt_ref;
// //             $surname=strtoupper($studentdata->surname);
// //             $othernames=strtoupper($studentdata->othernames);
// //             $department=strtoupper($studentdata->department);
// //             $level=strtoupper($studentdata->level);
// //             $desc=strtoupper($studentdata->status);
// //             $amount=$studentdata->amount;
// //             $datetime=date('Y-m-d',strtotime($studentdata->datetime));
		    
// // 		    if (isset($portal) AND ($portal==1)){
// //                             $dept = $student['programme'];
// //                         }else if(isset($student->department)){$dept = $student->department;}
                        
// //                         if (isset($portal) AND ($portal==1)){
// //  							    $student_number =$student['fno'];
// //                         }else if (isset($student->formno)){
                            
// //                             if ((substr($student->formno,0,2)=='DP') AND ($student->level=='ND I')){
// // 		                     $programtype="ND I DPT";
// // 		                    }else if ((substr($student->formno,0,2)=='DP') AND ($student->level=='ND II')){
// // 		                        $programtype="ND II DPT"; 
// // 		                   }else if ((substr($student->formno,0,2)=='PU')AND ($student->level=='ND I')){
// // 		                    $programtype="ND I FT";
		                    
// // 		                    }else if ((substr($student->formno,0,2)=='PU')AND ($student->level=='ND II')){
// // 		                      $programtype="ND II FT";  
// // 		                    }else if ((substr($student->formno,0,2)=='FT')AND ($student->level=='HND I')){
// // 		                    $programtype="HND I FT";
// // 		                    }else if ((substr($student->formno,0,2)=='FT')AND ($student->level=='HND II')){
// // 		                    $programtype="HND II FT";
// // 		                    }
// //                             $student_number =$student->formno;
// //         }
		   
		  
// // 		     fputcsv($output,array($sn,$formno,$transref,$surname,$othernames,$programtype,$department,$level,$desc,$amount,$datetime));
           
// // 		 } 
 
        
    
// //  }
// // //  echo json($output);
// //   fclose($output)
//  }
?>
 
 
 
  
            <script src="javascripts/jquery.min.js"></script>  
           <link rel="stylesheet" href="javascripts/bootstrap.min.css" />  
           <script src="javascripts/jquery.dataTables.min.js"></script>  
           <script src="javascripts/dataTables.bootstrap.min.js"></script>
           
         
  
  
  
  <script>

  $( function() {
      $( "#start_date" ).datepicker();
      $( "#end_date" ).datepicker();
     
  } );
</script>           
             
   
       <?php 
       if (isset($brief)){
           echo "<h2 style='text-align:center'>".$brief."</h2>";
           
           $_SESSION['brief']=$brief;
       }
          ?> 
		<!--<div class="container" style="position:relative;">-->
		
		
<div style="display:inline">

    <!--<div class="form-group input-group" align="Right">-->
    <table class="table table-bordered table-hover" align="center">
        
<form method="POST" action=""  id="insert_data" style="margin-top:0">
        <tr>
<div style="margin-top:0px; border-bottom:1px inset #000">
    
    

<td width="90px">
<label style="margin-left:0px" >Portal</label>
<select id="portal" name="portal" style="width:100%">
<option value="">Choose...</option>
<option value="1">Portal 1</option>

<option value="2">Portal 2</option>

</select>
</td>    
    


<!--<td >-->
<!--<label style="margin-left:10px">Faculty</label>-->

<!--<select id="fac" name="fac" style="width:100%" >-->
<!--<option value="">Choose...</option>-->
<!--<option value="Business and Management Sciences">Business and Management Sciences</option>-->
<!--<option value="Environmental Studies">Environmental Studies</option>-->
<!--<option value="Applied Sciences">Applied Sciences</option>-->
<!--<option value="Engineering">Engineering</option>-->
<!--<option value="Information Communication and Technology">Information Communication and Technology</option>-->
<!--</select>-->
<!--</td>-->
<!--<td width="80px">-->
<!--<label style="margin-left:0px" >Department</label>-->
<!--<select id="depart" name="depart" style="width:100%">-->
<!--<option value="">Choose...</option>-->
<!--<option value="all">All departments</option>-->
<!--<?php //foreach($alldept as $dept){?>-->
<!--<option value="<?php //echo $dept->department;?>">
//  -->
<!--//$dprepare= strtolower($dept->department);-->
<!--//echo ucwords($dprepare);?></option>-->
<!--<?php //}?>-->
<!--</select>-->
<!--</td>-->
<!--<td width="50px">-->
<!--<label style="margin-left:0px" >Session</label>-->
<!--<select id="sess" name="sess"  style="width:100%" >-->
<!--<option value="">Choose...</option>-->
<!--<?php //foreach($allsess as $session){?>-->
<!--     	<option value="<?php //echo $session->session;?>">
 -->
<!--      //echo $session->session?></option><?php //}?>>-->
      <!--<option value"2019/2020">2019/2020</option>-->
<!--</select>-->
<!--</td>-->

<!--<td width="50px">-->
<!--<label style="margin-left:0px">Programme</label>-->

<!--<select id="prog" name="prog" style="width:100%" >-->
<!--<option value="">Choose...</option>-->
<!--<option value="DP">Daily Part-Time</option>-->
<!--<option value="FT">Full-Time</option>-->
<!--<option value="PU">Post UTME</option>-->
<!--</select>-->
<!--</td>-->

<!--<td width="50px">-->
<!--<label  style="margin-left:0px" >Level</label>-->
<!--<select id="lev" name="lev" style="width:100%">-->
<!--<option value="">Choose...</option>-->
<!--<option value="ND I">ND I</option>-->
<!--<option value="ND II">ND II</option>-->
<!--<option value="HND I">HND I</option>-->
<!--<option value="HND II">HND II</option>-->
<!--</select>-->
<!--</td>-->
<td >
<div class="input-daterange">

<div style="font-size:14px;float:left"><label>Start Date</label></div>
<div  style="width:100%">
    <input type="text"  autocomplete="off" name="start_date" id="start_date" style="width:100%; text-align:left" placeholder="StartDate"/>
</div>
</div>
</td>


<td >
    
<div class="input-daterange">

<div><label style='text-align:left;font-size:14px'>End Date</label></div>
<div style="width:100%">
    <input type="text" autocomplete="off" name="end_date" id="end_date" style="width:100%; text-align:left" placeholder="EndDate"/>
</div>

</div>
</td>


<td width="50px">
    <div><label  style="margin-left:0px" >Action</label></div>
<div>
    <input type="submit" name="search" id ="search" value="Search" class="btn btn-primary" />
</div>
</td>

<td width="50px">
    <div><label  style="margin-left:0px" >Action</label></div>
   <div >
        <input type="submit" name="Print_pdf" id ="Print_pdf" class="btn btn-danger" value="Print PDF" />
    </div>
</td>





</form>

<!--<form method="POST" action="">-->

<!--</form>-->

<td width="50px">
    <div><label  style="margin-left:0px" >Action</label></div>
   <div >
       
    <button name="export_excel" id ="export_excel" class="btn btn-success">Export Excel</button>
        
    </div>
</td>
</tr>
</table>
</div>





 <div id="bursar" style="width:100%; height:500px; overflow:scroll; background-color:; margin-left:3px ">
 
 <?php
 if (isset($output)){
 
    echo $output;
 
 }
?>                  
</div>

<div id="summary" style="width:100%; height:40px; background-color:; margin-left:3px ">
                        
        <?php
 if (isset($output2)){
 
    echo $output2;
 
 }
?>                  
                  
</div>
</div>

</body>
<!--<script src="../javascripts/jquery.min.js"></script>        -->
<!--<script src="../javascripts/bootstrap.min.js" type="text/javascript"></script>-->



                               

 
 
  
<script type="text/javascript">


var ptype = <?php echo json_encode($tag);?>;

$(document).ready(function() {
    
    // e.preventDefault();
    // $('#fac').change(function(){
        
    //     $('#depart').attr("disabled",true);
    // });
    
    // $('#depart').change(function(){
    //     $('#fac').attr("disabled",true);
    // })
  
  /*
    //e.preventdefault(e);
// 	$('#bursar').DataTable();
	$('#fac').on('change', function(){
	//	alert($('#fac').val());
	if($('#fac').val() != '')
		{
			var fac = $(this).val();
			//alert("value" + faculty);
			$('#depart').attr('disabled', 'disabled');
		}else {
		$('#depart').attr('disabled', 'false');
	}
		 
	})
	
	$('#depart').on('change', function(){
	    //e.preventdefault(e);
	//	alert($('#fac').val());
	if($('#depart').val() != '')
		{
			var dpt = $(this).val();
			//alert("value" + faculty);
			$('#fac').attr('disabled', 'disabled');
		}else {
		$('#depart').attr('disabled', 'false');
	}
		 
	})
	
// 	$('.input-daterange').datepicker({
//   todayBtn:'linked',
//   format: "yyyy-mm-dd",
//   autoclose: true
//  });

	 
$('#btn').on('click', function(e){
    e.preventDefault(e);
    var fac= $('#fac').val();
    var dpt =$('#depart').val();
    var lev = $('#lev').val();
    var ses = $('#sess').val();
    var prog = $('#prog').val();
    var view = $('#view').val();
    	alert(ptype);
    alert(fac);
    alert(dpt);
    alert(lev);
    alert(ses);
    alert(ses);
    
		
        $.ajax({                    
            method:"POST",
			url: "fetch_details.php",
            data:{ptye:ptype,fac:fac,dpt:dpt,lev:lev,ses:ses,prog:prog,view:view},
            success:function(data)
            {
				alert(data);
               $('#bursar').html(data);
            }
        })
    });
    
    $('#search').on("click", function(e){
    e.preventDefault(e);
    
    var fac= $('#fac').val();
    var dpt =$('#depart').val();
    var lev = $('#lev').val();
    var ses = $('#sess').val();
    var prog = $('#prog').val();
    var start = $('#start_date').datepicker();
    var end = $('#end_date').datepicker();
    
    alert(ptype);
    alert(fac);
    alert(dpt);
    alert(lev);
    alert(ses);
   
    
        {
            $.ajax({
            	url:"fetch_details.php",
            	method:"POST",
            	data:{fac:fac,ptype:ptype,dpt:dpt,lev:lev,ses:ses,prog:prog,start:start,end:end},
            	success:function(data)
            	{
            	    alert(data);
            		$('#bursar').html(data);
            	}
            });
        }
 });*/
 
 
//  const objectToCsv=function(data){
//      const csvRows=[];
//      const headers=Object.keys(data[0]);
//      csvRows.push(headers.join(','));
//      console.log(headers);
//  }
 
 const saveData = (function () {
    const a = document.createElement("a");
    document.body.appendChild(a);
    a.style = "display: none";
    return function (data, fileName) {
        const blob = new Blob([data], {type: "octet/stream"}),
            url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
    };
}());
 
 $('#export_excel').on("click",function(e){
     e.preventDefault(e);
     
    //  alert("here");
       var tag= <?php echo json_encode($tag);?>;
     var start_date= <?php echo json_encode($start_date);?>;
     var end_date= <?php echo json_encode($end_date);?>;
      var portal= <?php echo json_encode($portal);?>;
     if ((portal=='') || (portal=='undefined')){
         portal=2;
     }
    //   alert(start_date+' '+end_date+' '+portal);
     $.ajax({
        type:"POST",
        url:"pages/exceldownloadadmissionform.php",
        data:{tag:tag,start_date:start_date,end_date:end_date,portal:portal},
        
        // beforeSend: function(){
        //     alert('Retrieving student payment details')
        // },
        
        success: function(data){
            // alert(data);
            
            // const csvData=objectToCsv(data);
            
    //         const data =data;
    //         // 'a,b,c\n5,6,7',
       fileName = "admission-payment-details.csv";

          saveData(data, fileName);
        }
        
         
         
          })
 })
 
 $('#Print_pdf').on("click", function(e){
    e.preventDefault(e);
        {
    		window.open("bursary_report.php");
        }
 });
    
    
    
    
    
});

</script>