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

$success = false;

if (isset($_POST['simpan'])) {
  $nip      = $_POST['nip'];
  $nama     = $_POST['nama_lengkap'];
  $mapel    = $_POST['mapel'];
  $jabatan  = $_POST['jabatan'];
  $no_wa    = $_POST['no_wa'];
  $email    = $_POST['email'];
  $status   = $_POST['status'];

  mysqli_query($koneksi, "
    INSERT INTO guru 
    (nip, nama_lengkap, mapel, jabatan, no_wa, email, status)
    VALUES
    ('$nip','$nama','$mapel','$jabatan','$no_wa','$email','$status')
  ");

  $success = true;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Tambah 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-person-plus-fill"></i> Tambah 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" class="form-control" required>
  </div>

  <div class="col-md-8">
    <label class="label">Nama Lengkap</label>
    <input type="text" name="nama_lengkap" class="form-control" required>
  </div>

  <div class="col-md-4">
    <label class="label">Mapel</label>
    <input type="text" name="mapel" class="form-control" required>
  </div>

  <div class="col-md-4">
    <label class="label">Jabatan</label>
    <input type="text" name="jabatan" class="form-control">
  </div>

  <div class="col-md-4">
    <label class="label">Status</label>
    <select name="status" class="form-select">
      <option>Aktif</option>
      <option>Nonaktif</option>
    </select>
  </div>

  <div class="col-md-6">
    <label class="label">No. WhatsApp</label>
    <input type="text" name="no_wa" class="form-control">
  </div>

  <div class="col-md-6">
    <label class="label">Email</label>
    <input type="email" name="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 ditambahkan',
  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>
