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

$requestData= $_REQUEST;

$conditions='';

$from_date=$_POST['from_date'] ?? date('Y-m-d');
$to_date=$_POST['to_date'] ?? date('Y-m-d');
$type=$_POST['type'] ?? 1;

    $conditions.=" and tbl_delivery_status.date BETWEEN '$from_date' AND '$to_date'";
    
    

/*Fetch Database column Details*/
        $columns = array( 
            0 =>'id', 
            1 =>'id', 
            2 =>'id',
            3 =>'id'
        );
/*Fetch All Data Start*/        
        
    	$statement = $pdo->prepare("SELECT tbl_delivery_status.* FROM `tbl_delivery_status` where 1=1 ".$conditions);
		$statement->execute();
		$query = $statement->fetchAll(PDO::FETCH_ASSOC);
		$totalData = $statement->rowCount();
        $totalFiltered = $totalData;
/*Fetch All Data End*/       
        
/*Fetch Filter Data Start*/    
         $sql="SELECT tbl_delivery_status.* FROM `tbl_delivery_status` where 1=1 ".$conditions;
        if( !empty($requestData['search']['value']) ) 
        {   
        	$sql.=" AND ( tbl_delivery_status.delivery_status_no LIKE '%".$requestData['search']['value']."%' ";    
        	$sql.=" OR tbl_delivery_status.date 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']."   ";
       error_log($sql);
        $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'>";
	 $action.=" <a style='' href='delivery-run-sheet-print.php?id=".$row["id"]."' target='_blank' title='Print DRS' class='btn btn-warning btn-xs mr-2'><i class='fa fa-print'></i> Print Sheet</a>  ";
	 
	 $action.="</br><a style='margin-top: 5px;' href='drs-delivery-status-update.php?id=".$row["id"]."' target='_blank' title='Update Status' class='btn btn-info btn-xs mr-2'><i class='fa fa-map-marker'></i> Update Status</a>  ";
	 
		 	$action.="</div></div>";
	
	
 	$nestedData[] = $i;
 	$nestedData[] = $action;
 	$_details='';
        
        	
        if($row["status"]==0){	
            $s= "<a class='btn btn-danger btn-xs'><i class='fa fa-edit'></i> In-Active</a>";
        }else{
            $s= "<a class='btn btn-success btn-xs'><i class='fa fa-eye'></i> Active</a>";
        }
       
 		$bo=0;$wt=0;
 	    $statement = $pdo->prepare("SELECT tbl_delivery_status.*,
         (select sum(pickup_entry.boxes) from pickup_entry where pickup_entry.id in (".$row['shipment_ids'].")) as box,
         (select sum(a_weight) from pickup_entry where id in (".$row['shipment_ids'].")) as wt FROM `tbl_delivery_status` where id=".$row["id"]);
        $statement->execute();
		$qu = $statement->fetchAll(PDO::FETCH_ASSOC);
		if($qu){
		    $bo=$qu[0]['box'];
		    $wt=$qu[0]['wt'];
		}
 
 	$nestedData[] = "<span class='label_title'>".$row["delivery_status_no"]."</span>";
 	$nestedData[] = "<span class='label_title'>".nl2br($row["shipment_count"])."</span>";
 	$nestedData[] = "<span class='label_title'>".nl2br($bo)."</span>";
 	$nestedData[] = "<span class='label_title'>".nl2br($wt)."</span>";
 	$nestedData[] = "<span class='label_title'>".$row["date"]."</span>";
	$nestedData[] = $s;
	$data[] = $nestedData;
	$i++;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),  
			"recordsTotal"    => intval( $totalData ), 
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data  
			);

echo json_encode($json_data);  

?>