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

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

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

$success = false;

if(isset($_POST['simpan'])){
  mysqli_query($koneksi,"
    UPDATE guru SET
    nip='$_POST[nip]',
    nama_lengkap='$_POST[nama_lengkap]',
    mapel='$_POST[mapel]',
    jabatan='$_POST[jabatan]',
    no_wa='$_POST[no_wa]',
    email='$_POST[email]',
    status='$_POST[status]'
    WHERE id='$id'
  ");
  $success = true;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Edit Guru</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);
}
.label{font-weight:600;color:#555;}
</style>
</head>

<body>

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

<h4 class="fw-bold mb-4">
  <i class="bi bi-gear"></i> Edit Data Guru
</h4>

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

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

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

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

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

  <div class="col-md-4">
    <label class="label">Status</label>
    <select name="status" class="form-select">
      <option <?= $data['status']=='Aktif'?'selected':'' ?>>Aktif</option>
      <option <?= $data['status']=='Nonaktif'?'selected':'' ?>>Nonaktif</option>
    </select>
  </div>

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

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

</div>

<div class="d-flex gap-2 mt-4">
  <button name="simpan" class="btn btn-success rounded-pill px-4">
    <i class="bi bi-check-circle"></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 guru berhasil diperbarui',
  timer:1500,
  showConfirmButton:false
}).then(()=>{
  try{
    parent.document.getElementById('iframeContent').src='kelola_guru/guru.php';
  }catch{
    window.location.href='guru.php';
  }
});
</script>
<?php endif; ?>

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

</body>
</html>
