<?php
session_start();
require_once('../connection.php'); 

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $email = $_POST['email'];
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $password = $_POST['password'];
    
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

    try {

    $stmt = $conn->prepare("INSERT INTO customers (email, first_name, last_name, password) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $email, $first_name, $last_name, $hashedPassword);
    

        if ($stmt->execute()) {
            $_SESSION ['userID'] = $stmt->insert_id;
            $_SESSION ['email'] = $email;
            $_SESSION['first_name'] = $first_name;
            echo "<script>
                alert('You have successfully created an account.');
                window.location.href = '../dashboard/dashboard.php';
              </script>";
            exit();
        } else {
        echo "<script>
                alert('An account already exists with that email address.');
                window.location.href = '../login/login.html';
              </script>";
            exit();
        }
    } catch (Exception $e) {
    }
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GLH | Register</title>
</head>
</html>