<?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];
$zdbroot = $_SESSION["zdbroot"] = $dbname;   // sim_root
mysqli_select_db($conn, $zdb);

$vouchno = isset($_GET['vouchno']) ? (int)$_GET['vouchno'] : 0;

$result = mysqli_query($conn,
    "SELECT p.*, c.name AS customername, s.name AS suppliername, g.name AS godownname
     FROM `purchase` p
     LEFT JOIN `accmast` c ON c.accode = p.customer
     LEFT JOIN `accmast` s ON s.accode = p.supplier
     LEFT JOIN `godown` g ON g.code = p.godown
     WHERE p.vouchno='$vouchno'") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);
if (!$get) $get = [];

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

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

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

require_once("includes/upload_path.php");
$uploadurl = upload_path('purchase')['url'];

function pf($v) {
    if ($v === null || $v === '') return '';
    $v = (float)$v;
    return $v == 0 ? '' : (($v == (int)$v) ? (string)(int)$v : number_format($v, 2));
}

// totals
$totctn = $totqty = $totamt = $totcbm = $totweight = 0;
foreach ($lines as $ln) {
    $totctn    += (float)$ln['ctn'];
    $totqty    += (float)$ln['qty'];
    $totamt    += (float)$ln['amt'];
    $totcbm    += (float)$ln['tcbm'];
    $totweight += (float)$ln['tweight'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Packing List - <?php echo $vouchno; ?></title>
<style>
    body { font-family: Arial, sans-serif; font-size:13px; color:#1e293b; margin:20px; }
    h2 { text-align:center; margin:0 0 2px; }
    h3 { text-align:center; margin:0 0 4px; letter-spacing:.06em; color:#475569; font-size:15px; }
    .sub { text-align:center; color:#64748b; margin-bottom:16px; }
    table { width:100%; border-collapse:collapse; margin-top:10px; }
    th, td { border:1px solid #cbd5e1; padding:5px 6px; font-size:11px; }
    th { background:#f1f5f9; text-align:center; }
    td.num { text-align:right; }
    .hdr-table td { border:none; padding:3px 6px; font-size:12.5px; }
    .hdr-label { color:#64748b; font-weight:600; width:120px; }
    tfoot td { font-weight:700; background:#f8fafc; }
    .shipmark-box {
        border:2px solid #1e293b; padding:14px 24px; display:inline-block;
        font-size:20px; font-weight:700; letter-spacing:.05em; margin:6px 0 16px;
    }
    .shipmark-wrap { text-align:center; }
    .pic-thumb { width:46px; height:46px; object-fit:cover; border-radius:3px; border:1px solid #cbd5e1; }
    @media print { .no-print { display:none; } }
</style>
</head>
<body>
    <h2><?php echo htmlspecialchars($compname); ?></h2>
    <h3>PACKING LIST</h3>
    <div class="sub">Purchase Voucher No. <?php echo $vouchno; ?> &nbsp;|&nbsp; Date: <?php echo htmlspecialchars($vouchdt_disp); ?></div>

    <?php if (!empty($get['shipmarka'])): ?>
    <div class="shipmark-wrap">
        <div class="shipmark-box"><?php echo htmlspecialchars($get['shipmarka']); ?></div>
    </div>
    <?php endif; ?>

    <table class="hdr-table">
        <tr>
            <td class="hdr-label">Supplier</td><td><?php echo htmlspecialchars($get['suppliername'] ?? ''); ?></td>
            <td class="hdr-label">Customer</td><td><?php echo htmlspecialchars($get['customername'] ?? ''); ?></td>
        </tr>
        <tr>
            <td class="hdr-label">Godown</td><td><?php echo htmlspecialchars($get['godownname'] ?? ''); ?></td>
            <td class="hdr-label">Remarks</td><td><?php echo htmlspecialchars($get['remarks'] ?? ''); ?></td>
        </tr>
    </table>

    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Image</th>
                <th>Marker</th>
                <th>Description</th>
                <th>Model No.</th>
                <th>CTN</th>
                <th>QTY</th>
                <th>T.QTY</th>
                <th>Unit</th>
                <th>Price</th>
                <th>Amount</th>
                <th>CBM</th>
                <th>T.CBM</th>
                <th>Weight</th>
                <th>T.Weight</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($lines as $i => $ln): ?>
            <tr>
                <td style="text-align:center;"><?php echo $i + 1; ?></td>
                <td style="text-align:center;">
                    <?php if (!empty($ln['picfile'])): ?>
                    <img class="pic-thumb" src="<?php echo $uploadurl . htmlspecialchars($ln['picfile']); ?>" alt="">
                    <?php endif; ?>
                </td>
                <td><?php echo htmlspecialchars($ln['marka']); ?></td>
                <td><?php echo htmlspecialchars($ln['description']); ?></td>
                <td><?php echo htmlspecialchars($ln['modelno']); ?></td>
                <td class="num"><?php echo pf($ln['ctn']); ?></td>
                <td class="num"><?php echo pf($ln['qty']); ?></td>
                <td class="num"><?php echo pf($ln['tqty']); ?></td>
                <td style="text-align:center;">PCS</td>
                <td class="num"><?php echo pf($ln['rate']); ?></td>
                <td class="num"><?php echo pf($ln['amt']); ?></td>
                <td class="num"><?php echo pf($ln['cbm']); ?></td>
                <td class="num"><?php echo pf($ln['tcbm']); ?></td>
                <td class="num"><?php echo pf($ln['weight']); ?></td>
                <td class="num"><?php echo pf($ln['tweight']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="5" style="text-align:right;">Total</td>
                <td class="num"><?php echo pf($totctn); ?></td>
                <td class="num"><?php echo pf($totqty); ?></td>
                <td></td>
                <td></td>
                <td></td>
                <td class="num"><?php echo pf($totamt); ?></td>
                <td></td>
                <td class="num"><?php echo pf($totcbm); ?></td>
                <td></td>
                <td class="num"><?php echo pf($totweight); ?></td>
            </tr>
        </tfoot>
    </table>

    <div class="no-print" style="margin-top:16px;text-align:center;">
        <button onclick="window.print()" style="padding:8px 18px;border:none;border-radius:6px;background:#0891b2;color:#fff;cursor:pointer;">Print</button>
    </div>
</body>
</html>
