<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #007BFF;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 10px;
        }
        .container {
            max-width: 400px;
            width: 100%;
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            background-color: white;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        .btn-custom {
            background-color: #4A90E2;
            color: white;
            width: 100%;
            padding: 10px;
            border-radius: 5px;
        }
        .btn-custom:hover {
            background-color: #357ABD;
        }
        .alert {
            margin-top: 15px;
            text-align: center;
        }
    </style>
</head>
<body>

    <div class="container">
        <h2>Form Registrasi</h2>
        <?php
        include 'koneksi.php';

        if (isset($_POST['submit'])) {
            $email = $_POST['email'];
            $password = $_POST['password'];
            $role = $_POST['role'];

            // Validasi email
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                echo "<div class='alert alert-danger'>Email tidak valid!</div>";
            } else {
                // Hash password
                $hashed_password = password_hash($password, PASSWORD_BCRYPT);

                // Query untuk menambahkan pengguna baru
                $query = "INSERT INTO pengguna (email, password, role) VALUES ('$email', '$hashed_password', '$role')";

                if (mysqli_query($koneksi, $query)) {
                    $id_pengguna = mysqli_insert_id($koneksi);

                    // Tambahkan data ke tabel sesuai dengan role
                    if ($role == 'siswa') {
                        $insert_siswa = "INSERT INTO tbl_siswa (id_pengguna, email) VALUES ('$id_pengguna', '$email')";
                        mysqli_query($koneksi, $insert_siswa);
                    } elseif ($role == 'guru') {
                        $insert_guru = "INSERT INTO tbl_guru (id_pengguna, email) VALUES ('$id_pengguna', '$email')";
                        mysqli_query($koneksi, $insert_guru);
                    } elseif ($role == 'admin') {
                        $insert_pengguna = "INSERT INTO tbl_pengguna (email) VALUES ('$email')";
                        mysqli_query($koneksi, $insert_pengguna);
                    }

                    echo "<div class='alert alert-success'>Registrasi berhasil! Anda akan diarahkan ke halaman login.</div>";
                    header("refresh:3;url=login.php");
                } else {
                    echo "<div class='alert alert-danger'>Error: " . mysqli_error($koneksi) . "</div>";
                }
            }

            mysqli_close($koneksi);
        }
        ?>
        <form method="POST" action="daftar.php">
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" class="form-control" name="email" required>
            </div>
            
            <div class="form-group">
                <label for="password">Password:</label>
                <input type="password" class="form-control" name="password" required>
            </div>
            
            <div class="form-group">
                <label for="role">Peran:</label>
                <select class="form-control" name="role" required>
                    <option value="admin">Admin</option>
                    <option value="guru">Guru</option>
                    <option value="siswa">Siswa</option>
                </select>
            </div>

            <button type="submit" name="submit" class="btn btn-custom">Daftar</button>
        </form>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
