<?php 
require 'Prozigzig.php';
$h = new Prozigzig();
header('Content-type: application/json');

$data = json_decode(file_get_contents('php://input'), true);

if (empty($data['uid'])) {
    $returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went Wrong!");
    echo json_encode($returnArr);
    exit;
}

$uid = intval($data['uid']);
$parcel_id = $data['parcel_id'] ?? ''; // optional filter
$parcelList = [];

$whereStatus = '';

$row = $h->queryfire("
    SELECT * FROM tbl_parcel 
    WHERE id = $parcel_id  
    ORDER BY id DESC
")->fetch_assoc();


    // Fetch related bus info
    $bus = $h->queryfire("SELECT title, bno, bus_img FROM tbl_bus WHERE id = " . intval($row['bus_id']))->fetch_assoc();

    // Fetch from & to city titles
    $fromCity = $h->queryfire("SELECT title FROM tbl_city WHERE id = " . intval($row['from_city']))->fetch_assoc();
    $toCity = $h->queryfire("SELECT title FROM tbl_city WHERE id = " . intval($row['to_city']))->fetch_assoc();
    $pname = $h->queryfire("select * from tbl_payment_list where id=".$row['p_method_id']."")->fetch_assoc();
	
    $parcelList[] = array(
        "parcel_id" => $row['id'],
        "bus_id" => $row['bus_id'],
        "bus_title" => $bus['title'] ?? '',
        "bus_no" => $bus['bno'] ?? '',
        "bus_img" => $bus['bus_img'] ?? '',
        "trip_date" => $row['trip_date'],
        "sender_name" => $row['sender_name'],
        "sender_mobile" => $row['sender_mobile'],
        "receiver_name" => $row['receiver_name'],
        "receiver_mobile" => $row['receiver_mobile'],
		"wall_amt" => $row['wall_amt'],
        "from_city" => $fromCity['title'] ?? '',
        "to_city" => $toCity['title'] ?? '',
        "parcel_weight" => $row['parcel_weight'],
        "price" => $row['price'],
        "status" => $row['status'],
        "created_at" => $row['created_at'],
        "price_per_km" => $row['price_per_km'],
        "distance" => $row['distance'],
		"transaction_id" =>$row['transaction_id'],
		"p_method_name"=>$pname['title']
    );


$returnArr = array(
    "ResponseCode" => "200",
    "Result" => "true",
    "ResponseMsg" => "Parcel details fetched successfully!",
    "parcel_history" => $parcelList
);

echo json_encode($returnArr);