<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
ini_set ("display_errors", "0");
error_reporting(E_ALL);
require("../connect.php");

// Data Transport Receipt
$stmtSPB = mysqli_query($conn, "SELECT id AS SPBID, field_949 AS SPBNUMBER, 
FROM_UNIXTIME(field_933, '%Y-%m-%d') AS PICKUPDATE, SUBSTRING(SEC_TO_TIME(field_1135), 4) AS PICKUPTIME, 
(SELECT field_309 FROM app_entity_31 WHERE id = field_950) AS DRIVER,
(SELECT field_328 FROM app_entity_32 WHERE id = field_951) AS VEHICLE,
IFNULL((SELECT field_1152 FROM app_entity_71 WHERE field_1205 = field_949), '') AS WEIGHBRIDGE
FROM app_entity_63 WHERE parent_item_id = ".$_GET['vehicleid']." AND FROM_UNIXTIME(field_933) >= DATE(NOW()) - INTERVAL 14 DAY;");

$dataSPB = array();
while ($rowSPB = mysqli_fetch_assoc($stmtSPB)) {
    
	if (strlen($rowSPB['WEIGHBRIDGE']) == 0) {
    	$nestedSPB[] = array_map('utf8_encode', array(
        	"SPBID" => $rowSPB['SPBID'],
        	"SPBNUMBER" => $rowSPB['SPBNUMBER'], 
        	"PICKUPDATE" => $rowSPB['PICKUPDATE'], 
        	"PICKUPTIME" => $rowSPB['PICKUPTIME'],
        	"DRIVER" => $rowSPB['DRIVER'],
        	"VEHICLE" => $rowSPB['VEHICLE'],
        	"WEIGHBRIDGE" => $rowSPB['WEIGHBRIDGE']
    	));

    	$dataSPB = array("DATASPB" => $nestedSPB);
    }
}

echo json_encode($dataSPB);

?>