<?php
header('Content-Type: application/json');

if (!isset($baseDir)) {
	$baseDir = $_SERVER['DOCUMENT_ROOT'];
}

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

ini_wkvs_session();

check_user_permission('wk_leitung');

verify_csrf();

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


$type = 'wkl';

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

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

$id = intval($_POST['field_id'] ?? 0);
if ($id <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid Input.']);
    exit;
}

db_delete($mysqli, $db_tabelle_intern_benutzende, ['id' => $id]);
db_delete($mysqli, $db_tabelle_einmal_links, ['user_id' => $id]);

echo json_encode(['success' => true, 'message' =>  "Benutzer $id erfolgreich gelöscht.", 'id' => $id]);
exit;

