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

$headers = [
    "JMB",
    "XLR",
    "LRG", 
    "MEG",
    "SML",
    "PUL",
    "PWE",
    "SPW",
    "B1",
    "B2",
    "B3",
    "Dirty"
];

echo "<table class='drBalance shadow-sm'>";
echo "<thead><tr>";
foreach ($headers as $headerText) {
    echo "<th class='bg-light text-uppercase align-baseline'>{$headerText}</th>";
}
echo "</tr></thead><tbody>";

$dr_customer_name = "PG 7 - CEBU";

$query = "SELECT 
        COALESCE(SUM(dr_jmb_trays), 0) as sum_jmb_trays,
        COALESCE(SUM(dr_xlr_trays), 0) as sum_xlr_trays,
        COALESCE(SUM(dr_lrg_trays), 0) as sum_lrg_trays,
        COALESCE(SUM(dr_med_trays), 0) as sum_med_trays,
        COALESCE(SUM(dr_sml_trays), 0) as sum_sml_trays,
        COALESCE(SUM(dr_pul_trays), 0) as sum_pul_trays,
        COALESCE(SUM(dr_pwe_trays), 0) as sum_pwe_trays,
        COALESCE(SUM(dr_spw_trays), 0) as sum_spw_trays,
        COALESCE(SUM(dr_b1_trays), 0) as sum_b1_trays,
        COALESCE(SUM(dr_b2_trays), 0) as sum_b2_trays,
        COALESCE(SUM(dr_b3_trays), 0) as sum_b3_trays,
        COALESCE(SUM(dr_dirty_trays), 0) as sum_dirty_trays
    FROM 
        transactions 
    WHERE 
        dr_customer_name = ? AND receiver IS NOT NULL AND received_date IS NOT NULL AND dr_status = 2 AND is_deleted = FALSE AND is_current = TRUE";
$result = executeQuery($conn, $query, "s", [$dr_customer_name]);

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

while ($row = mysqli_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>" . $row["sum_jmb_trays"] . "</td>";
    echo "<td>" . $row["sum_xlr_trays"] . "</td>";
    echo "<td>" . $row["sum_lrg_trays"] . "</td>";
    echo "<td>" . $row["sum_med_trays"] . "</td>";
    echo "<td>" . $row["sum_sml_trays"] . "</td>";
    echo "<td>" . $row["sum_pul_trays"] . "</td>";
    echo "<td>" . $row["sum_pwe_trays"] . "</td>";
    echo "<td>" . $row["sum_spw_trays"] . "</td>";
    echo "<td>" . $row["sum_b1_trays"] . "</td>";
    echo "<td>" . $row["sum_b2_trays"] . "</td>";
    echo "<td>" . $row["sum_b3_trays"] . "</td>";
    echo "<td>" . $row["sum_dirty_trays"] . "</td>";
    echo "</tr>";
} 

echo "</tbody></table>";
?>