<?php
require_once('includes/load.php');
page_require_level(3);

$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-d'); // Default to today
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-d'); // Default to today
$sql = "SELECT id, resi, part_number, product_name, date_scan, quantity, scan_in FROM awb WHERE scan_in IS NOT NULL AND DATE(scan_in) BETWEEN '{$start_date}' AND '{$end_date}'ORDER BY scan_in DESC";
$result = $db->query($sql);

$scanned_awbs = [];
while ($row = $result->fetch_assoc()) {
    $scanned_awbs[] = $row;
}

$latest_awb = count($scanned_awbs) > 0 ? $scanned_awbs[0] : null;

echo json_encode(['scanned_awbs' => $scanned_awbs, 'latest_awb' => $latest_awb, 'current_date_time' => $current_date_time]);
