<?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);

// Read JSON data
$data = json_decode(file_get_contents("php://input"), true) ?? [];

// Ensure 'rolename' is passed in the request
if (empty($data['rolename'])) exit('error');

// Set the rolename
$userrole->rolename = $data['rolename'];

// Loop through all fields and assign values
foreach ($data as $key => $value) {
    if ($key !== 'rolename') {
        $userrole->$key = $value ?? 0;  // Ensure non-null values are set
    }
}

// Perform update using rolename
echo ($userrole->update() ? 'updated' : 'error');
?>
