<?php
global $db, $config, $auth;

$basePath = rtrim(($config['app']['base_path'] ?? ''), '/');
$houseId  = (int)($_GET['house_bl_id'] ?? 0);

$servedBy = '';
if (isset($auth) && $auth instanceof Auth) {
  $authUser = $auth->user();
  if (is_array($authUser) && !empty($authUser['full_name'])) {
    $servedBy = strtoupper(trim((string)$authUser['full_name']));
  }
}
if ($servedBy === '' && !empty($_SESSION['uid'])) {
  try {
    $uStmt = $db->prepare("SELECT full_name FROM users WHERE id = ? LIMIT 1");
    $uStmt->execute([(int)$_SESSION['uid']]);
    $uRow = $uStmt->fetch();
    if ($uRow && !empty($uRow['full_name'])) {
      $servedBy = strtoupper(trim((string)$uRow['full_name']));
    }
  } catch (Throwable $e) {
    $servedBy = '';
  }
}

$stmt = $db->prepare("
  SELECT
    h.id,
    h.hbl_no,
    h.goods_description,
    h.packages,
    h.gross_weight_kg,
    h.cbm,

    m.mbl_no,
    m.vessel_name,
    m.voyage_no,
    m.container_no,
    m.shipping_line_id,

    sl.name AS shipper_name,

    pol.name AS pol_name,
    pod.name AS pod_name,

    c1.name AS consignee_name,
    c2.name AS billing_name

  FROM house_bls h
  JOIN master_bls m ON m.id = h.master_bl_id
  LEFT JOIN shipping_lines sl ON sl.id = m.shipping_line_id
  LEFT JOIN ports pol ON pol.id = m.pol_port_id
  LEFT JOIN ports pod ON pod.id = m.pod_port_id
  JOIN customers c1 ON c1.id = h.consignee_customer_id
  JOIN customers c2 ON c2.id = h.billing_party_customer_id
  WHERE h.id = ?
");
$stmt->execute([$houseId]);
$h = $stmt->fetch();

if (!$h) {
  echo "<div class='glass rounded-3xl p-5'>HBL not found.</div>";
  return;
}

if (!function_exists('n2')) { function n2($v){ return number_format((float)$v, 2); } }
if (!function_exists('n3')) { function n3($v){ return number_format((float)$v, 3); } }

$dateStr = date('d/m/Y');

$consignee = (string)($h['consignee_name'] ?? '');
$shipper   = (string)($h['shipper_name'] ?? '');
$desc      = (string)($h['goods_description'] ?? '');
$pkgs      = (string)($h['packages'] ?? '');
$weightKg  = (float)($h['gross_weight_kg'] ?? 0);
$cbm       = (float)($h['cbm'] ?? 0);

$blNo      = trim((string)($h['hbl_no'] ?? ''));
$container = trim((string)($h['container_no'] ?? ''));
$vesselVoy = trim((string)($h['vessel_name'] ?? ''));
$voyage    = trim((string)($h['voyage_no'] ?? ''));
if ($voyage !== '') $vesselVoy = trim($vesselVoy . ' / ' . $voyage);

$loadPort  = (string)($h['pol_name'] ?? '');
$destPort  = (string)($h['pod_name'] ?? '');
?>

<style>
body .max-w-7xl > .glass-strong > .flex.items-center.justify-between {
  display: none !important;
}

body .max-w-7xl {
  max-width: none !important;
  margin: 0 !important;
  padding: 0 !important;
}
body .max-w-7xl > .glass-strong {
  border: 0 !important;
  box-shadow: none !important;
  border-radius: 0 !important;
}
body .max-w-7xl > .glass-strong > .px-6.py-6 {
  padding: 0 !important;
}

html, body {
  background: #fff !important;
}

header,
nav,
.app-header,
.app-navbar,
.topbar,
.user-menu {
  display: none !important;
}

body { margin-top: 0 !important; }

.doc-wrap{
  background:#fff;
  color:#000;
  font-family: Arial, Helvetica, sans-serif;
  padding:0;
  box-sizing:border-box;
}

.doc-sheet{
  max-width:900px;
  width:900px;
  margin-left:1cm;
  border:3px solid #f97316;
  padding:18px;
  box-sizing:border-box;
  min-height:297mm;
  position:relative;
}

.doc-actions{
  display:flex;
  justify-content:flex-end;
  gap:8px;
  margin-bottom:8px;
}

.btn{
  padding:8px 10px;
  border:1px solid #000;
  background:#fff;
  color:#000;
  border-radius:6px;
  cursor:pointer;
  font-size:12px;
  text-decoration:none;
  display:inline-block;
}

.doc-banner{
  display:flex;
  justify-content:flex-start;
  margin-bottom:8px;
}

.banner{
  width:auto;
  max-width:520px;
  height:auto;
  display:block;
  margin:0;
}

.doc-divider{
  height:0;
  border-top:3px solid #f97316;
  margin:8px -18px 14px;
}

.doc-title{
  margin:0 0 14px 0;
  font-weight:700;
  letter-spacing:.5px;
  color:#f59e0b;
  font-size:26px;
}

.doc-date{
  font-size:14px;
  margin:0 0 14px 0;
}

.doc-p{
  font-size:14px;
  line-height:1.5;
  margin:0 0 14px 0;
}

table.tbl{
  width:100%;
  border-collapse:collapse;
  margin:12px 0 16px;
  font-size:13px;
}

.tbl td{
  border:2px solid #1f2937;
  padding:6px 8px;
  vertical-align:top;
}

.k{
  width:160px;
  font-weight:700;
  text-transform:uppercase;
  background:#f3f4f6;
}

.v{ width:auto; }

.closing{
  margin-top:26px;
  font-size:14px;
  line-height:1.45;
}

.audit-footer{
  margin-top:28px;
  padding-top:8px;
  border-top:1px dashed #d1d5db;
  font-size:11px;
  color:#6b7280;
  text-align:right;
}

@media print{
  .doc-actions{ display:none !important; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

  .doc-sheet{
    margin-left:1cm;
    width:auto;
    max-width:none;
    padding:12px 12px 0 12px;
    border:3px solid #f97316;
    min-height:255mm;
    box-sizing:border-box;
    margin:0 !important;
    padding:12px !important;
  }

  .doc-divider{
    height:0 !important;
    border-top:3px solid #f97316 !important;
    margin:8px -12px 14px -12px !important;
  }

  .banner{
    max-height:60px;
    object-fit:contain;
  }
}
</style>

<div class="doc-wrap">
  <div class="doc-sheet">

    <div class="doc-actions">
      <button class="btn" onclick="window.print()">Print</button>
    </div>

    <div class="doc-banner">
      <img class="banner" src="<?= h((string)$basePath) ?>/assets/wcs-banner.png" alt="WSC Banner" />
    </div>

    <div class="doc-divider"></div>

    <div class="doc-title">ARRIVAL ADVICE</div>

    <div class="doc-date"><?= h((string)$dateStr) ?></div>

    <p class="doc-p"><strong>Dear Valued Customer,</strong></p>
    <p class="doc-p">
      Thank you for trusting WSC Logistics Company for your shipping services.<br>
      We are pleased to notify you of the arrival of your shipment.
    </p>

    <table class="tbl">
      <tr><td class="k">CONSIGNEE</td><td class="v"><?= h((string)$consignee) ?></td></tr>
      <tr><td class="k">SHIPPER</td><td class="v"><?= h((string)$shipper) ?></td></tr>
      <tr><td class="k">DESCRIPTION</td><td class="v"><?= h((string)$desc) ?></td></tr>
      <tr><td class="k">NO OF PACKAGES</td><td class="v"><?= h((string)$pkgs) ?></td></tr>
      <tr><td class="k">WEIGHT</td><td class="v"><?= h((string)n2($weightKg)) ?></td></tr>
      <tr><td class="k">CBM</td><td class="v"><?= h((string)n3($cbm)) ?></td></tr>
    </table>

    <table class="tbl">
      <tr><td class="k">BL NO.</td><td class="v"><?= h((string)$blNo) ?></td></tr>
      <tr><td class="k">CONTAINER NO.</td><td class="v"><?= h((string)$container) ?></td></tr>
      <tr><td class="k">VESSEL/ VOY</td><td class="v"><?= h((string)$vesselVoy) ?></td></tr>
      <tr><td class="k">LOAD PORT</td><td class="v"><?= h((string)$loadPort) ?></td></tr>
      <tr><td class="k">DESTINATION PORT</td><td class="v"><?= h((string)$destPort) ?></td></tr>
    </table>

    <div class="closing">
      <p>We pledge to deliver our best service to you.</p>

      <p style="margin-top:22px;">
        Thanking you,<br>
        For and on behalf of<br>
        <strong>WSC LOGISTICS KENYA LIMITED</strong>
      </p>
    </div>

    <div class="audit-footer">
      You were served by: <?= h((string)($servedBy !== '' ? $servedBy : '—')) ?>
    </div>

  </div>
</div>