<?php
ob_start();
session_start();
include("../inc/config.php");
include("../inc/session.php");
$allPermissions = $_SESSION['permission'];

$requestData= $_REQUEST;



/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'id', 
            2 =>'id',
            3 =>'id',
            4 =>'id',
            5 =>'id',
            6 =>'id'
        );
 ini_set('memory_limit', '1024M');       
$condition='where 1=1 ';

      
		$statement = $pdo->prepare("SELECT * FROM tbl_transporter  $condition" );

		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    

		$sql = "SELECT * FROM tbl_transporter  $condition";
		error_log($sql);				
         
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( transport_name LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR driver_mobile_no LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR vechicle_no LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR vechicle_type LIKE '%".$requestData['search']['value']."%' ) ";
        	}
        	 
       $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalFiltered = $statement->rowCount();
        
/*Fetch Filter Data End*/       
        


         $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
        //  $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */	
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);

$data = array();
$i=$requestData['start']+1;
foreach( $query as $row ) {  // preparing an array
	$nestedData=array(); 
	
	   $action= "<div class='text-center'><div class='btn-group'>";
	                   if($session_id==1 || $allPermissions['transporter_details']['update']=="1"){ 
	                    $action.=" <a style='padding: 6px;margin-right: 3px;' href='transporter-edit.php?id=".$row["id"]."'  title='Update Details' class='tip btn btn-warning btn-xs'>
		                    <i class='fa fa-edit'></i>
		                </a>";
	                   }
		                
		                
    		                
    	$action.="</div></div>";
	
 	$nestedData[] = $i;
 	$nestedData[] = $action;
 	$nestedData[] = $row["transport_name"];
 	$nestedData[] = $row["driver_mobile_no"];
	$nestedData[] = $row["vechicle_no"];
	$nestedData[] = date("d-m-Y", strtotime($row["ftl_date"]));
	$nestedData[] = $row["advance_percentage"];
 	if($row["status"]==1){
	    $nestedData[] ="<div class='text-center'><button class='btn btn-success btn-xs' onClick='delete_Details(".$row["id"].")'><i class='fa fa-eye'></i> Active</button></div>";
	}else if($row["status"]==0){
	    $nestedData[] ="<div class='text-center'><button class='btn btn-danger btn-xs' onclick='DynamicActive(".$row["id"].")'><i class='fa fa-eye-slash'></i> In-Active</button></div>";
	}
 
	
	
	$data[] = $nestedData;
	$i++;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $data   // total data array
			);

echo json_encode($json_data);  // send data as json format

?>