{include file="_partials/head.tpl"}

<style>
  .rt-top{display:flex;align-items:center;gap:18px;background:linear-gradient(135deg,#1797F0,#0f79cc);color:#fff;border-radius:14px;padding:22px 24px;margin:18px 0}
  .rt-top .num{font-size:44px;font-weight:800;line-height:1}
  .rt-top .lbl{font-size:14px;opacity:.92}
  .rt-dot{width:10px;height:10px;border-radius:50%;background:#22e07a;display:inline-block;margin-right:7px;box-shadow:0 0 0 0 rgba(34,224,122,.7);animation:rtp 1.6s infinite}
  @keyframes rtp{0%{box-shadow:0 0 0 0 rgba(34,224,122,.6)}70%{box-shadow:0 0 0 9px rgba(34,224,122,0)}100%{box-shadow:0 0 0 0 rgba(34,224,122,0)}}
  .rt-hint{font-size:12.5px;color:var(--text-muted);margin:0 2px 10px}
  .rt-feed{background:var(--surface);border:1px solid var(--border);border-radius:14px;padding:6px 12px}
  .rt-item{display:flex;align-items:center;gap:12px;padding:12px 10px;border-bottom:1px solid var(--border-soft);border-left:3px solid transparent;border-radius:8px}
  .rt-item:last-child{border-bottom:0}
  .rt-ico{width:34px;height:34px;border-radius:9px;display:flex;align-items:center;justify-content:center;flex-shrink:0}
  .rt-main{flex:1;min-width:0}
  .rt-label{font-weight:600;color:var(--text);font-size:14px}
  .rt-url{color:var(--text-muted);font-size:12.5px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
  .rt-side{text-align:right;flex-shrink:0}
  .rt-time{font-variant-numeric:tabular-nums;color:var(--text-muted);font-weight:600;font-size:13px}
  .rt-tags{margin-top:4px;display:flex;gap:6px;align-items:center;justify-content:flex-end;flex-wrap:wrap}
  .rt-badge{background:var(--surface-2);color:var(--text-muted);border-radius:999px;padding:1px 8px;font-size:11px;white-space:nowrap}
  .rt-flag{width:15px;height:11px;border-radius:2px;vertical-align:-1px;margin-right:3px;box-shadow:0 0 0 1px rgba(0,0,0,.1)}
  .rt-vid{font-weight:700;font-size:11px;white-space:nowrap}
  @media(max-width:640px){.rt-url{max-width:42vw}}
</style>

<div class="page-head">
  <div><h1>Realtime</h1></div>
  <div><a href="/admin/analytics" class="btn btn-secondary">{icon name="chart" size="16"} Дашборд</a></div>
</div>

<div class="rt-top">
  <div><div class="num" id="rtOnline">0</div></div>
  <div class="lbl"><span class="rt-dot"></span>Онлайн сейчас (активность за 5 минут)</div>
</div>

<p class="rt-hint">Каждая строка — одно действие. Цвет полоски слева и метка <b>#id</b> — это посетитель: одинаковый цвет = один и тот же человек. Иконка слева — тип действия (открыл страницу / достиг цели / ушёл).</p>

<div class="rt-feed" id="rtFeed"><p class="muted" style="font-size:13px">Загрузка…</p></div>

<script>
(function () {
  var EYE  = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7z"/><circle cx="12" cy="12" r="3"/></svg>';
  var GOAL = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2.2"><path d="M5 12.5l4.5 4.5L19 7"/></svg>';
  var EXIT = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2"><path d="M10 17l5-5-5-5M15 12H3M21 4v16"/></svg>';
  var TYPES = {
    pageview: { c: '#1797F0', ic: EYE },
    event:    { c: '#16a34a', ic: GOAL },
    leave:    { c: '#94a3b8', ic: EXIT }
  };
  var VPAL = ['#1797F0','#16a34a','#f59e0b','#8b5cf6','#ef4444','#06b6d4','#ec4899','#0ea5e9','#65a30d','#f43f5e'];
  function vcolor(v) { var h = 0, i; v = String(v || ''); for (i = 0; i < v.length; i++) h = (h * 31 + v.charCodeAt(i)) >>> 0; return VPAL[h % VPAL.length]; }
  var onlineEl = document.getElementById('rtOnline');
  var feedEl = document.getElementById('rtFeed');
  function esc(s) { var d = document.createElement('div'); d.textContent = (s == null ? '' : String(s)); return d.innerHTML; }
  function load() {
    if (document.visibilityState === 'hidden') return;
    fetch('/admin/analytics/realtime/data', { headers: { 'Accept': 'application/json' } })
      .then(function (r) { return r.json(); })
      .then(function (d) {
        if (!d || !d.ok) return;
        onlineEl.textContent = d.online;
        var html = '';
        (d.feed || []).forEach(function (e) {
          var meta = TYPES[e.type] || TYPES.pageview;
          var vc = vcolor(e.vid);
          var cc = (e.country || '').toLowerCase();
          var geo = e.country
            ? ('<img class="rt-flag" src="/public/assets/analytics/flags/' + cc + '.svg" onerror="this.remove()">' + esc(e.city || e.country))
            : esc(e.device);
          var dev = e.country ? (esc(e.device) + ' · ') : '';
          var dur = e.dur ? (' · ' + esc(e.dur)) : '';
          html += '<div class="rt-item" style="border-left-color:' + vc + '">'
                + '<span class="rt-ico" style="background:' + meta.c + '">' + meta.ic + '</span>'
                + '<div class="rt-main"><div class="rt-label">' + esc(e.label) + '</div>'
                + '<div class="rt-url">' + esc(e.url || '/') + dur + '</div></div>'
                + '<div class="rt-side"><div class="rt-time">' + esc(e.time) + '</div>'
                + '<div class="rt-tags"><span class="rt-badge">' + esc(e.source) + '</span>'
                + '<span class="rt-badge">' + dev + geo + '</span>'
                + '<span class="rt-vid" style="color:' + vc + '">#' + esc(e.vid) + '</span></div></div>'
                + '</div>';
        });
        feedEl.innerHTML = html || '<p class="muted" style="font-size:13px">Пока тихо… Откройте сайт в другой вкладке — событие появится здесь.</p>';
      })
      .catch(function () {});
  }
  load();
  setInterval(load, 5000);
})();
</script>

{include file="_partials/foot.tpl"}
