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

ini_set('display_errors', 1);

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

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

ini_wkvs_session();

check_user_permission('wk_leitung');

verify_csrf();

$type = 'wkl';

$data = include $baseDir . '/../scripts/db/db-verbindung-script.php';

if ($data['success'] === false){
	echo json_encode(['success' => false, 'message' => $data['message']]);
    exit; 
}

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

$slug = $_POST['slug'] ?? '';

$allowed_slugs = ['rankLive-overview', 'rankLive-geraet', 'kampfrichter-admin', 'kampfrichter-geraet', 'rangliste'];

if (!in_array($slug, $allowed_slugs)){
    echo json_encode(['success' => false, 'message' => 'Invalid Type']);
    exit;
}

$_SESSION['current_ranklive_slug'] = $slug;

$db_res = db_select($mysqli, $db_tabelle_tabellen_konfiguration, '`json`', '`type_slug` = ?', [$slug], '', 1);

$res = $db_res[0]['json'] ?? '';

// Return JSON
echo json_encode([
    'success' => true,
    'data' => $res,
    'message' => 'gespeichert'
]);
exit;