<?php

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

if(isset($_POST["id"])) {
    
     $name = $_POST["id"];
     $type = $_POST["type"];
                           
                            // $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,(SELECT location from tbl_tracking WHERE pickup_entry_id=pickup_entry.id ORDER by pickup_entry_id DESC LIMIT 1) tracking_location,(SELECT created_at from tbl_tracking WHERE pickup_entry_id=pickup_entry.id ORDER by pickup_entry_id DESC LIMIT 1) tracking_date FROM `pickup_entry` INNER JOIN tbl_user ON pickup_entry.client_id=tbl_user.id  WHERE pickup_entry.3pl_awb like '%".$name."%' order by pickup_entry.id asc limit 3");
                            $statement = $pdo->prepare("SELECT pickup_entry.*,tbl_user.full_name as client_name,(SELECT location from tbl_tracking WHERE pickup_entry_id=pickup_entry.id ORDER by pickup_entry_id DESC LIMIT 1) tracking_location,(SELECT created_at from tbl_tracking WHERE pickup_entry_id=pickup_entry.id ORDER by pickup_entry_id DESC LIMIT 1) tracking_date FROM `pickup_entry` INNER JOIN tbl_user ON pickup_entry.client_id=tbl_user.id  WHERE pickup_entry.".$type." like '%".$name."%' order by pickup_entry.id asc limit 3");
							$statement->execute();
							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
							$count=$statement->rowCount();
							if($count==1)
							{
							     foreach ($result as $row) 
							     {
                                    $id = $row['id'];
                                    $pl3_awb = $row['3pl_awb'];
                                    $cscs_awb = $row['cscs_awb'];
                                    $ref_no = $row['ref_no'];
                                    $pics = $row['quantity'];
                                    $address = $row['consignee_address'];
                                    $phone = $row['consignee_phone'];
                                    $c_weight = $row['c_weight'];
                                    $consignee_city = $row['consignee_city'];
                                    $client_id = $row['client_name'];
                                    // $created_at = $row['created_at'];
                                    $created_at=date_create($row['date_added']);
                                    $created_at=date_format($created_at,'d-m-Y'); 
                                    $tstatus = $row['status'];
                                    $tracking_location=$row['tracking_location'];
                                    // $tracking_date=$row['tracking_date'];
                                    $tracking_date=date_create($row['tracking_date']);
                                    $tracking_date=date_format($tracking_date,'d-m-Y'); 
                                    
                                 } 
                                 $status="Success";
                                 $datas[] = array('id' => $id,'status'=>$status,'3pl_awb'=>$pl3_awb,'ref_no'=>$ref_no,'cscs_awb'=>$cscs_awb,'pics'=>$pics,'address'=>$address,'phone'=>$phone,'c_weight'=>$c_weight,'destination'=>$consignee_city,'client_id'=>$client_id,'created'=>$created_at,'tracking_location'=>$tracking_location,'tracking_date'=>$tracking_date,'tstatus'=>$tstatus);
                                echo json_encode($datas); 
							}
							elseif($count==0)
							{
							   $id=0;
							   $status="Invalid AWB Number";
							    $datas[] = array('id' => $id,'status'=>$status);
                                echo json_encode($datas);
							}
							else
							{
							    foreach ($result as $row) 
							     {
							       $id = $row['id'];
                                    $pl3_awb = $row['3pl_awb'];
                                    $cscs_awb = $row['cscs_awb'];
                                    $ref_no = $row['ref_no'];
                                    $pics = $row['quantity'];
                                    $address = $row['consignee_address'];
                                    $phone = $row['consignee_phone'];
                                    $c_weight = $row['c_weight'];
                                    $consignee_city = $row['consignee_city'];
                                    $client_id = $row['client_name'];
                                    // $created_at = $row['created_at'];
                                    $created_at=date_create($row['date_added']);
                                    $created_at=date_format($created_at,'d-m-Y');
                                    $tstatus = $row['status'];
                                    $tracking_location=$row['tracking_location'];
                                    // $tracking_date=$row['tracking_date'];
                                    $tracking_date=date_create($row['tracking_date']);
                                    $tracking_date=date_format($tracking_date,'d-m-Y');
                                    $status=2;
                                 $datas[] = array('id' => $id,'status'=>$status,'3pl_awb'=>$pl3_awb,'ref_no'=>$ref_no,'cscs_awb'=>$cscs_awb,'pics'=>$pics,'address'=>$address,'phone'=>$phone,'c_weight'=>$c_weight,'destination'=>$consignee_city,'client_id'=>$client_id,'created'=>$created_at,'tracking_location'=>$tracking_location,'tracking_date'=>$tracking_date,'tstatus'=>$tstatus);
							     }
							     echo json_encode($datas);
							}
							
							 
}

?>
