<?php

$baseDir = $_SERVER['DOCUMENT_ROOT'];

require_once $baseDir . '/../scripts/session_functions.php';

ini_wkvs_session();

check_user_permission('wk_leitung');

verify_csrf();

// Validate input
if (!isset($_POST['abt']) || !ctype_digit($_POST['abt']) || !isset($_POST['newName'])) {
    http_response_code(406);
    exit;
}

$abtInput = (int) $_POST['abt'];
$new_name = trim($_POST['newName']);

$type = 'wkl';

$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';

if ($dbconnection['success'] !== true){
    echo 'Critical DB Error.';
    exit;
}

require $baseDir . '/../scripts/db/db-functions.php';
require $baseDir . '/../scripts/db/db-tables.php';


db_update(
    $mysqli,
    $db_tabelle_gruppen,
    ['name' => $new_name],
    ['id'   => $abtInput]
);

http_response_code(200);
echo json_encode(['success' => true]);
exit;