<?php
$list = $_GET['list'] ?? '';
$filepath = __DIR__ . '/ticker-listen/' . basename($list) . '.json';

if (!file_exists($filepath)) {
	echo '<p style="color:red;">Datei nicht gefunden: ' . htmlspecialchars($filepath) . '</p>';
	exit;
}

$json = file_get_contents($filepath);
$data = json_decode($json, true);

if (!is_array($data) || count($data) === 0) {
	echo '<p style="color:red;">Keine Daten gefunden.</p>';
	exit;
}

$columns = array_keys($data[0]);

echo '<div class="table-responsive"><table id="tickerTable" class="table table-striped table-bordered mb-0 w-100">';
echo '<thead class="table-dark"><tr>';
foreach ($columns as $col) {
	echo '<th>' . htmlspecialchars($col) . '</th>';
}
echo '</tr></thead><tbody>';
foreach ($data as $row) {
	echo '<tr>';
	foreach ($columns as $col) {
		echo '<td>' . htmlspecialchars($row[$col] ?? '') . '</td>';
	}
	echo '</tr>';
}
echo '</tbody></table></div>';