<?php
session_start();
include 'config/koneksi.php';

$nama = trim($_POST['nama_lengkap']);
$email = trim($_POST['email']);
$password = $_POST['password'];

// Cek apakah email sudah terdaftar
$cek = mysqli_query($conn, "SELECT * FROM admin WHERE email = '$email'");
if (mysqli_num_rows($cek) > 0) {
    $_SESSION['error'] = "Email sudah digunakan!";
    header("Location: register.php");
    exit;
}

// Enkripsi password
$hash = password_hash($password, PASSWORD_DEFAULT);

// Simpan ke database
$query = mysqli_query($conn, "INSERT INTO admin (nama_lengkap, email, password) VALUES ('$nama', '$email', '$hash')");

if ($query) {
    $_SESSION['success'] = "Berhasil daftar. Silakan login.";
    header("Location: login.php");
    exit;
} else {
    $_SESSION['error'] = "Registrasi gagal!";
    header("Location: register.php");
    exit;
}
?>
