<?php
ob_start();
require("dbconnect.php");
require("config.php");
require("function.php");
if (!isset($_SESSION["username_".$dbname])) { echo json_encode(['html'=>'']); exit; }
$zdb = $_SESSION["zdbyear_".$dbname];
mysqli_select_db($conn, $zdb);
$cn = isset($_POST['containerno']) ? mysqli_real_escape_string($conn, trim($_POST['containerno'])) : '';
header('Content-Type: application/json');
if (!$cn) { echo json_encode(['html'=>'<tr><td colspan="17" style="text-align:center;padding:16px;color:#94a3b8;">Select a container.</td></tr>']); exit; }

$res = mysqli_query($conn,
    "SELECT lt.*, s.name AS supname, c.name AS custname
     FROM loadingplantr lt
     INNER JOIN loadingplan lp ON lp.vouchno = lt.vouchno
     LEFT JOIN accmast s ON s.accode = lt.supplier
     LEFT JOIN accmast c ON c.accode = lt.customer
     WHERE lp.containerno='$cn' ORDER BY lt.srl") or die(mysqli_error($conn));

function pfs($v){ $v=(float)$v; return $v?number_format($v,2):''; }
$rows = []; $html = '';
$stCtn=$stTqty=$stAmt=$stAdv=$stOur=$stShp=$stCr=$stBal=$stCbm=$stWt=$stCry=0;
$i=0;
while ($sl = mysqli_fetch_assoc($res)) {
    $i++;
    $stCtn+=$sl['ctn']; $stTqty+=$sl['tqty']; $stAmt+=$sl['amt'];
    $stAdv+=$sl['advance']; $stOur+=$sl['our']; $stShp+=$sl['shipperamount'];
    $stCr+=$sl['credit']; $stBal+=$sl['balance']; $stCbm+=$sl['cbm'];
    $stWt+=$sl['weight']; $stCry+=$sl['cry'];
    $bcBadge = $sl['bcvouchno']>0 ? '<span style="background:#ede9fe;color:#5b21b6;padding:1px 6px;border-radius:8px;font-size:10px;font-weight:700;">BC#'.$sl['bcvouchno'].'</span>' : '';
    $html .= '<tr style="border-bottom:1px solid #f1f5f9;">'
        . '<td style="text-align:center;color:#94a3b8;font-size:11px;padding:6px 4px;">'.$i.'</td>'
        . '<td style="padding:6px 5px;font-size:11px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="'.htmlspecialchars($sl['supname']??'').'">'.htmlspecialchars($sl['supname']??'').'</td>'
        . '<td style="padding:6px 5px;font-size:11px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="'.htmlspecialchars($sl['custname']??'').'">'.htmlspecialchars($sl['custname']??'').'</td>'
        . '<td style="padding:6px 4px;text-align:center;">'.$bcBadge.'</td>'
        . '<td style="padding:6px 5px;font-size:11px;">'.htmlspecialchars($sl['shipmark']).'</td>'
        . '<td style="padding:6px 5px;font-size:11px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="'.htmlspecialchars($sl['description']).'">'.htmlspecialchars($sl['description']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['ctn']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['tqty']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;font-weight:600;">'.pfs($sl['amt']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['advance']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['our']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['shipperamount']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['credit']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;font-weight:600;color:#065f46;">'.pfs($sl['balance']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['cbm']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['weight']).'</td>'
        . '<td style="padding:6px 4px;text-align:right;font-size:11px;">'.pfs($sl['cry']).'</td>'
        . '</tr>';
}
if ($i > 0) {
    $html .= '<tr style="background:#f1f5f9;border-top:2px solid #e2e8f0;font-weight:700;">'
        . '<td colspan="6" style="padding:7px 8px;text-align:right;font-size:11px;color:#64748b;">Total</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stCtn).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stTqty).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stAmt).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stAdv).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stOur).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stShp).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stCr).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;color:#065f46;">'.pfs($stBal).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stCbm).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stWt).'</td>'
        . '<td style="padding:7px 4px;text-align:right;font-size:11.5px;">'.pfs($stCry).'</td>'
        . '</tr>';
} else {
    $html = '<tr><td colspan="17" style="text-align:center;padding:16px;color:#94a3b8;">No Loading Plan lines found for this container.</td></tr>';
}
echo json_encode(['html' => $html]);
