<?php
// nato_program.php
// Zobrazí program z https://mobile.natodays.cz/public/mobile/DN25/program.json
// Uložte do webserveru (PHP 7+). 😀

$programUrl = "https://mobile.natodays.cz/public/mobile/DN25/program.json";

// funkce pro získání obsahu (file_get_contents fallback -> curl)
function fetchUrl(string $url, int $timeout = 10): ?string {
    // 1) file_get_contents (pokud povoleno)
    $ctx = stream_context_create([
        'http' => ['timeout' => $timeout],
        'https'=> ['timeout' => $timeout],
    ]);
    $content = @file_get_contents($url, false, $ctx);
    if ($content !== false) return $content;

    // 2) fallback na cURL
    if (function_exists('curl_init')) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        $res = curl_exec($ch);
        curl_close($ch);
        if ($res !== false) return $res;
    }
    return null;
}

$json = fetchUrl($programUrl);
if ($json === null) {
    echo "<h1>Chyba: Nelze načíst program.json 😕</h1>";
    echo "<p>Zkontroluj připojení a URL: <code>" . htmlspecialchars($programUrl) . "</code></p>";
    exit;
}

$data = json_decode($json, true);
if ($data === null) {
    echo "<h1>Chyba: Neplatný JSON 😵‍💫</h1>";
    echo "<pre>" . htmlspecialchars($json) . "</pre>";
    exit;
}

// Některé základní informace
$title_cs = $data['name_cs'] ?? ($data['name_en'] ?? 'Program');
$code = $data['code'] ?? '';

function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }

// HTML hlavička + jednoduché CSS
?><!doctype html>
<html lang="cs">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= h($title_cs) ?> — Program</title>
<style>
    body{font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial; margin:18px; background:#f7f9fb; color:#111;}
    h1,h2{margin:6px 0;}
    .meta{color:#666; margin-bottom:12px;}
    .day {margin:18px 0; background:#fff; padding:10px; border-radius:8px; box-shadow:0 1px 4px rgba(0,0,0,0.05);}
    table{width:100%; border-collapse:collapse; margin-top:8px;}
    th, td{padding:8px 10px; border-bottom:1px solid #eee; text-align:left; vertical-align:top; font-size:14px;}
    th{background:#fafafa; font-weight:600; color:#333; position:sticky; top:0}
    .time{white-space:nowrap; width:120px;}
    .thumb{width:120px; max-width:120px;}
    .flag{display:inline-block; padding:3px 6px; border-radius:6px; font-weight:600; font-size:12px; margin-right:6px; background:#eef; color:#115;}
    .canceled{background:#ffecec; color:#a00; padding:4px 8px; border-radius:6px; font-weight:700;}
    .break{background:#fff3cd; color:#7a5a00; padding:4px 8px; border-radius:6px; font-weight:700;}
    .nations{font-size:13px;color:#444}
    .unit{color:#444; font-style:italic}
    .imgwrap img{max-width:100%; border-radius:6px;}
    @media (max-width:700px){
        .thumb{display:none;}
    }
</style>
</head>
<body>
<h1>📅 <?= h($title_cs) ?> (<?= h($code) ?>)</h1>
<div class="meta">Zdroj: <a href="<?= h($programUrl) ?>"><?= h($programUrl) ?></a> • Zobrazeno: <?= date('j. n. Y H:i') ?></div>

<?php
// pole dnů - pokud jich není, vypíšeme celý JSON (fallback)
$days = $data['days'] ?? null;
if (!$days || !is_array($days)) {
    echo "<pre>" . h(json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)) . "</pre>";
    exit;
}

// projdeme každý den
foreach ($days as $day) {
    $dayNameCs = $day['name_cs'] ?? '';
    $dayNameEn = $day['name_en'] ?? '';
    $dayDate = $day['date'] ?? '';
    // zobrazit datum pěkně
    try {
        $dt = new DateTime($dayDate);
        $datePretty = $dt->format('j. n. Y'); // cz style
    } catch (Exception $e) {
        $datePretty = $dayDate;
    }
    echo "<div class='day'>";
    echo "<h2>🗓️ " . h($dayNameCs) . " <small style='color:#666'>(" . h($dayNameEn) . ")</small></h2>";
    echo "<div style='color:#666;margin-bottom:6px;'>Datum: <strong>" . h($datePretty) . "</strong></div>";

    $activities = $day['activities'] ?? [];
    if (empty($activities)) {
        echo "<p>Žádné aktivity.</p>";
        echo "</div>";
        continue;
    }

    echo "<table>";
    echo "<thead><tr>
            <th class='time'>Čas ⏱️</th>
            <th>Název (cs / en)</th>
            <th class='unit'>Jednotka</th>
            <th class='nations'>Národy</th>
            <th class='thumb'>Obrázek</th>
          </tr></thead>";
    echo "<tbody>";
    foreach ($activities as $act) {
        $begin = $act['begin'] ?? '';
        $end = $act['end'] ?? '';
        $begin_iso = $act['begin_iso'] ?? '';
        $end_iso = $act['end_iso'] ?? '';
        $name_cs = $act['name_cs'] ?? '';
        $name_en = $act['name_en'] ?? '';
        $unit_cs = $act['unit_cs'] ?? '';
        $unit_en = $act['unit_en'] ?? '';
        $nations = $act['nations'] ?? [];
        $img = $act['thumb'] ?? ($act['image'] ?? '');
        $isCanceled = (isset($act['canceled']) && (string)$act['canceled'] !== '0');
        $isBreak = !empty($act['break']) && $act['break'];

        // prepared fields
        $timeCell = h($begin) . ($end ? ' — ' . h($end) : '');
        $nameCell = "<strong>" . h($name_cs) . "</strong><div style='color:#666;margin-top:6px;'>" . h($name_en) . "</div>";
        $unitCell = h($unit_cs ?: $unit_en);
        $nationsCell = $nations ? h(implode(', ', $nations)) : '';

        echo "<tr>";
        echo "<td class='time'>" . $timeCell;
        if ($isCanceled) echo "<div class='canceled' style='margin-top:6px;'>❌ Zrušeno</div>";
        if ($isBreak) echo "<div class='break' style='margin-top:6px;'>☕ Pauza</div>";
        echo "</td>";

        echo "<td>" . $nameCell . "</td>";
        echo "<td class='unit'>" . $unitCell . "</td>";
        echo "<td class='nations'>" . $nationsCell . "</td>";

        echo "<td class='thumb'>";
        if ($img) {
            // obrázek klikací (otevře originál)
            $imgClean = h($img);
            echo "<div class='imgwrap'><a href='{$imgClean}' target='_blank' rel='noopener'><img src='{$imgClean}' alt='thumb' style='max-width:120px;'></a></div>";
        } else {
            echo "<span style='color:#999'>—</span>";
        }
        echo "</td>";

        echo "</tr>";
    }
    echo "</tbody></table>";
    echo "</div>"; // .day
}

?>
</body>
</html>

