<?php
include 'config/db.php';
include 'includes/csrf.php';

$message = '';

if(isset($_POST['register']))
{
    if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) die('Invalid request.');
    $fullname = trim($_POST['fullname']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $workplace = trim($_POST['workplace']);

    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];

    if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($password) < 8)
    {
        $message = "Enter a valid email address and a password of at least 8 characters";
    }
    elseif($password != $confirm_password)
    {
        $message = "Passwords do not match";
    }
    else
    {
        $check =
        mysqli_prepare(
        $conn,
        "SELECT id
         FROM users
         WHERE email=?"
        );

        mysqli_stmt_bind_param(
        $check,
        "s",
        $email
        );

        mysqli_stmt_execute($check);

        $result =
        mysqli_stmt_get_result($check);

        if(mysqli_num_rows($result)>0)
        {
            $message =
            "Email already exists";
        }
        else
        {
            $hash = password_hash( $password, PASSWORD_DEFAULT );

            $stmt = mysqli_prepare($conn,
            "INSERT INTO users
            (
            fullname,
            email,
            phone,
            workplace,
            password
            )
            VALUES
            (?,?,?,?,?)"
            );

            mysqli_stmt_bind_param(
            $stmt,
            "sssss",
            $fullname,
            $email,
            $phone,
            $workplace,
            $hash
            );

            mysqli_stmt_execute($stmt);

            echo "
            <script>
            alert('Registration Successful');
            window.location='login.php';
            </script>
            ";
            exit;
        }
    }
}

include 'includes/header.php';
?>

<div class="container mt-5">

    <div class="row justify-content-center">

        <div class="col-md-6">

            <div class="card shadow">

                <div class="card-header bg-primary text-white">
                User Registration
                </div>

                <div class="card-body">

                    <?php if($message!=''){ ?>

                    <div class="alert alert-danger">
                    <?= $message ?>
                    </div>

                    <?php } ?>

                    <form method="POST">

                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">

                        <div class="mb-3">
                        <label>Full Name</label>
                        <input type="text"
                        name="fullname"
                        class="form-control"
                        required>
                        </div>

                        <div class="mb-3">
                        <label>Email Address</label>
                        <input type="email"
                        name="email"
                        class="form-control"
                        required>
                        </div>

                        <div class="mb-3">
                        <label>Phone Number</label>
                        <input type="text"
                        name="phone"
                        class="form-control"
                        required>
                        </div>

                        <div class="mb-3">
                        <label>Workplace</label>
                        <input type="text"
                        name="workplace"
                        class="form-control"
                        required>
                        </div>

                        <div class="mb-3">
                        <label>Password</label>
                        <input type="password"
                        name="password"
                        class="form-control"
                        required>
                        </div>

                        <div class="mb-3">
                        <label>Confirm Password</label>
                        <input type="password"
                        name="confirm_password"
                        class="form-control"
                        required>
                        </div>

                        <button
                        type="submit"
                        name="register"
                        class="btn btn-success w-100">

                        Register

                        </button>

                    </form>

                </div>

            </div>

        </div>

    </div>

</div>

<?php include 'includes/footer.php'; ?>
