<?php
include('../db_connect.php'); // connect to database

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name     = mysqli_real_escape_string($conn, $_POST['form_name']);
    $email    = mysqli_real_escape_string($conn, $_POST['form_email']);
    $phone    = mysqli_real_escape_string($conn, $_POST['form_phone']);
    $password = mysqli_real_escape_string($conn, $_POST['form_password']); // plain password

    // Check if email or phone already exists
    $check = "SELECT * FROM users WHERE email='$email' OR phone='$phone'";
    $result = mysqli_query($conn, $check);

    if (mysqli_num_rows($result) > 0) {
        echo "<script>alert('Email or Phone already registered'); window.location.href='../frontend/sign-up.php';</script>";
    } else {
        // Store plain visible password (for testing only)
        $sql = "INSERT INTO users (first_name, email, phone, password) 
                VALUES ('$name', '$email', '$phone', '$password')";

        if (mysqli_query($conn, $sql)) {
            echo "<script>alert('Registration successful! Please login.'); window.location.href='../frontend/login.php';</script>";
        } else {
            echo "<script>alert('Database error: " . mysqli_error($conn) . "');</script>";
        }
    }
}
?>
