<?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];
mysqli_select_db($conn, $zdb);
$frmdate = $_POST['frmdate'] ?? date('Y-m-01');
$todate  = $_POST['todate']  ?? date('Y-m-d');
$agent   = (int)($_POST['agent'] ?? 0);
$where = "WHERE lp.vouchdt BETWEEN '$frmdate' AND '$todate'
          AND lp.containerno != ''
          AND lp.containerno NOT IN (SELECT containerno FROM despatch WHERE containerno != '')";
if ($agent) $where .= " AND lp.shipper='$agent'";
$res = mysqli_query($conn,
    "SELECT lp.vouchno, lp.vouchdt, lp.containerno, a.name AS agentname,
            lp.totctn, lp.totamt, lp.totcbm, lp.totweight,
            DATEDIFF(CURDATE(), lp.vouchdt) AS days_old
     FROM loadingplan lp
     LEFT JOIN accmast a ON a.accode=lp.shipper
     $where ORDER BY lp.vouchno DESC") or die(mysqli_error($conn));
$rows=[]; while($r=mysqli_fetch_assoc($res)) $rows[]=$r;
?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Pending Despatch</title>
<?php include("includes/rpt_style.php"); ?>
</head><body>
<div class="rpt-wrap">
<div class="rpt-header">
    <div class="rpt-title"><i class="fa fa-truck" style="color:#8b5cf6;"></i> Pending Despatch</div>
    <div class="rpt-meta"><?php echo date('d-m-Y',strtotime($frmdate)); ?> to <?php echo date('d-m-Y',strtotime($todate)); ?> &nbsp;·&nbsp; <?php echo count($rows); ?> records</div>
</div>
<?php if (!$rows): ?><div class="empty">No pending despatches found.</div><?php else: ?>
<table>
    <thead><tr><th>#</th><th>LP#</th><th>Date</th><th>Days Old</th><th>Container No.</th><th>Shipper/Agent</th><th class="num">CTN</th><th class="num">Amount</th><th class="num">CBM</th><th class="num">Weight</th></tr></thead>
    <tbody>
    <?php $tC=$tA=$tCbm=$tWt=0; foreach($rows as $i=>$r):
        $tC+=(float)$r['totctn']; $tA+=(float)$r['totamt']; $tCbm+=(float)$r['totcbm']; $tWt+=(float)$r['totweight'];
        $age=(int)$r['days_old']; $ageCls=$age>30?'br':($age>14?'ba':'bg');
    ?>
    <tr>
        <td><?php echo $i+1; ?></td>
        <td><span class="badge bp">LP#<?php echo $r['vouchno']; ?></span></td>
        <td><?php echo $r['vouchdt']?date('d-m-Y',strtotime($r['vouchdt'])):''; ?></td>
        <td class="ctr"><span class="badge <?php echo $ageCls; ?>"><?php echo $age; ?> days</span></td>
        <td style="font-weight:600;"><?php echo htmlspecialchars($r['containerno']); ?></td>
        <td><?php echo htmlspecialchars($r['agentname']??''); ?></td>
        <td class="num"><?php echo number_format((float)$r['totctn'],0); ?></td>
        <td class="num">¥<?php echo number_format((float)$r['totamt'],2); ?></td>
        <td class="num"><?php echo number_format((float)$r['totcbm'],3); ?></td>
        <td class="num"><?php echo number_format((float)$r['totweight'],2); ?></td>
    </tr>
    <?php endforeach; ?>
    </tbody>
    <tfoot><tr><td colspan="6" class="num">Total</td><td class="num"><?php echo number_format($tC,0); ?></td><td class="num">¥<?php echo number_format($tA,2); ?></td><td class="num"><?php echo number_format($tCbm,3); ?></td><td class="num"><?php echo number_format($tWt,2); ?></td></tr></tfoot>
</table>
<?php endif; ?>
</div>
<div class="rpt-actions">
    <button class="btn-print" onclick="window.print()"><i class="fa fa-print"></i> Print</button>
    <button class="btn-back" onclick="if(window.parent&&window.parent!==window){window.parent.document.getElementById('rptframe').src='about:blank';}else{window.close();}"><i class="fa fa-arrow-left"></i> Go Back</button>
</div>
</body></html>
