<?php
ob_start();
session_start();
include "../inc/config.php";
include "../inc/session.php";

$requestData= $_REQUEST;
$allPermissions = $_SESSION['permission'];


$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$pod_type = $_REQUEST['pod_type'];

$condition='';

if($pod_type!=""){
    $condition.=" and tp.pod_type=".$pod_type;
}

if($from!="" && $to!=""){
     $condition.=" and tp.created_at BETWEEN '$from' and '$to'";
}

if($session_id==1){
    $condition.="";
}else{
    $condition.=" and pk.client_id IN ($session_client_ids)";
}



/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'id',
            2 =>'id',
            3 =>'id',
            4 =>'id',
            5 =>'id',
            6 =>'id',
            7 =>'id',
            8 =>'id',
            9 =>'id',
            10 =>'id',
            11 =>'id',
            12 =>'id',
        );
        
/*Fetch All Data Start*/        
    	$statement = $pdo->prepare("SELECT tp.* FROM `tbl_pod` tp 
    	INNER JOIN pickup_entry pk ON tp.awb_id=pk.id
    	WHERE 1=1 $condition ORDER BY tp.id DESC");
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
        $sql="SELECT tp.* FROM `tbl_pod` tp INNER JOIN pickup_entry pk ON tp.awb_id=pk.id
    	WHERE 1=1 $condition";

        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( tp.awb_no LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR tp.location LIKE '%".$requestData['search']['value']."%' )";
        }
        
        
        error_log("sql: ".$sql);
        $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']."   ";
 
 
        $statement = $pdo->prepare($sql);
        $statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);

$data = array();
$i=$requestData['start']+1;
foreach( $query as $row ) {
    
    $nestedData=array(); 
    
    
    if($row['pod_type']=='1'){ 
        $pod_type = 'PICKUP';
    }else if($row['pod_type']=='2'){ 
        $pod_type = 'DELIVERY';
    }

 	$nestedData[] = $i;
	$nestedData[] = "<input type='checkbox' class='checkbox' name='checkboxlist' value=".$row['id']." >";
	$nestedData[] = "<div class='text-center'>".$row["awb_no"]."</div>";
	$nestedData[] = "<div class='text-center'>".$pod_type."</div>";
	$nestedData[] = "<div class='text-center'>".date("d-m-Y", strtotime($row["created_at"]))."</div>";
	
if($row['url_1']!=''){
	$nestedData[] = '<div class="text-center">
	                                    <a download="'.$row['url_1'].'" href="img/POD/'.$row['url_1'].'" title="ImageName">
                                         <img alt="ImageName" style="width: 80px;border: 2px solid red;" class="img-thumbnail" src="img/POD/'.$row['url_1'].'">
                                        </a>
                    </div>';
}else{
    $nestedData[] = '<div class="text-center"><span class="NoImage">No Image</span></div>';
}	


if($row['url_2']!=''){
	$nestedData[] = '<div class="text-center">
	                                    <a download="'.$row['url_1'].'" href="img/POD/'.$row['url_1'].'" title="ImageName">
                                         <img alt="ImageName" style="width: 80px;border: 2px solid red;" class="img-thumbnail" src="img/POD/'.$row['url_1'].'">
                                        </a>
                    </div>';
}else{
    $nestedData[] = '<div class="text-center"><span class="NoImage">No Image</span></div>';
}


if($row['url_3']!=''){
	$nestedData[] = '<div class="text-center">
	                                    <a download="'.$row['url_1'].'" href="img/POD/'.$row['url_1'].'" title="ImageName">
                                         <img alt="ImageName" style="width: 80px;border: 2px solid red;" class="img-thumbnail" src="img/POD/'.$row['url_1'].'">
                                        </a>
                    </div>';
}else{
    $nestedData[] = '<div class="text-center"><span class="NoImage">No Image</span></div>';
}


if($row['url_4']!=''){
	$nestedData[] = '<div class="text-center">
	                                    <a download="'.$row['url_1'].'" href="img/POD/'.$row['url_1'].'" title="ImageName">
                                         <img alt="ImageName" style="width: 80px;border: 2px solid red;" class="img-thumbnail" src="img/POD/'.$row['url_1'].'">
                                        </a>
                    </div>';
}else{
    $nestedData[] = '<div class="text-center"><span class="NoImage">No Image</span></div>';
}
if($row['url_5']!=''){
	$nestedData[] = '<div class="text-center">
	                                    <a download="'.$row['url_1'].'" href="img/POD/'.$row['url_1'].'" title="ImageName">
                                         <img alt="ImageName" style="width: 80px;border: 2px solid red;" class="img-thumbnail" src="img/POD/'.$row['url_1'].'">
                                        </a>
                    </div>';
}else{
    $nestedData[] = '<div class="text-center"><span class="NoImage">No Image</span></div>';
}

if($session_id==1){
    $nestedData[] = '<a href="#" class="btn btn-danger btn-xs" data-href="pod-delete.php?id= '.$row['id'].' &pod_type='.$row['pod_type'].'" data-toggle="modal" data-target="#confirm-delete">
                        <i class="fa fa-trash"></i> Delete
                    </a>';
}else{
    $nestedData[] = '';
}
	
	
	
	$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

?>