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



if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $roleId = $_POST['role_id'];
    $rights = $_POST['rights'] ?? [];

    // Remove old rights first
    $conn->query("DELETE FROM role_rights WHERE role_id = $roleId");

    // Insert new rights
    $stmt = $conn->prepare("INSERT INTO role_rights (role_id, right_id) VALUES (?, ?)");
    foreach ($rights as $rightId) {
        $stmt->bind_param("ii", $roleId, $rightId);
        $stmt->execute();
    }

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