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

$err = ""; $ok = "";

if($_SERVER["REQUEST_METHOD"]==="POST"){
  $nomor = trim($_POST["nomor_resi"] ?? "");
  $nama  = trim($_POST["nama_paket"] ?? "");
  $status = $_POST["status"] ?? "valid";

  if($nomor===""){
    $err = "Nomor resi wajib diisi.";
  } else {
    $stmt = $conn->prepare("INSERT INTO resi (nomor_resi, nama_paket, status) VALUES (?,?,?)");
    $stmt->bind_param("sss", $nomor, $nama, $status);
    if($stmt->execute()){
      $ok = "Resi berhasil ditambahkan.";
    } else {
      $err = "Gagal menambahkan resi. Mungkin resinya sudah ada.";
    }
    $stmt->close();
  }
}
?>

<div class="card-glass p-4">
  <h3 class="mb-1">Tambah Resi</h3>
  <div class="text-muted mb-3">Masukkan resi yang dianggap valid untuk akses.</div>

  <?php if($err): ?>
    <div class="alert alert-danger"><?= htmlspecialchars($err) ?></div>
  <?php endif; ?>
  <?php if($ok): ?>
    <div class="alert alert-success"><?= htmlspecialchars($ok) ?> <a href="/smart-drop-point/public/resi.php" class="alert-link">Lihat data</a></div>
  <?php endif; ?>

  <form method="post" class="row g-3">
    <div class="col-md-6">
      <label class="form-label">Nomor Resi</label>
      <input class="form-control" name="nomor_resi" placeholder="contoh: JP123456789" required>
    </div>
    <div class="col-md-6">
      <label class="form-label">Status</label>
      <select class="form-select" name="status">
        <option value="valid" selected>valid</option>
        <option value="invalid">invalid</option>
      </select>
    </div>
    <div class="col-12">
      <label class="form-label">Nama Paket (opsional)</label>
      <input class="form-control" name="nama_paket" placeholder="contoh: Paket Shopee / Buku / dll">
    </div>
    <div class="col-12 d-grid">
      <button class="btn btn-accent text-white py-2"><i class="bi bi-check2-circle me-2"></i>Simpan</button>
    </div>
  </form>
</div>

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