<?php
global $db, $config;

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

$stmt = $db->prepare("SELECT m.*, sl.name AS shipping_line_name,
                             p1.name AS pol_name, p1.country AS pol_country,
                             p2.name AS pod_name, p2.country AS pod_country
                      FROM master_bls m
                      JOIN shipping_lines sl ON sl.id=m.shipping_line_id
                      JOIN ports p1 ON p1.id=m.pol_port_id
                      JOIN ports p2 ON p2.id=m.pod_port_id
                      WHERE m.id=?");
$stmt->execute([$id]);
$m = $stmt->fetch();
if (!$m) { echo '<div class="glass rounded-3xl p-5">MBL not found.</div>'; return; }
?>

<div class="max-w-3xl">
  <div class="flex items-center justify-between mb-4">
    <h1 class="text-2xl font-semibold">Edit Master BL</h1>
    <a class="px-4 py-2 rounded-2xl bg-white/15 hover:bg-white/20 transition" href="<?= h($basePath) ?>/mbl" data-nav>Back</a>
  </div>

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

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

    <div class="md:col-span-2">
      <label class="form-label required">MBL Number</label>
      <input name="mbl_no" required value="<?= h($m['mbl_no']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900 focus:ring-0" />
    </div>

    <div>
      <label class="form-label required">Shipping Line</label>
      <select name="shipping_line_id" required class="js-shipping-line mt-1 w-full">
        <option value="<?= (int)$m['shipping_line_id'] ?>" selected><?= h($m['shipping_line_name']) ?></option>
      </select>
    </div>

    <div>
      <label class="form-label">ETA</label>
      <input type="date" name="eta_date" value="<?= h($m['eta_date']) ?>"
             class="mt-1 w-full px-4 py-3 rounded-2xl bg-white/15 border border-white/20" />
    </div>

    <div>
      <label class="form-label required">Port of Loading (POL)</label>
      <select name="pol_port_id" required class="js-port mt-1 w-full">
        <option value="<?= (int)$m['pol_port_id'] ?>" selected>
          <?= h($m['pol_name']) ?><?= $m['pol_country'] ? ' (' . h($m['pol_country']) . ')' : '' ?>
        </option>
      </select>
    </div>

    <div>
      <label class="form-label required">Port of Destination (POD)</label>
      <select name="pod_port_id" required class="js-port mt-1 w-full">
        <option value="<?= (int)$m['pod_port_id'] ?>" selected>
          <?= h($m['pod_name']) ?><?= $m['pod_country'] ? ' (' . h($m['pod_country']) . ')' : '' ?>
        </option>
      </select>
    </div>

    <div>
      <label class="form-label">Vessel Name</label>
      <input name="vessel_name" value="<?= h($m['vessel_name']) ?>"
class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div>
      <label class="form-label">Voyage No</label>
      <input name="voyage_no" value="<?= h($m['voyage_no']) ?>"
        class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

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

    <div>
      <label class="form-label">Container Type</label>
      <select name="container_type"
              class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900">
        <option value="">Select container type…</option>
        <option value="20ft SD" <?= (($m['container_type'] ?? '') === '20ft SD') ? 'selected' : '' ?>>20ft SD</option>
        <option value="40ft SD" <?= (($m['container_type'] ?? '') === '40ft SD') ? 'selected' : '' ?>>40ft SD</option>
        <option value="40ft HC" <?= (($m['container_type'] ?? '') === '40ft HC') ? 'selected' : '' ?>>40ft HC</option>
        <option value="20ft OT" <?= (($m['container_type'] ?? '') === '20ft OT') ? 'selected' : '' ?>>20ft OT</option>
        <option value="40ft HC-OT" <?= (($m['container_type'] ?? '') === '40ft HC-OT') ? 'selected' : '' ?>>40ft HC-OT</option>
        <option value="20ft RF" <?= (($m['container_type'] ?? '') === '20ft RF') ? 'selected' : '' ?>>20ft RF</option>
      </select>
    </div>

    <!-- ✅ NEW: Seal No (must come after container_no) -->
    <div>
      <label class="form-label">Seal No.</label>
      <input name="seal_no" value="<?= h($m['seal_no'] ?? '') ?>"
            class="mt-1 w-full px-4 py-3 rounded-2xl border border-slate-200 bg-white text-slate-900" />
    </div>

    <div class="md:col-span-2 flex justify-start">
      <button class="btn-primary px-5 py-3 rounded-2xl transition">
        Update MBL
      </button>
    </div>

  </form>
</div>