<?php


$tpAnnSig = '';
$tpAnnHtml = '';
if (class_exists('AnnouncementService')) {
    try {
        $tpAnnList = AnnouncementService::cached();
        $tpAnnSig  = AnnouncementService::signature($tpAnnList);
        $tpAnnHtml = AnnouncementService::renderBar($tpAnnList);
    } catch (\Throwable $e) {
        $tpAnnSig = '';
        $tpAnnHtml = '';
    }
}
?>
<div id="tp-ann-bar" data-sig="<?= htmlspecialchars($tpAnnSig, ENT_QUOTES) ?>"><?= $tpAnnHtml ?></div>
<script>
(function () {
    var BAR = document.getElementById('tp-ann-bar');
    if (!BAR) return;

    
    
    function poll() {
        fetch('/admin/announcements/feed', {headers: {'X-Requested-With': 'XMLHttpRequest'}, credentials: 'same-origin'})
            .then(function (r) { return r.ok ? r.json() : null; })
            .then(function (d) {
                if (!d || typeof d.sig !== 'string') return;
                if (d.sig !== (BAR.getAttribute('data-sig') || '')) {
                    BAR.innerHTML = d.html || '';
                    BAR.setAttribute('data-sig', d.sig);
                }
            })
            .catch(function () {});
    }

    poll();
    setInterval(poll, 20000);
    document.addEventListener('visibilitychange', function () { if (!document.hidden) poll(); });
})();
</script>
