<?php
include '../db.php';
include 'auth.php';  

require_right('Create User');



if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $email = trim($_POST['email']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $roleId = $_POST['role_id'];

    if (!empty($username) && !empty($email) && !empty($password) && !empty($roleId)) {
        $stmt = $conn->prepare("INSERT INTO users (username, email, password, role_id) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("sssi", $username, $email, $password, $roleId);
        $stmt->execute();
    }

    header("Location: ../users.php?success=user_created");
    exit();
}
?>
