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

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

// Filter
$w = [];

if ($status === "berhasil" || $status === "gagal") {
  $w[] = "status='" . $conn->real_escape_string($status) . "'";
}

if ($q !== "") {
  $safe = $conn->real_escape_string($q);
  $w[] = "nomor_resi LIKE '%{$safe}%'";
}

$where = count($w) ? "WHERE " . implode(" AND ", $w) : "";

// Ambil data log
$logs = $conn->query("SELECT * FROM log_akses {$where} ORDER BY created_at DESC LIMIT 300");
?>

<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">Log Akses</h3>
      <div class="text-muted">Riwayat autentikasi barcode (berhasil/gagal) + bukti foto.</div>
    </div>
    <a class="btn btn-outline-light" href="/smart-drop-point/public/log_add_mock.php">
      <i class="bi bi-lightning-charge me-2"></i>Tambah Log Dummy
    </a>
  </div>

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

    <div class="col-md-5">
      <select class="form-select" name="status">
        <option value="">Semua status</option>
        <option value="berhasil" <?= $status === "berhasil" ? "selected" : "" ?>>berhasil</option>
        <option value="gagal" <?= $status === "gagal" ? "selected" : "" ?>>gagal</option>
      </select>
    </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>Waktu</th>
          <th>Nomor Resi</th>
          <th>Status</th>
          <th>Pesan</th>
          <th>Foto</th>
          <th class="text-center">Aksi</th>
        </tr>
      </thead>
      <tbody>

        <?php if ($logs && $logs->num_rows > 0): ?>
          <?php while ($l = $logs->fetch_assoc()): ?>
            <tr>
              <td class="text-muted">
                <?= htmlspecialchars($l["created_at"] ?? "-") ?>
              </td>

              <td class="fw-semibold">
                <?= !empty($l["nomor_resi"]) ? htmlspecialchars($l["nomor_resi"]) : "-" ?>
              </td>

              <td>
                <?php if (($l["status"] ?? "") === "berhasil"): ?>
                  <span class="badge text-bg-success">Berhasil</span>
                <?php else: ?>
                  <span class="badge text-bg-danger">Gagal</span>
                <?php endif; ?>
              </td>

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

              <td>
                <?php if (!empty($l["foto_path"])): ?>
                  <a target="_blank" href="<?= htmlspecialchars($l["foto_path"]) ?>">
                    <img
                      src="<?= htmlspecialchars($l["foto_path"]) ?>"
                      style="width:70px;height:70px;object-fit:cover;border-radius:10px;border:1px solid rgba(255,255,255,.2);"
                      alt="foto">
                  </a>
                <?php else: ?>
                  <span class="text-muted">-</span>
                <?php endif; ?>
              </td>

              <td class="text-center">
                <a
                  href="/smart-drop-point/public/hapus_log.php?id=<?= (int)($l["id"] ?? 0) ?>"
                  class="btn btn-sm btn-outline-danger"
                  onclick="return confirm('Yakin mau hapus log ini?');"
                  title="Hapus log">
                  <i class="bi bi-trash"></i>
                </a>
              </td>
            </tr>
          <?php endwhile; ?>

        <?php else: ?>
          <tr>
            <td colspan="6" class="text-muted text-center">
              Belum ada log.
            </td>
          </tr>
        <?php endif; ?>

      </tbody>
    </table>
  </div>
</div>

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