<?php
require_once __DIR__ . '/config/config.php';

$error  = '';
$sukses = '';

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

    $nama_lengkap = trim($_POST['nama_lengkap']);
    $npsn         = trim($_POST['username']); // NPSN = username
    $password     = trim($_POST['password']);

    /* =========================
       VALIDASI DASAR
    ========================= */
    if ($nama_lengkap === '' || $npsn === '' || $password === '') {
        $error = "Semua field wajib diisi!";
    }
    elseif (!ctype_digit($npsn)) {
        $error = "Username harus berupa NPSN (angka)!";
    }
    else {

        /* =========================
           CEK USER SUDAH ADA
        ========================= */
        $cekUser = $conn->prepare(
            "SELECT id_user FROM users WHERE username = ? LIMIT 1"
        );
        $cekUser->bind_param("s", $npsn);
        $cekUser->execute();
        $cekUser->store_result();

        if ($cekUser->num_rows > 0) {
            $error = "Akun dengan NPSN ini sudah terdaftar!";
        }
        else {

            /* =========================
               SIMPAN USER SEKOLAH
            ========================= */
            $password_hash = password_hash($password, PASSWORD_DEFAULT);

            $role   = 'sekolah';
            $status = 'aktif';

            $stmt = $conn->prepare(
                "INSERT INTO users
                (nama_lengkap, username, password, role, status)
                VALUES (?,?,?,?,?)"
            );
            $stmt->bind_param(
                "sssss",
                $nama_lengkap,
                $npsn,
                $password_hash,
                $role,
                $status
            );

            if ($stmt->execute()) {
                $sukses = "Pendaftaran berhasil. Silakan login menggunakan NPSN.";
            } else {
                $error = "Gagal mendaftarkan akun!";
            }
        }
    }
}
?>



<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Register e-BOS</title>
<!-- Favicon (logo di tab browser) -->
<link rel="icon" href="assets/images/logomu.ico">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
/* ===== BACKGROUND HALAMAN ===== */
body{
    min-height:100vh;
    background:
        linear-gradient(135deg, #4e73df, #1cc88a),
        url("data:image/svg+xml,%3Csvg width='80' height='80' viewBox='0 0 80 80' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23ffffff' fill-opacity='0.06'%3E%3Ccircle cx='40' cy='40' r='2'/%3E%3C/g%3E%3C/svg%3E");
    background-blend-mode: overlay;
    padding:25px;
    font-family:"Segoe UI",sans-serif;
    display:flex;
    align-items:center;
    justify-content:center;
}

/* ===== CARD FORM ===== */
.card-custom{
    width:100%;
    max-width:420px;
    background:#fff;
    padding:35px;
    border-radius:0px;
    box-shadow:0 12px 30px rgba(0,0,0,.18);
}

/* ===== LOGO ===== */
.logo{
    text-align:center;
    margin-bottom:15px;
}
.logo img{
    width:90px;
}

/* ===== BUTTON ===== */
.btn-primary{
    background:#4e73df;
    border:none;
}
.btn-primary:hover{
    background:#3b5bd6;
}
</style>
</head>

<body>

<div class="card-custom">
    <div class="logo">
        <img src="assets/images/logoku.png" alt="Logo Sekolah">
    </div>

    <h4 class="mb-3 text-center fw-semibold">Daftar Akun</h4>

    <?php if(isset($error) && $error){ ?>
        <div class="alert alert-danger"><?= $error ?></div>
    <?php } ?>

    <?php if(isset($sukses) && $sukses){ ?>
        <div class="alert alert-success text-center">
            <?= $sukses ?><br>
            <a href="index.php" class="fw-semibold">➡ Login Sekarang</a>
        </div>
    <?php } ?>

    <form method="post">
        <div class="mb-3">
            <label class="form-label">Nama Sekolah</label>
            <input type="text" name="nama_lengkap" class="form-control" required>
        </div>

        <div class="mb-3">
			<label class="form-label">Username</label>
			<input type="text" name="username" class="form-control" required>
			<small class="text-muted">Daftar dengan username : NPSN Lembaga</small>
		</div>

        <div class="mb-4">
            <label class="form-label">Password</label>
            <input type="password" name="password" class="form-control" required>
        </div>

        <button name="daftar" class="btn btn-primary w-100">
            💾 Daftar
        </button>

        <div class="text-center mt-3">
            <a href="index.php" class="text-decoration-none">⬅ Kembali ke Login</a>
        </div>
    </form>
</div>

</body>
</html>

