<?php
require_once __DIR__ . "/../config/db.php";
include __DIR__ . "/../partials/header.php";
include __DIR__ . "/../partials/sidebar.php";

$q = trim($_GET["q"] ?? "");
$where = "";

if ($q !== "") {
  $safe = $conn->real_escape_string($q);
  $where = "WHERE nomor_resi LIKE '%{$safe}%' OR nama_paket LIKE '%{$safe}%'";
}

$res = $conn->query("SELECT * FROM resi {$where} ORDER BY created_at DESC LIMIT 200");
?>

<div class="card-glass p-4 mb-3">
  <div class="d-flex justify-content-between align-items-center flex-wrap gap-2">
    <div>
      <h3 class="mb-1">Data Resi</h3>
      <div class="text-muted">Daftar resi yang boleh akses loker (valid/invalid).</div>
    </div>

    <div class="d-flex gap-2">
      <a class="btn btn-accent text-white px-3" href="/smart-drop-point/public/resi_add.php">
        <i class="bi bi-plus-circle me-2"></i>Tambah
      </a>

      <a class="btn btn-outline-light px-3" href="/smart-drop-point/public/log_add_mock.php">
        <i class="bi bi-lightning-charge me-2"></i>Buat Log Dummy
      </a>
    </div>
  </div>

  <form class="row g-2 mt-3" method="get">
    <div class="col-md-10">
      <input 
        class="form-control" 
        name="q" 
        value="<?= htmlspecialchars($q) ?>" 
        placeholder="Cari nomor resi / nama paket..."
      >
    </div>

    <div class="col-md-2 d-grid">
      <button class="btn btn-outline-light" type="submit">
        <i class="bi bi-search me-2"></i>Cari
      </button>
    </div>
  </form>
</div>

<div class="card-glass p-3">
  <div class="table-responsive">
    <table class="table table-dark table-hover align-middle mb-0">
      <thead>
        <tr>
          <th>Nomor Resi</th>
          <th>Nama Paket</th>
          <th>Status</th>
          <th>Dibuat</th>
          <th width="220">Aksi</th>
        </tr>
      </thead>

      <tbody>
        <?php if ($res && $res->num_rows > 0): ?>
          <?php while ($r = $res->fetch_assoc()): ?>
            <tr>
              <td class="fw-semibold">
                <?= htmlspecialchars($r["nomor_resi"] ?? "-") ?>
              </td>

              <td class="text-muted">
                <?= htmlspecialchars($r["nama_paket"] ?? "-") ?>
              </td>

              <td>
                <?php if (($r["status"] ?? "") === "valid"): ?>
                  <span class="badge text-bg-success">VALID</span>
                <?php else: ?>
                  <span class="badge text-bg-secondary">INVALID</span>
                <?php endif; ?>
              </td>

              <td class="text-muted">
                <?= htmlspecialchars($r["created_at"] ?? "-") ?>
              </td>

              <td>
                <div class="d-flex gap-2 flex-wrap">
                  <a class="btn btn-sm btn-outline-light" href="/smart-drop-point/public/resi_detail.php?id=<?= (int)$r["id"] ?>">
                    <i class="bi bi-eye me-1"></i>Detail
                  </a>

                  <a class="btn btn-sm btn-outline-warning" href="/smart-drop-point/public/resi_toggle.php?id=<?= (int)$r["id"] ?>">
                    <i class="bi bi-arrow-repeat me-1"></i>Toggle
                  </a>
                </div>
              </td>
            </tr>
          <?php endwhile; ?>
        <?php else: ?>
          <tr>
            <td colspan="5" class="text-muted">Belum ada data resi.</td>
          </tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php include __DIR__ . "/../partials/footer.php"; ?>