<?php 
global $db, $config;

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

$stmt = $db->prepare("SELECT h.*,
                             m.mbl_no,
                             c1.name consignee_name,
                             c2.name billing_name,
                             c3.name notify_name
                      FROM house_bls h
                      JOIN master_bls m ON m.id=h.master_bl_id
                      JOIN customers c1 ON c1.id=h.consignee_customer_id
                      JOIN customers c2 ON c2.id=h.billing_party_customer_id
                      LEFT JOIN customers c3 ON c3.id=h.notify_party_customer_id
                      WHERE h.id=?");
$stmt->execute([$id]);
$h = $stmt->fetch();
if (!$h) { echo '<div class="glass rounded-3xl p-5">HBL not found.</div>'; return; }
?>

<div class="max-w-5xl">
  <div class="flex items-center justify-between mb-4">
    <h1 class="text-2xl font-semibold">Edit House BL</h1>
    <a class="px-4 py-2 rounded-2xl bg-white border border-orange-200 text-orange-700 hover:bg-orange-50 transition"
       href="<?= h($basePath) ?>/hbl?master_bl_id=<?= (int)$h['master_bl_id'] ?>" data-nav>Back</a>
  </div>

  <form class="glass rounded-3xl p-6 grid md:grid-cols-6 gap-4"
        action="<?= h($basePath) ?>/api/hbl/update" method="post" data-ajax>

    <input type="hidden" name="id" value="<?= (int)$h['id'] ?>">

    <div class="md:col-span-2">
      <label class="form-label required">MBL</label>
      <select name="master_bl_id" required class="js-mbl mt-1 w-full">
        <option value="<?= (int)$h['master_bl_id'] ?>" selected><?= h($h['mbl_no']) ?></option>
      </select>
    </div>

    <div class="md:col-span-2">
      <label class="form-label required">HBL No</label>
      <input name="hbl_no" required value="<?= h($h['hbl_no']) ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div>
      <label class="form-label">Weight (kg)</label>
      <input name="gross_weight_kg" type="number" step="0.001" value="<?= h($h['gross_weight_kg']) ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div>
      <label class="form-label">CBM</label>
      <input name="cbm" type="number" step="0.001" value="<?= h($h['cbm']) ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div class="md:col-span-3">
      <label class="form-label required">Consignee</label>
      <select name="consignee_customer_id" required class="js-customer mt-1 w-full">
        <option value="<?= (int)$h['consignee_customer_id'] ?>" selected><?= h($h['consignee_name']) ?></option>
      </select>
    </div>

    <div class="md:col-span-3">
      <div class="flex items-center justify-between">
        <label class="form-label required">Billing Party</label>

        <button type="button"
                class="text-xs px-3 py-1 rounded-xl bg-ink-100 hover:bg-ink-200 transition"
                onclick="copyConsigneeToField(this,'billing_party_customer_id')">
          Copy from Consignee
        </button>
      </div>

      <select name="billing_party_customer_id" required class="js-customer mt-1 w-full">
        <option value=""></option>
        <?php if (!empty($h['billing_party_customer_id'])): ?>
          <option value="<?= (int)$h['billing_party_customer_id'] ?>" selected>
            <?= h($h['billing_name'] ?? '') ?>
          </option>
        <?php endif; ?>
      </select>

      <div class="text-[11px] text-ink-500 mt-1">Can be same as consignee or different.</div>
    </div>

    <div class="md:col-span-3">
      <div class="flex items-center justify-between">
        <label class="form-label">Notify Party</label>

        <button type="button"
                class="text-xs px-3 py-1 rounded-xl bg-ink-100 hover:bg-ink-200 transition"
                onclick="copyConsigneeToField(this,'notify_party_customer_id')">
          Copy from Consignee
        </button>
      </div>

      <select name="notify_party_customer_id" class="js-customer mt-1 w-full">
        <option value=""></option>
        <?php if (!empty($h['notify_party_customer_id'])): ?>
          <option value="<?= (int)$h['notify_party_customer_id'] ?>" selected>
            <?= h($h['notify_name'] ?? '') ?>
          </option>
        <?php endif; ?>
      </select>

      <div class="text-[11px] text-ink-500 mt-1">Optional.</div>
    </div>

    <div class="md:col-span-4">
      <label class="form-label">Goods Description</label>
      <input name="goods_description" value="<?= h($h['goods_description']) ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div>
      <label class="form-label">Packages</label>
      <input name="packages" type="number" step="1" value="<?= h($h['packages']) ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div>
      <label class="form-label">Type</label>
      <select name="package_type"
              class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900">
        <option value=""></option>
        <option value="PALLET"  <?= (($h['package_type'] ?? '') === 'PALLET')  ? 'selected' : '' ?>>PALLET</option>
        <option value="CARTONS" <?= (($h['package_type'] ?? '') === 'CARTONS') ? 'selected' : '' ?>>CARTONS</option>
        <option value="ROLLS"   <?= (($h['package_type'] ?? '') === 'ROLLS')   ? 'selected' : '' ?>>ROLLS</option>
        <option value="BOXES"   <?= (($h['package_type'] ?? '') === 'BOXES')   ? 'selected' : '' ?>>BOXES</option>
      </select>
    </div>

    <div class="md:col-span-2">
      <label class="form-label">HS Code</label>
      <input name="hs_code" value="<?= h($h['hs_code'] ?? '') ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div class="md:col-span-2">
      <label class="form-label">Manifest No.</label>
      <input name="manifest_no" value="<?= h($h['manifest_no'] ?? '') ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div class="md:col-span-2">
      <label class="form-label">ICMS BL No</label>
      <input name="icms_bl_no" value="<?= h($h['icms_bl_no'] ?? '') ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div class="md:col-span-3">
      <label class="form-label">CFS</label>
      <input name="cfs" value="<?= h($h['cfs'] ?? '') ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div class="md:col-span-3">
      <label class="form-label">Marks &amp; Numbers</label>
      <input name="marks_numbers" value="<?= h($h['marks_numbers'] ?? '') ?>"
             class="mt-1 w-full px-3 py-2 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div class="md:col-span-6 pt-2">
      <button class="btn-primary px-5 py-3 rounded-2xl transition">
        Update HBL
      </button>
    </div>

    <div class="md:col-span-6 text-xs text-ink-500">
      Note: Chargeable CBM / W(Ton) / W/M Units will be recalculated on save.
    </div>

  </form>
</div>

