<?php
require_once '../config/db_config.php';
$conn = getDbConnection();
if (!$conn) {
    die("Connection failed: Database not found");
}

// Ambil data dari form
$username = $_POST['username'];
$name = $_POST['name'];
$email = $_POST['email'];
$raw_password = $_POST['password'];

// Enkripsi password
$hashed_password = password_hash($raw_password, PASSWORD_DEFAULT);

// Simpan ke database
try {
    $sql = "INSERT INTO users (username, name, email, password, role) VALUES (:username, :name, :email, :password, 'customer')";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':password', $hashed_password);
    if ($stmt->execute()) {
        header('Location: daftar.php?success=1');
        exit;
    } else {
        echo "Error: Gagal membuat akun.";
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>
