<?php
include 'koneksi.php';

$error = "";
$success = "";

$mapelIPA = [
'Al-Qur’an Hadits','Akidah Akhlak','Fiqih','SKI',
'Bahasa Indonesia','Bahasa Inggris','Bahasa Arab',
'Matematika Wajib','Matematika Peminatan',
'Fisika','Kimia','Biologi',
'Sejarah Indonesia','PPKn',
'PJOK','Seni Budaya','Prakarya'
];

$mapelIPS = [
'Al-Qur’an Hadits','Akidah Akhlak','Fiqih','SKI',
'Bahasa Indonesia','Bahasa Inggris','Bahasa Arab',
'Matematika Wajib',
'Ekonomi','Geografi','Sosiologi','Sejarah Peminatan',
'Sejarah Indonesia','PPKn',
'PJOK','Seni Budaya','Prakarya'
];

if (isset($_POST['register'])) {

    $role     = $_POST['role'];
    $username = mysqli_real_escape_string($koneksi,$_POST['username']);
    $password = md5($_POST['password']); // konsisten dg login

    $cekUser = mysqli_query($koneksi,"SELECT id FROM users WHERE username='$username'");
    if (mysqli_num_rows($cekUser)>0){
        $error = "Username sudah digunakan!";
    } else {

        /* ======================
           REGISTRASI SISWA
        ====================== */
        if ($role=='siswa') {

            mysqli_query($koneksi,"
              INSERT INTO users (username,password,role)
              VALUES ('$username','$password','siswa')
            ");
            $user_id = mysqli_insert_id($koneksi);

            mysqli_query($koneksi,"
              INSERT INTO siswa
              (user_id,nisn,nama_lengkap,kelas,alamat,tempat_lahir,
               tanggal_lahir,no_wa,email,nama_ayah,nama_ibu)
              VALUES (
                '$user_id',
                '{$_POST['nisn']}',
                '{$_POST['nama_lengkap']}',
                '{$_POST['kelas']}',
                '{$_POST['alamat']}',
                '{$_POST['tempat_lahir']}',
                '{$_POST['tanggal_lahir']}',
                '{$_POST['no_wa']}',
                '{$_POST['email']}',
                '{$_POST['nama_ayah']}',
                '{$_POST['nama_ibu']}'
              )
            ");

            $success = "siswa";
        }

        /* ======================
   REGISTRASI GURU (FIX)
====================== */
elseif ($role=='guru') {

    if ($_POST['token'] !== '040301') {
        $error = "Token guru tidak valid!";
    } else {

        // 1️⃣ INSERT KE USERS
        mysqli_query($koneksi,"
          INSERT INTO users (username,password,role)
          VALUES ('$username','$password','guru')
        ");

        // 2️⃣ AMBIL user_id BARU
        $user_id = mysqli_insert_id($koneksi);

        // 3️⃣ INSERT KE GURU + user_id
        mysqli_query($koneksi,"
          INSERT INTO guru
          (user_id,nip,nama_lengkap,jenis_kelamin,tempat_lahir,
           tanggal_lahir,alamat,no_wa,email,mapel,jabatan)
          VALUES (
            '$user_id',
            '{$_POST['nip']}',
            '{$_POST['nama_lengkap']}',
            '{$_POST['jenis_kelamin']}',
            '{$_POST['tempat_lahir']}',
            '{$_POST['tanggal_lahir']}',
            '{$_POST['alamat']}',
            '{$_POST['no_wa']}',
            '{$_POST['email']}',
            '{$_POST['mapel']}',
            '{$_POST['jabatan']}'
          )
        ");

        $success = "guru";
    }
}

    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Registrasi | MAYABSEN</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<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">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>
body{
    font-family:'Poppins',sans-serif;
    background:linear-gradient(135deg,#eaf2f8,#fff);
}
.card{
    border-radius:22px;
    box-shadow:0 12px 35px rgba(0,0,0,.15);
}
</style>
</head>

<body>

<div class="container py-5">
<div class="row justify-content-center">
<div class="col-lg-7">

<div class="card p-4">
<h4 class="fw-bold text-center mb-4">
<i class="bi bi-person-plus-fill"></i> Registrasi Akun
</h4>

<?php if($error): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>

<form method="POST">

<div class="mb-3">
<label class="form-label">Daftar Sebagai</label>
<select name="role" id="role" class="form-select" required>
<option value="">-- Pilih --</option>
<option value="siswa">Siswa</option>
<option value="guru">Guru</option>
</select>
</div>

<div class="row">
<div class="col-md-6 mb-3">
<label>Username</label>
<input name="username" class="form-control" required>
</div>
<div class="col-md-6 mb-3">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>
</div>

<div id="tokenBox" class="mb-3 d-none">
<label>Token Guru</label>
<input name="token" class="form-control">
</div>

<hr>

<div class="mb-3">
<label>Nama Lengkap</label>
<input name="nama_lengkap" class="form-control" required>
</div>

<!-- SISWA -->
<div id="siswaBox" class="d-none">
<input name="nisn" class="form-control mb-2" placeholder="NISN">
<select name="kelas" class="form-select mb-2">
<option>X IPA</option><option>X IPS</option>
<option>XI IPA</option><option>XI IPS</option>
<option>XII IPA</option><option>XII IPS</option>
</select>
<input name="nama_ayah" class="form-control mb-2" placeholder="Nama Ayah">
<input name="nama_ibu" class="form-control mb-2" placeholder="Nama Ibu">
</div>

<!-- GURU -->
<div id="guruBox" class="d-none">
<input name="nip" class="form-control mb-2" placeholder="NIP">
<select name="jenis_kelamin" class="form-select mb-2">
<option value="L">Laki-laki</option>
<option value="P">Perempuan</option>
</select>

<select name="mapel" class="form-select mb-2">
<optgroup label="Mapel IPA">
<?php foreach($mapelIPA as $m): ?>
<option><?= $m ?></option>
<?php endforeach; ?>
</optgroup>
<optgroup label="Mapel IPS">
<?php foreach($mapelIPS as $m): ?>
<option><?= $m ?></option>
<?php endforeach; ?>
</optgroup>
</select>

<input name="jabatan" class="form-control mb-2" placeholder="Jabatan">
</div>

<input name="tempat_lahir" class="form-control mb-2" placeholder="Tempat Lahir">
<input type="date" name="tanggal_lahir" class="form-control mb-2">
<textarea name="alamat" class="form-control mb-2" placeholder="Alamat"></textarea>
<input name="no_wa" class="form-control mb-2" placeholder="No WhatsApp">
<input name="email" type="email" class="form-control mb-3" placeholder="Email">

<button name="register" class="btn btn-primary w-100 rounded-pill">
<i class="bi bi-check-circle"></i> Daftar
</button>

</form>
</div>
</div>
</div>
</div>

<?php if($success): ?>
<script>
Swal.fire({
    icon:'success',
    title:'Registrasi Berhasil 🎉',
    text:'Akun <?= $success ?> berhasil dibuat',
    timer:1600,
    showConfirmButton:false
}).then(()=>{
    window.location='login.php';
});
</script>
<?php endif; ?>

<script>
const role=document.getElementById('role');
role.onchange=()=>{
document.getElementById('siswaBox').classList.toggle('d-none',role.value!=='siswa');
document.getElementById('guruBox').classList.toggle('d-none',role.value!=='guru');
document.getElementById('tokenBox').classList.toggle('d-none',role.value!=='guru');
}
</script>

<script src="bootstrap-5.3.8-dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
