<?php
// Sertakan file config.php untuk koneksi database
include 'config.php';

// Cek apakah form telah disubmit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Ambil data dari form
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $role = $_POST['role'];

    // Validasi data - pastikan semua field diisi
    if (empty($username) || empty($email) || empty($password) || empty($role)) {
        echo "All fields are required!";
        exit;
    }

    // Hash password
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

    // Siapkan SQL untuk memasukkan data ke tabel admin_master
    $query = "INSERT INTO admin_master (username, email, password, role) VALUES (:username, :email, :password, :role)";
    $stmt = $db->prepare($query);

    // Bind parameter
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':password', $hashedPassword);
    $stmt->bindParam(':role', $role);

    // Eksekusi query
    if ($stmt->execute()) {
        // Redirect ke halaman login jika sukses
        header("Location: login.php");
        exit;
    } else {
        echo "Failed to register admin. Please try again.";
    }
}
?>
