<?php
include("../inc/config.php");
$courier_id=$_REQUEST['courier_id'];
$status=$_REQUEST['status'];
$requestData= $_REQUEST;
// $allPermissions = $_SESSION['permission'];

/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'awb_code',
            2 =>'courier_id',
            3 =>'status',
            4 =>'created_at',
            5 =>'company_name'
        );
        
/*Fetch All Data Start*/    
        if($courier_id=="all" && $status=="all")
    	    { $statement = $pdo->prepare("SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id"); }
    	else if($courier_id!="all" && $status=="all") 
    	    { $statement = $pdo->prepare("SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id where a.courier_id=$courier_id"); }
    	else if($courier_id=="all" && $status!="all") 
    	    { $statement = $pdo->prepare("SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id where a.status=$status"); }
	    else 
	    { $statement = $pdo->prepare("SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id where a.courier_id=$courier_id and a.status=$status"); }
    	 
    	/*$statement = $pdo->prepare("SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id");*/   
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
        if($courier_id=="all" && $status=="all")
    	    { $sql="SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id"; }
    	else if($courier_id!="all" && $status=="all") 
    	    { $sql="SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id where a.courier_id=$courier_id"; }
    	else if($courier_id=="all" && $status!="all") 
    	    { $sql="SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id where a.status=$status"; }
	    else 
	    { $sql="SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id where a.courier_id=$courier_id and a.status=$status"; }

        /*$sql="SELECT a.*,c.company_name  FROM tbl_awb a inner join courier_company c on a.courier_id=c.id";*/
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( a.awb_code LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR a.status LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR a.created_at LIKE '%".$requestData['search']['value']."%' ";
        	$sql.=" OR c.company_name 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(); 

 	$nestedData[] = $i;
 	$nestedData[] = $row["awb_code"];
	$nestedData[] = $row["company_name"];
	if($row["status"]==0){ $nestedData[] = "<a href='#' class='btn btn-success btn-xs'>Availabe</a>"; }	else{ $nestedData[] = "<a href='#' class='btn btn-danger btn-xs'>Not Available</a>"; }
	$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

?>