<?php
include '../../koneksi.php';

if (!isset($_GET['id'])) {
  die("ID tidak ditemukan");
}

$id = $_GET['id'];

$q = mysqli_query($koneksi, "SELECT * FROM siswa WHERE id='$id'");
$data = mysqli_fetch_assoc($q);
if (!$data) die("Data tidak ditemukan");

$success = false;

if (isset($_POST['simpan'])) {
  mysqli_query($koneksi, "
    UPDATE siswa SET
      nisn='$_POST[nisn]',
      nama_lengkap='$_POST[nama_lengkap]',
      kelas='$_POST[kelas]',
      tempat_lahir='$_POST[tempat_lahir]',
      tanggal_lahir='$_POST[tanggal_lahir]',
      no_wa='$_POST[no_wa]',
      email='$_POST[email]'
    WHERE id='$id'
  ");

  $success = true;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Edit Siswa</title>

<link href="../../bootstrap-5.3.8-dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>
body { background:#f4f6f9; }
.card {
  border-radius:18px;
  box-shadow:0 12px 35px rgba(0,0,0,.12);
}
</style>
</head>

<body>

<div class="container-fluid mt-3">
<div class="card p-4">

<h4 class="fw-bold mb-4">
  <i class="bi bi-pencil-square"></i> Edit Data Siswa
</h4>

<form method="POST">
<div class="row g-3">

  <div class="col-md-4">
    <label class="form-label">NISN</label>
    <input type="text" name="nisn" value="<?= $data['nisn'] ?>" class="form-control" required>
  </div>

  <div class="col-md-8">
    <label class="form-label">Nama Lengkap</label>
    <input type="text" name="nama_lengkap" value="<?= $data['nama_lengkap'] ?>" class="form-control" required>
  </div>

  <div class="col-md-4">
    <label class="form-label">Kelas</label>
    <input type="text" name="kelas" value="<?= $data['kelas'] ?>" class="form-control" required>
  </div>

  <div class="col-md-4">
    <label class="form-label">Tempat Lahir</label>
    <input type="text" name="tempat_lahir" value="<?= $data['tempat_lahir'] ?>" class="form-control">
  </div>

  <div class="col-md-4">
    <label class="form-label">Tanggal Lahir</label>
    <input type="date" name="tanggal_lahir" value="<?= $data['tanggal_lahir'] ?>" class="form-control">
  </div>

  <div class="col-md-6">
    <label class="form-label">No WhatsApp</label>
    <input type="text" name="no_wa" value="<?= $data['no_wa'] ?>" class="form-control">
  </div>

  <div class="col-md-6">
    <label class="form-label">Email</label>
    <input type="email" name="email" value="<?= $data['email'] ?>" class="form-control">
  </div>

</div>

<div class="mt-4 d-flex gap-2">
  <button name="simpan" class="btn btn-success rounded-pill px-4">
    <i class="bi bi-save"></i> Simpan
  </button>

  <button type="button" onclick="kembali()"
    class="btn btn-secondary rounded-pill px-4">
    Kembali
  </button>
</div>

</form>

</div>
</div>

<?php if ($success): ?>
<script>
Swal.fire({
  icon: 'success',
  title: 'Berhasil',
  text: 'Data siswa berhasil diperbarui',
  timer: 1500,
  showConfirmButton: false
}).then(() => {
  try {
    parent.document.getElementById('iframeContent').src = 'kelola_siswa/siswa.php';
  } catch {
    window.location.href = 'siswa.php';
  }
});
</script>
<?php endif; ?>

<script>
function kembali() {
  try {
    parent.document.getElementById('iframeContent').src = 'kelola_siswa/siswa.php';
  } catch {
    window.location.href = 'siswa.php';
  }
}
</script>

</body>
</html>
