<?php
require_once("dbconn.php");
require_once("phpFunctions.php");

$data_id = isset($_GET["data_id"]) ? $_GET["data_id"] : null;

$result = executeQuery($conn, "SELECT * FROM pricelist WHERE id = ?", "i", [$data_id]);

if ($result === false) { 
	throw new Exception("Error executing query: " . $conn->error);
} 

if ($result->num_rows > 0) {
	while($row = $result->fetch_assoc()) 
	{
		$response["pl_id"] = $row["id"];
		$response["pl_effectivity_date"] = $row["pl_effectivity_date"];
		$response["pl_customer_name"] = $row["pl_customer_name"];
		$response["pl_eggtray_charge"] = isset($row["pl_eggtray_charge"]) && $row["pl_eggtray_charge"] !== 0 ? $row["pl_eggtray_charge"] : "";
		$response["pl_shipping_charge"] = isset($row["pl_shipping_charge"]) && $row["pl_shipping_charge"] !== 0 ? $row["pl_shipping_charge"] : "";
		
		$response["pl_other_charges"] = isset($row["pl_other_charges"]) && $row["pl_other_charges"] !== 0 ? $row["pl_other_charges"] : "";
		$response["pl_other_charges_remark"] = $row["pl_other_charges_remark"];
		
		$response["pl_JMB"] = isset($row["pl_JMB"]) && $row["pl_JMB"] !== 0 ? $row["pl_JMB"] : "";
		$response["pl_XLR"] = isset($row["pl_XLR"]) && $row["pl_XLR"] !== 0 ? $row["pl_XLR"] : "";
		$response["pl_LRG"] = isset($row["pl_LRG"]) && $row["pl_LRG"] !== 0 ? $row["pl_LRG"] : "";
		$response["pl_MED"] = isset($row["pl_MED"]) && $row["pl_MED"] !== 0 ? $row["pl_MED"] : "";
		$response["pl_SML"] = isset($row["pl_SML"]) && $row["pl_SML"] !== 0 ? $row["pl_SML"] : "";
		$response["pl_PUL"] = isset($row["pl_PUL"]) && $row["pl_PUL"] !== 0 ? $row["pl_PUL"] : "";
		$response["pl_PWE"] = isset($row["pl_PWE"]) && $row["pl_PWE"] !== 0 ? $row["pl_PWE"] : "";
		$response["pl_SPW"] = isset($row["pl_SPW"]) && $row["pl_SPW"] !== 0 ? $row["pl_SPW"] : "";
		$response["pl_B1"] = isset($row["pl_B1"]) && $row["pl_B1"] !== 0 ? $row["pl_B1"] : "";
		$response["pl_B2"] = isset($row["pl_B2"]) && $row["pl_B2"] !== 0 ? $row["pl_B2"] : "";
		$response["pl_B3"] = isset($row["pl_B3"]) && $row["pl_B3"] !== 0 ? $row["pl_B3"] : "";
		$response["pl_Dirty"] = isset($row["pl_Dirty"]) && $row["pl_Dirty"] !== 0 ? $row["pl_Dirty"] : "";
	}
} 

if (empty($response)) {
	echo json_encode(["status" => "error", "message" => "ipRowData - An error occurred while processing your request."]);
} else {
    echo json_encode($response);
}
?>