<?php include "db.php"; ?>

<!DOCTYPE html>
<html>
<head>
<title>Sign Up - ApexUniversalMart</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
body{
    margin:0;
    font-family:Segoe UI;
    background:#f4f4f4;
}

.container{
    display:flex;
    justify-content:center;
    align-items:center;
    height:100vh;
}

.box{
    background:white;
    width:400px;
    padding:40px;
    border-radius:10px;
    box-shadow:0 10px 30px rgba(0,0,0,0.08);
}

h2{
    text-align:center;
    color:rgb(242,139,0);
}

input{
    width:100%;
    padding:12px;
    margin-bottom:15px;
    border:1px solid #ddd;
    border-radius:6px;
}

input:focus{
    border-color:rgb(242,139,0);
    outline:none;
}

button{
    width:100%;
    padding:12px;
    background:rgb(242,139,0);
    color:white;
    border:none;
    border-radius:6px;
    font-size:16px;
    cursor:pointer;
}

button:hover{
    background:#d97c00;
}

.error{
    color:red;
    text-align:center;
    margin-bottom:10px;
}

.success{
    color:green;
    text-align:center;
    margin-bottom:10px;
}
</style>
</head>

<body>

<div class="container">
<div class="box">

<h2>Create Account</h2>

<?php
if(isset($_POST['signup'])){

    $fullname = mysqli_real_escape_string($conn, $_POST['fullname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = $_POST['password'];
    $confirm = $_POST['confirm'];

    if($password != $confirm){
        echo "<div class='error'>Passwords do not match!</div>";
    } else {

        $check = mysqli_query($conn, "SELECT * FROM users WHERE email='$email'");
        
        if(mysqli_num_rows($check) > 0){
            echo "<div class='error'>Email already exists!</div>";
        } else {

            $hashed = password_hash($password, PASSWORD_DEFAULT);

            $insert = mysqli_query($conn, 
            "INSERT INTO users(fullname,email,password) 
             VALUES('$fullname','$email','$hashed')");

            if($insert){
                echo "<div class='success'>Account created successfully!</div>";
            } else {
                echo "<div class='error'>Something went wrong!</div>";
            }
        }
    }
}
?>

<form method="POST">
    <input type="text" name="fullname" placeholder="Full Name" required>
    <input type="email" name="email" placeholder="Email Address" required>
    <input type="password" name="password" placeholder="Password" required>
    <input type="password" name="confirm" placeholder="Confirm Password" required>
    <button type="submit" name="signup">Sign Up</button>
</form>
<div class="login-link">
            Already have an account? 
            <a href="login.php">Login</a>
        </div>

</div>
</div>

</body>
</html>