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

$nama     = $_POST['nama'];
$email    = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);

// Cek apakah email sudah ada
$cek = mysqli_query($conn, "SELECT * FROM pengguna WHERE email='$email'");
if (mysqli_num_rows($cek) > 0) {
    header("Location: ../register.php?pesan=gagal");
    exit;
}

$query = "INSERT INTO pengguna (nama, email, password, peran) VALUES ('$nama', '$email', '$password', 'pelanggan')";

if (mysqli_query($conn, $query)) {
    header("Location: ../login.php?pesan=daftar_sukses");
} else {
    echo "Gagal registrasi: " . mysqli_error($conn);
}
?>
