<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

$zdb    = $_SESSION["zdbyear_" . $dbname];
$compcd = $_SESSION["comp_"    . $dbname];
mysqli_select_db($conn, $zdb);

$vouchno = isset($_GET['vouchno']) ? (int)$_GET['vouchno'] : 0;
$res     = mysqli_query($conn, "SELECT * FROM `loadingplan` WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
$get     = mysqli_fetch_assoc($res) ?: [];

$tres  = mysqli_query($conn, "SELECT * FROM `loadingplantr` WHERE `vouchno`='$vouchno' ORDER BY `srl`") or die(mysqli_error($conn));
$lines = [];
while ($r = mysqli_fetch_assoc($tres)) { $lines[] = $r; }

$compRes  = mysqli_query($conn, "SELECT `name` FROM `$dbname`.`company` WHERE `code`='$compcd'");
$compRow  = $compRes ? mysqli_fetch_assoc($compRes) : null;
$compname = $compRow ? $compRow['name'] : '';

$dt = (!empty($get['vouchdt']) && $get['vouchdt'] != '0000-00-00') ? date('d-m-Y', strtotime($get['vouchdt'])) : '';

function pf($v) { $v=(float)$v; return $v==0?'':number_format($v,2); }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Loading Plan - <?php echo $vouchno; ?></title>
<style>
    body { font-family:Arial,sans-serif; font-size:12px; color:#1e293b; margin:16px; }
    h2 { text-align:center; margin:0 0 2px; font-size:16px; }
    .sub { text-align:center; color:#64748b; font-size:12px; margin-bottom:12px; }
    .hdr-table { width:100%; border-collapse:collapse; margin-bottom:10px; }
    .hdr-table td { padding:3px 8px; font-size:12px; }
    .hdr-label { color:#64748b; font-weight:700; width:110px; }
    table.reg { width:100%; border-collapse:collapse; margin-top:6px; font-size:10.5px; }
    table.reg th { background:#f1f5f9; padding:6px 5px; border:1px solid #cbd5e1; text-align:center; font-weight:700; font-size:10px; white-space:nowrap; }
    table.reg td { border:1px solid #e2e8f0; padding:5px 5px; }
    td.num { text-align:right; }
    tfoot td { font-weight:700; background:#f8fafc; }
    .no-print { display:block; text-align:center; margin-top:14px; }
    @media print { .no-print { display:none; } }
</style>
</head>
<body>
    <h2><?php echo htmlspecialchars($compname); ?></h2>
    <div class="sub">Loading Plan</div>

    <table class="hdr-table">
        <tr>
            <td class="hdr-label">Voucher No</td><td><?php echo $vouchno; ?></td>
            <td class="hdr-label">Date</td><td><?php echo $dt; ?></td>
        </tr>
        <tr>
            <td class="hdr-label">Container No.</td><td><?php echo htmlspecialchars($get['containerno'] ?? ''); ?></td>
            <td class="hdr-label">Shipper</td><td><?php echo htmlspecialchars($get['shipper'] ?? ''); ?></td>
        </tr>
        <?php if (!empty($get['remarks'])): ?>
        <tr>
            <td class="hdr-label">Remarks</td><td colspan="3"><?php echo htmlspecialchars($get['remarks']); ?></td>
        </tr>
        <?php endif; ?>
    </table>

    <table class="reg">
        <thead>
            <tr>
                <th>#</th>
                <th style="text-align:left;">WeChat ID</th>
                <th style="text-align:left;">Ship Mark</th>
                <th style="text-align:left;">Description</th>
                <th class="num">CTN</th>
                <th class="num">T.QTY</th>
                <th class="num">AMT</th>
                <th class="num">OUR</th>
                <th class="num">JOHN INV.</th>
                <th class="num">CREDIT</th>
                <th class="num">BALANCE</th>
                <th class="num">CBM</th>
                <th class="num">WEIGHT</th>
                <th class="num">CTN*</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($lines as $i => $ln): ?>
            <tr>
                <td style="text-align:center;"><?php echo $i+1; ?></td>
                <td><?php echo htmlspecialchars($ln['wechatid']); ?></td>
                <td><?php echo htmlspecialchars($ln['shipmark']); ?></td>
                <td><?php echo htmlspecialchars($ln['description']); ?></td>
                <td class="num"><?php echo pf($ln['ctn']); ?></td>
                <td class="num"><?php echo pf($ln['tqty']); ?></td>
                <td class="num"><?php echo pf($ln['amt']); ?></td>
                <td class="num"><?php echo pf($ln['our']); ?></td>
                <td class="num"><?php echo pf($ln['john']); ?></td>
                <td class="num"><?php echo pf($ln['credit']); ?></td>
                <td class="num"><?php echo pf($ln['balance']); ?></td>
                <td class="num"><?php echo pf($ln['cbm']); ?></td>
                <td class="num"><?php echo pf($ln['weight']); ?></td>
                <td class="num"><?php echo pf($ln['extractn']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <?php
            $tc=$ttq=$ta=$to=$tj=$tcr=$tbal=$tcbm=$twt=$txctn=0;
            foreach ($lines as $ln) {
                $tc+=(float)$ln['ctn']; $ttq+=(float)$ln['tqty']; $ta+=(float)$ln['amt'];
                $to+=(float)$ln['our']; $tj+=(float)$ln['john']; $tcr+=(float)$ln['credit'];
                $tbal+=(float)$ln['balance']; $tcbm+=(float)$ln['cbm']; $twt+=(float)$ln['weight'];
                $txctn+=(float)$ln['extractn'];
            }
            ?>
            <tr>
                <td colspan="4" class="num">Total</td>
                <td class="num"><?php echo pf($tc); ?></td>
                <td class="num"><?php echo pf($ttq); ?></td>
                <td class="num"><?php echo pf($ta); ?></td>
                <td class="num"><?php echo pf($to); ?></td>
                <td class="num"><?php echo pf($tj); ?></td>
                <td class="num"><?php echo pf($tcr); ?></td>
                <td class="num"><?php echo pf($tbal); ?></td>
                <td class="num"><?php echo pf($tcbm); ?></td>
                <td class="num"><?php echo pf($twt); ?></td>
                <td class="num"><?php echo pf($txctn); ?></td>
            </tr>
        </tfoot>
    </table>

    <div class="no-print">
        <button onclick="window.print()" style="padding:8px 20px;border:none;border-radius:6px;background:#2563eb;color:#fff;cursor:pointer;font-size:13px;">Print</button>
    </div>
</body>
</html>
