<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/createrole.php';

$database = new Database();
$db = $database->getConnection();
$userrole = new UserRole($db);
$data = json_decode(file_get_contents("php://input"), true) ?? [];

if (empty($data['rolename'])) exit('error');

$userrole->rolename = $data['rolename'];

foreach ($data as $key => $value) {
    if ($key !== 'rolename') $userrole->$key = $value ?? 0;
}

echo $userrole->exist() ? 'duplicate' : ($userrole->create() ? 'created' : 'error');
?>
