<?php

// session_start();
//error_reporting(1);
// set_time_limit(105000000);
error_reporting(E_ALL);
ini_set('display_errors',1);
require_once("includes/initialize.php");



$alldept = ONLINEPAY::showdept();

// date_default_timezone_set('Africa/Lagos');
// $dprepare ="";
$allsess = ONLINEPAY::showsession();


//print($allsession);

 $tag = $_GET['type'];
 
 if ($tag=='omoluabi_transcript'){
     $tag='transcript_tranzact';
 }
// $_SESSION['tablename'] =$tag;







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


// //if(isset($_POST['fac']) || ($_POST['dpt']))
 if (isset($_POST['search']))
 {
     $portal= $_POST['portal'];
   
  if (($portal=='') OR ($portal=='2')){
   $start_date = date('Y-m-d', strtotime($_POST['start_date']));
   $end_date = date('Y-m-d', strtotime($_POST['end_date']));
  
 $count = 0;
       
      
        $allsession = ONLINEPAY::find_omoluabi_cert_name($tag, $start_date, $end_date);
    
 
 $brief='Transcript Payment Between '.$start_date .' AND '.$end_date;
  }else{
      
  }
 
 
 //print_r($allsession);
 
 
 
 $output= ' <table id="bursar" class="table table-striped table-bordered">
             <thead>
                                     <tr>
                                     <th>S/N</th>  
                                         <th>MATRIC NO</th>
                                         <th>PROG. TYPE</th>  
                                         <th>TRAN. REF</th>
                                         <th>SURNAME</th>     
                                       
                                         <th>DEPARTMENT</th>
                                         <th>LEVEL</th> 
                                         
                                         <th>AMOUNT</th>
                                         <th>DATE/TIME</th>                         
                                     </tr>
                                 </thead>
         ';

// $_SESSION['allsession']=$allsession;

$total_sum=0;
  foreach ($allsession as $student){
    
                       
            
 					
                        
                        $total_sum += $student->amount;
                     $output .='<tr>'
 							.'<td>'.++$count.'</td>'
 							.'<td>'.$student->matricno.'</td>'
 							
 							.'<td>'.$student->ptype.'</td>'
 							.'<td>'.$student->txt_ref.'</td>'
 							.'<td>'.strtoupper($student->fullname).'</td>'
 							
 							.'<td>'.strtoupper($student->department).'</td>'
 							.'<td>'.strtoupper($student->level).'</td>'
 						
 							.'<td>'.$student->amount.'</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;
                

        
?>


 
 
  
 
 
  
            <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 >
<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/exceldownloadtranscript.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 = "transcript-payment-details.csv";

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

</script>