<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Akkurtcast - Raporlar</title>
    <link rel="icon" type="image/png" href="/static/img/icon.png">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
    <style>
        :root { --sidebar-bg: #1a1d21; --sidebar-hover: #2a2e35; --sidebar-text: #a8b2c0; --page-bg: #f0f2f5; --card-border: #e2e8f0; --accent: #0d6efd; }
        * { box-sizing: border-box; }
        body { background: var(--page-bg); margin: 0; font-family: system-ui, -apple-system, sans-serif; }
        .sidebar { min-height: 100vh; background: var(--sidebar-bg); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
        .brand { display: flex; align-items: center; gap: 10px; padding: 20px 18px 16px; color: #fff; font-weight: 800; font-size: 1.05rem; letter-spacing: -0.3px; border-bottom: 1px solid #2a2e35; flex-shrink: 0; }
        .brand img { width: 46px; height: 46px; object-fit: contain; border-radius: 8px; }
        .brand-mark { display: flex; flex-direction: column; min-width: 0; line-height: 1.05; }
        .brand-name { color: #f8fafc; font-size: 1.18rem; font-weight: 850; letter-spacing: .2px; text-shadow: 0 1px 0 rgba(0,0,0,.25); }
        .brand-subtitle { margin-top: 4px; color: #7dd3fc; font-size: .63rem; font-weight: 700; letter-spacing: 1.25px; text-transform: uppercase; }
        .nav-section-label { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: #4a5568; padding: 14px 18px 4px; }
        .sidebar a { color: var(--sidebar-text); display: flex; align-items: center; gap: 8px; padding: 9px 18px; text-decoration: none; font-size: 0.875rem; font-weight: 500; border-left: 3px solid transparent; transition: all 0.15s; }
        .sidebar a:hover { background: var(--sidebar-hover); color: #fff; }
        .sidebar a.active { background: rgba(13,110,253,0.12); color: #4d9fff; border-left-color: var(--accent); }
        .sidebar a .nav-icon { width: 22px; height: 22px; min-width: 22px; border-radius: 7px; display: inline-flex; align-items: center; justify-content: center; color: #fff; font-size: .78rem; line-height: 1; box-shadow: inset 0 1px 0 rgba(255,255,255,.18), 0 6px 14px rgba(0,0,0,.18); }
.nav-icon-panel { background: linear-gradient(135deg,#2563eb,#38bdf8); }
.nav-icon-music { background: linear-gradient(135deg,#7c3aed,#ec4899); }
.nav-icon-jingle { background: linear-gradient(135deg,#f59e0b,#f97316); }
.nav-icon-library { background: linear-gradient(135deg,#0f766e,#22c55e); }
.nav-icon-calendar { background: linear-gradient(135deg,#dc2626,#fb7185); }
.nav-icon-automation { background: linear-gradient(135deg,#475569,#94a3b8); }
.nav-icon-news { background: linear-gradient(135deg,#0891b2,#06b6d4); }
.nav-icon-broadcast { background: linear-gradient(135deg,#4338ca,#818cf8); }
.nav-icon-dj { background: linear-gradient(135deg,#111827,#64748b); }
.nav-icon-mobile { background: linear-gradient(135deg,#0e7490,#14b8a6); }
.nav-icon-reports { background: linear-gradient(135deg,#16a34a,#84cc16); }
.nav-icon-widget { background: linear-gradient(135deg,#9333ea,#22d3ee); }
.nav-icon-backup { background: linear-gradient(135deg,#6b7280,#d97706); }
.nav-icon-settings { background: linear-gradient(135deg,#334155,#0ea5e9); }
.nav-icon-logout { background: linear-gradient(135deg,#7f1d1d,#ef4444); }
.sidebar a:hover .nav-icon, .sidebar a.active .nav-icon { transform: translateY(-1px); box-shadow: inset 0 1px 0 rgba(255,255,255,.22), 0 8px 18px rgba(13,110,253,.25); }
        
        .sidebar-footer { margin-top: auto; border-top: 1px solid #2a2e35; padding: 8px 0; }
        .topbar { background: #fff; border-bottom: 1px solid var(--card-border); padding: 12px 24px; display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 100; }
        .topbar-title { font-weight: 700; font-size: 0.95rem; color: #1a202c; }
        .listener-chart { width: 100%; height: 220px; display: block; }
        .panel { background: #fff; border: 1px solid var(--card-border); border-radius: 8px; }
        .pager { display: flex; justify-content: flex-end; align-items: center; gap: 8px; padding: 10px 12px; border-top: 1px solid #edf0f3; }
        .listener-map { position: relative; height: 360px; overflow: hidden; border-radius: 6px; background: #e9f4fb; border: 1px solid #dbe5ee; }
        .listener-map .leaflet-container { width: 100%; height: 100%; font-family: inherit; }
        .listener-marker {
            position: relative;
            display: block;
            width: 30px;
            height: 30px;
            transform: rotate(-45deg);
            border-radius: 50% 50% 50% 0;
            background: linear-gradient(145deg, #62c6d6, #2f9fb3);
            border: 2px solid #fff;
            box-shadow: 0 2px 3px rgba(18,38,63,.18), 0 9px 20px rgba(18,38,63,.28);
        }
        .listener-marker::before {
            content: "";
            position: absolute;
            inset: 7px;
            border-radius: 50%;
            background: #fff;
            box-shadow: inset 0 0 0 3px rgba(47,159,179,.22);
        }
        .listener-marker::after {
            content: "";
            position: absolute;
            width: 18px;
            height: 6px;
            left: 4px;
            bottom: -9px;
            transform: rotate(45deg);
            border-radius: 50%;
            background: rgba(18,38,63,.20);
            filter: blur(2px);
        }
        .map-empty { position: absolute; inset: 0; z-index: 500; display: grid; place-items: center; text-align: center; color: #6c757d; padding: 24px; pointer-events: none; background: rgba(255,255,255,.70); }
        @media (max-width: 767px) {
            .sidebar { min-height: auto; height: auto; position: static; flex-direction: row; overflow-x: auto; overflow-y: hidden; white-space: nowrap; }
            .brand { border-bottom: none; border-right: 1px solid #2a2e35; padding: 12px 14px; }
            .nav-section-label { display: none; }
            .sidebar a { padding: 12px 14px; border-left: none; border-bottom: 3px solid transparent; }
            .sidebar a.active { border-left-color: transparent; border-bottom-color: var(--accent); }
            .sidebar-footer { margin-top: 0; border-top: none; }
            .topbar { padding: 10px 16px; }
        }
    
        /* Mobile shell fix: keep the sidebar from squeezing content on phones. */
        @media (max-width: 767px) {
            html, body { width: 100%; max-width: 100%; overflow-x: hidden; }
            .container-fluid > .row.g-0 { display: block; width: 100%; margin: 0; }
            aside.sidebar,
            .sidebar {
                width: 100% !important;
                min-width: 0 !important;
                max-width: 100% !important;
                min-height: auto !important;
                height: auto !important;
                position: relative !important;
                top: auto !important;
                flex-direction: row !important;
                align-items: stretch;
                overflow-x: auto;
                overflow-y: hidden;
                white-space: nowrap;
                -webkit-overflow-scrolling: touch;
            }
            .brand { min-width: 188px; padding: 10px 12px !important; }
            .brand img { width: 40px !important; height: 40px !important; }
            .brand-name { font-size: 1rem !important; }
            .brand-subtitle { font-size: .55rem !important; letter-spacing: .9px !important; }
            .sidebar a { flex: 0 0 auto; padding: 12px 10px !important; }
            .sidebar a .nav-icon { width: 28px; height: 28px; min-width: 28px; }
            .sidebar-footer { display: flex; flex: 0 0 auto; }
            main.col,
            main[class*="col"] {
                display: block;
                width: 100% !important;
                max-width: 100% !important;
                flex: 0 0 100% !important;
                min-width: 0 !important;
            }
            .topbar { position: sticky; top: 0; z-index: 50; padding: 10px 14px !important; }
            .topbar-title { font-size: 1rem; }
            .topbar-status { font-size: .75rem; padding: 4px 9px; }
            main > .p-4 { padding: 14px !important; }
            .card-panel { border-radius: 10px; }
            .card-panel-header { align-items: flex-start; flex-direction: column; padding: 14px !important; }
            .card-panel-header > .d-flex,
            .card-panel-header .d-flex { flex-wrap: wrap; }
            .card-panel-body { padding: 14px !important; }
            .broadcast-controls { display: grid !important; grid-template-columns: 1fr; gap: 10px; }
            .broadcast-controls .action-btn { width: 100%; min-height: 44px; }
            .stream-row,
            .stream-actions { flex-direction: column !important; align-items: stretch !important; gap: 10px !important; }
            .stream-url,
            .stream-row a { max-width: 100%; overflow-wrap: anywhere; word-break: break-word; }
            .history-item { align-items: flex-start; }
            .history-time { margin-left: 0; white-space: normal; }
            .now-playing-card { padding: 16px !important; }
            .next-track-box .next-path { white-space: normal; overflow-wrap: anywhere; }
        }
    </style>
</head>
<body>
<div class="container-fluid p-0">
    <div class="row g-0">
        <aside class="col-12 col-md-auto sidebar p-0" style="width: 220px; min-width: 220px;">
            <div class="brand">
                <img src="/static/img/akkurtcast-badge.png" alt="Akkurtcast">
                <span class="brand-mark"><span class="brand-name">Akkurtcast</span><span class="brand-subtitle">Broadcast Panel</span></span>
            </div>
            <div class="nav-section-label">Yayın</div>
            <a href="/"><span class="nav-icon nav-icon-panel"><i class="bi bi-house-door-fill"></i></span> Panel</a>
            <a href="/muzik"><span class="nav-icon nav-icon-music"><i class="bi bi-music-note-beamed"></i></span> Müzik listesi</a>
            <a href="/jingle"><span class="nav-icon nav-icon-jingle"><i class="bi bi-bell-fill"></i></span> Jingle listesi</a>
            <a href="/kutuphane"><span class="nav-icon nav-icon-library"><i class="bi bi-folder-fill"></i></span> Kütüphane</a>
            <div class="nav-section-label">Planlama</div>
            <a href="/takvim"><span class="nav-icon nav-icon-calendar"><i class="bi bi-calendar-week-fill"></i></span> Takvim</a>
            <a href="/otomasyon"><span class="nav-icon nav-icon-automation"><i class="bi bi-sliders"></i></span> Otomasyon</a>
            <a href="/haber-bulteni"><span class="nav-icon nav-icon-news"><i class="bi bi-newspaper"></i></span> Haber Bülteni</a>
            <div class="nav-section-label">Yayın Araçları</div>
            <a href="/naklen-yayin"><span class="nav-icon nav-icon-broadcast"><i class="bi bi-broadcast-pin"></i></span> Naklen Yayın</a>
            <a href="/dj"><span class="nav-icon nav-icon-dj"><i class="bi bi-headphones"></i></span> DJ</a>
            <a href="/mobil"><span class="nav-icon nav-icon-mobile"><i class="bi bi-phone-fill"></i></span> Mobil</a>
            <div class="nav-section-label">Sistem</div>
            <a class="active" href="/raporlar"><span class="nav-icon nav-icon-reports"><i class="bi bi-bar-chart-fill"></i></span> Raporlar</a>
            <a href="/widgetler"><span class="nav-icon nav-icon-widget"><i class="bi bi-puzzle-fill"></i></span> Widget</a>
            <a href="/yedek"><span class="nav-icon nav-icon-backup"><i class="bi bi-device-hdd-fill"></i></span> Yedek</a>
            <a href="/ayarlar"><span class="nav-icon nav-icon-settings"><i class="bi bi-gear-fill"></i></span> Ayarlar</a>
            <a href="/akis"><span class="nav-icon nav-icon-calendar"><i class="bi bi-list-check"></i></span> Yayın Akışı</a>
            <div class="sidebar-footer">
                <a href="/logout"><span class="nav-icon nav-icon-logout"><i class="bi bi-box-arrow-right"></i></span> Çıkış</a>
            </div>
        </aside>
        <main class="col p-0" style="min-width: 0;">
            <div class="topbar">
                <div class="topbar-title">Raporlar</div>
                <span class="text-muted small ms-auto">{{ durum }}</span>
            </div>
            <div class="p-4">
                <div class="row g-3 mb-3">
                    <div class="col-md-4">
                        <section class="panel p-4 h-100">
                            <div class="text-muted small">Anlık dinleyici</div>
                            <div class="display-6 fw-bold">{{ listeners }}</div>
                        </section>
                    </div>
                    <div class="col-md-8">
                        <section class="panel p-4 h-100">
                            <div class="fw-bold mb-2">Disk</div>
                            <div class="progress mb-2" style="height: 10px;"><div class="progress-bar" style="width: {{ disk.percent }}%"></div></div>
                            <div class="small text-muted">{{ disk.used_gb }} GB kullanıldı · {{ disk.free_gb }} GB boş · toplam {{ disk.total_gb }} GB</div>
                        </section>
                    </div>
                </div>
                <section class="panel p-4 mb-3">
                    <div class="d-flex flex-column flex-md-row justify-content-between gap-2 mb-3">
                        <div>
                            <h2 class="h5 mb-1">Dinleyici Grafiği</h2>
                            <div class="text-muted small">Son kayıtlar üzerinden anlık dinleyici trendi</div>
                        </div>
                        <div class="small text-muted">Zirve {{ listener_chart.peak }} · Ortalama {{ listener_chart.avg }}</div>
                    </div>
                    <canvas id="listenerChart" class="listener-chart"></canvas>
                </section>
                <section class="panel p-4 mb-3">
                    <h2 class="h5 mb-3">Yayın Sağlığı</h2>
                    <div class="row g-3">
                        <div class="col-md-2"><div class="text-muted small">Sunucu</div><div class="fw-bold">{{ health.container }}</div></div>
                        <div class="col-md-2"><div class="text-muted small">Kaynak</div><div class="fw-bold">{{ health.source }}</div></div>
                        <div class="col-md-2"><div class="text-muted small">Kalan</div><div class="fw-bold">{{ health.remaining }}</div></div>
                        <div class="col-md-3"><div class="text-muted small">Zamanlayıcı</div><div class="fw-bold">{{ health.scheduler }}</div></div>
                        <div class="col-md-3"><div class="text-muted small">Son jingle</div><div class="fw-bold">{{ health.last_jingle }}</div></div>
                    </div>
                    <div class="row g-3 mt-2">
                        <div class="col-md-3"><div class="text-muted small">Son işlem</div><div class="fw-bold">{{ health.last_action }}</div></div>
                        <div class="col-md-3"><div class="text-muted small">İşlem zamanı</div><div class="fw-bold">{{ health.last_action_time }}</div></div>
                        <div class="col-md-3"><div class="text-muted small">İşlem IP</div><div class="fw-bold">{{ health.last_action_ip }}</div></div>
                        <div class="col-md-3"><div class="text-muted small">Durdurma/engellenen</div><div class="fw-bold">{{ health.stop_count }}/{{ health.blocked_count }}</div></div>
                    </div>
                    <div class="small text-muted mt-2">Son jingle sonucu: {{ health.last_jingle_result }}</div>
                </section>
                <div class="row g-3 mb-3">
                    <div class="col-md-3"><section class="panel p-3 h-100"><div class="text-muted small">Son 500 kayıtta şarkı</div><div class="h3 mb-0">{{ report_summary.track_total }}</div></section></div>
                    <div class="col-md-3"><section class="panel p-3 h-100"><div class="text-muted small">Son 500 kayıtta jingle</div><div class="h3 mb-0">{{ report_summary.jingle_total }}</div></section></div>
                    <div class="col-md-6"><section class="panel p-3 h-100"><div class="text-muted small">Dinleyici zirvesi</div><div class="h5 mb-0">{{ report_summary.listener_peak.listeners or 0 }} dinleyici · {{ report_summary.listener_peak.time or '-' }}</div></section></div>
                </div>
                <section class="panel p-4 mb-3">
                    <div class="d-flex flex-column flex-lg-row justify-content-between gap-2 mb-3">
                        <div>
                            <h2 class="h5 mb-1">Dinleyici Haritası</h2>
                            <div class="text-muted small">Icecast anlık dinleyici IP listesinden ülke/şehir tahmini yapılır. Yerel veya gizli IP'ler konumsuz görünür.</div>
                        </div>
                        <span class="badge text-bg-primary align-self-start">{{ listener_map.total }} anlık bağlantı</span>
                    </div>
                    <div class="row g-3">
                        <div class="col-lg-7">
                            <div class="listener-map">
                                <div id="listenerMap"></div>
                                {% if not listener_map.points %}
                                <div class="map-empty">
                                    <div>
                                        <div class="fw-bold">Konum verisi bekleniyor</div>
                                        <div class="small">Dinleyici geldiğinde public IP'ler haritada nokta olarak görünür.</div>
                                    </div>
                                </div>
                                {% endif %}
                            </div>
                        </div>
                        <div class="col-lg-5">
                            <div class="row g-3">
                                <div class="col-md-6 col-lg-12">
                                    <div class="fw-bold mb-2">Ülke dağılımı</div>
                                    <table class="table table-sm mb-0">
                                        <tbody>
                                        {% for name, count in listener_map.countries %}
                                        <tr><td>{{ name }}</td><td class="text-end">{{ count }}</td></tr>
                                        {% else %}
                                        <tr><td class="text-muted">Henüz veri yok.</td></tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                                <div class="col-md-6 col-lg-12">
                                    <div class="fw-bold mb-2">Şehirler</div>
                                    <table class="table table-sm mb-0">
                                        <tbody>
                                        {% for name, count in listener_map.cities %}
                                        <tr><td>{{ name }}</td><td class="text-end">{{ count }}</td></tr>
                                        {% else %}
                                        <tr><td class="text-muted">Şehir verisi yok.</td></tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% if listener_map.clients %}
                    <div class="table-responsive mt-3">
                        <table class="table table-sm mb-0">
                            <thead><tr><th>IP</th><th>Konum</th><th>Bağlantı</th><th>Player</th></tr></thead>
                            <tbody>
                            {% for item in listener_map.clients %}
                            <tr>
                                <td>{{ item.ip }}</td>
                                <td>{{ item.city }}, {{ item.country }}</td>
                                <td>{{ item.connected or '-' }}</td>
                                <td class="text-truncate" style="max-width: 360px;">{{ item.user_agent }}</td>
                            </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% endif %}
                </section>
                <div class="row g-3 mb-3">
                    <div class="col-lg-6">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom fw-bold">En çok çalan şarkılar</div>
                            <table class="table table-sm mb-0"><tbody>{% for name, count in report_summary.top_tracks %}<tr><td>{{ name }}</td><td class="text-end">{{ count }}</td></tr>{% else %}<tr><td class="text-muted">Kayıt yok.</td></tr>{% endfor %}</tbody></table>
                        </section>
                    </div>
                    <div class="col-lg-6">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom fw-bold">En çok çalan sanatçılar</div>
                            <table class="table table-sm mb-0"><tbody>{% for name, count in report_summary.top_artists %}<tr><td>{{ name }}</td><td class="text-end">{{ count }}</td></tr>{% else %}<tr><td class="text-muted">Kayıt yok.</td></tr>{% endfor %}</tbody></table>
                        </section>
                    </div>
                </div>
                <div class="row g-3 mb-3">
                    <div class="col-lg-6">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom fw-bold">Jingle denetimi</div>
                            <div class="px-3 py-2 small text-muted">Toplam {{ jingle_summary.total }} kayıt · başarılı {{ jingle_summary.success }} · sorunlu {{ jingle_summary.failed }}</div>
                            <div class="table-responsive">
                                <table class="table table-sm mb-0">
                                    <thead><tr><th>Zaman</th><th>Jingle</th><th>Olay</th></tr></thead>
                                    <tbody data-paginate="jingle" data-page-size="20">
                                    {% for item in jingle_audit %}
                                    <tr><td>{{ item.time }}</td><td>{{ item.name }}</td><td>{{ item.event }}{% if item.reason %}<br><span class="text-muted">{{ item.reason }}</span>{% endif %}</td></tr>
                                    {% else %}
                                    <tr><td colspan="3" class="text-muted">Henüz jingle kaydı yok.</td></tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            <div class="pager" data-pager-for="jingle"></div>
                        </section>
                    </div>
                    <div class="col-lg-6">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom fw-bold">Son yayın geçmişi</div>
                            <div class="px-3 py-2 small text-muted">Panel artık son {{ play_history|length }} kaydı gösterir; jingle kayıtları da görünür.</div>
                            <div class="table-responsive">
                                <table class="table table-sm mb-0">
                                    <thead><tr><th>Zaman</th><th>Parça</th><th>Kaynak</th></tr></thead>
                                    <tbody data-paginate="play" data-page-size="20">
                                    {% for item in play_history %}
                                    <tr><td>{{ item.time }}</td><td>{{ item.title }}<br><span class="text-muted">{{ item.artist }}</span></td><td>{{ item.source }}</td></tr>
                                    {% else %}
                                    <tr><td colspan="3" class="text-muted">Henüz yayın geçmişi yok.</td></tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            <div class="pager" data-pager-for="play"></div>
                        </section>
                    </div>
                </div>
                <section class="panel mb-3">
                    <div class="px-3 py-3 border-bottom fw-bold">Sunucu işlem günlüğü</div>
                    <div class="table-responsive">
                        <table class="table table-sm mb-0">
                            <thead><tr><th>Zaman</th><th>İşlem</th><th>Sonuç</th><th>IP</th><th>Adres</th></tr></thead>
                            <tbody data-paginate="actions" data-page-size="30">
                            {% for item in server_actions %}
                            <tr>
                                <td>{{ item.time }}</td>
                                <td>{{ item.action }}</td>
                                <td>{% if item.success %}Başarılı{% else %}Engellendi/Hata{% endif %}{% if item.reason %}<br><span class="text-muted">{{ item.reason }}</span>{% endif %}</td>
                                <td>{{ item.ip }}</td>
                                <td>{{ item.path }}</td>
                            </tr>
                            {% else %}
                            <tr><td colspan="5" class="text-muted">Henüz sunucu işlem kaydı yok.</td></tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    <div class="pager" data-pager-for="actions"></div>
                </section>
                <section class="panel">
                    <div class="px-3 py-3 border-bottom fw-bold">Dinleyici geçmişi</div>
                    <div class="table-responsive">
                        <table class="table mb-0 align-middle">
                            <thead><tr><th>Zaman</th><th>Dinleyici</th></tr></thead>
                            <tbody data-paginate="listeners" data-page-size="30">
                            {% for item in history|reverse %}
                                <tr><td>{{ item.time }}</td><td>{{ item.listeners }}</td></tr>
                            {% else %}
                                <tr><td colspan="2" class="text-muted">Henüz rapor kaydı yok.</td></tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    <div class="pager" data-pager-for="listeners"></div>
                </section>
            </div>
        </main>
    </div>
</div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
const listenerChartData = {{ listener_chart|tojson }};
const listenerChartCanvas = document.getElementById('listenerChart');
if (listenerChartCanvas) {
    const ctx = listenerChartCanvas.getContext('2d');
    const ratio = window.devicePixelRatio || 1;
    const rect = listenerChartCanvas.getBoundingClientRect();
    listenerChartCanvas.width = Math.max(1, Math.floor(rect.width * ratio));
    listenerChartCanvas.height = Math.max(1, Math.floor(rect.height * ratio));
    ctx.scale(ratio, ratio);
    const width = rect.width;
    const height = rect.height;
    const pad = 28;
    const values = listenerChartData.values || [];
    const labels = listenerChartData.labels || [];
    const maxValue = Math.max(1, ...values);
    ctx.clearRect(0, 0, width, height);
    ctx.strokeStyle = '#e2e8f0';
    ctx.lineWidth = 1;
    for (let i = 0; i <= 4; i += 1) {
        const y = pad + (height - pad * 2) * (i / 4);
        ctx.beginPath();
        ctx.moveTo(pad, y);
        ctx.lineTo(width - pad, y);
        ctx.stroke();
    }
    if (values.length) {
        ctx.strokeStyle = '#0d6efd';
        ctx.lineWidth = 2.5;
        ctx.beginPath();
        values.forEach((value, index) => {
            const x = pad + (width - pad * 2) * (values.length === 1 ? 0 : index / (values.length - 1));
            const y = height - pad - ((height - pad * 2) * (value / maxValue));
            if (index === 0) ctx.moveTo(x, y);
            else ctx.lineTo(x, y);
        });
        ctx.stroke();
        ctx.fillStyle = '#0d6efd';
        values.forEach((value, index) => {
            const x = pad + (width - pad * 2) * (values.length === 1 ? 0 : index / (values.length - 1));
            const y = height - pad - ((height - pad * 2) * (value / maxValue));
            ctx.beginPath();
            ctx.arc(x, y, 3, 0, Math.PI * 2);
            ctx.fill();
        });
    }
    ctx.fillStyle = '#64748b';
    ctx.font = '12px system-ui';
    ctx.fillText(String(maxValue), 4, pad + 4);
    ctx.fillText('0', 10, height - pad + 4);
    if (labels.length) {
        ctx.fillText(labels[0], pad, height - 6);
        ctx.fillText(labels[labels.length - 1], Math.max(pad, width - pad - 38), height - 6);
    }
}

const listenerPoints = {{ listener_map.points|tojson }};
const listenerMapEl = document.getElementById('listenerMap');
if (listenerMapEl && window.L) {
    const start = listenerPoints.length ? [listenerPoints[0].lat, listenerPoints[0].lon] : [39, 35];
    const map = L.map(listenerMapEl, {
        center: start,
        zoom: listenerPoints.length ? 5 : 3,
        scrollWheelZoom: true,
        dragging: true,
        worldCopyJump: true
    });
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: '&copy; OpenStreetMap'
    }).addTo(map);
    const markerIcon = L.divIcon({
        className: '',
        html: '<span class="listener-marker"></span>',
        iconSize: [30, 42],
        iconAnchor: [15, 30],
        popupAnchor: [0, -30]
    });
    const markers = listenerPoints.map((point) => (
        L.marker([point.lat, point.lon], { icon: markerIcon })
            .bindPopup(`<strong>${point.label}</strong><br>${point.ip}`)
            .addTo(map)
    ));
    if (markers.length > 1) {
        const bounds = L.latLngBounds(markers.map((marker) => marker.getLatLng()));
        map.fitBounds(bounds, { padding: [36, 36], maxZoom: 8 });
    }
}

function setupPager(name) {
    const tbody = document.querySelector(`[data-paginate="${name}"]`);
    const pager = document.querySelector(`[data-pager-for="${name}"]`);
    if (!tbody || !pager) return;
    const rows = Array.from(tbody.querySelectorAll('tr'));
    const pageSize = Number(tbody.dataset.pageSize || 20);
    if (rows.length <= pageSize) {
        pager.style.display = 'none';
        return;
    }
    let page = 1;
    const totalPages = Math.ceil(rows.length / pageSize);
    function render() {
        rows.forEach((row, index) => {
            row.style.display = index >= (page - 1) * pageSize && index < page * pageSize ? '' : 'none';
        });
        pager.innerHTML = `
            <button class="btn btn-outline-secondary btn-sm" ${page === 1 ? 'disabled' : ''} data-dir="-1">Önceki</button>
            <span class="small text-muted">${page} / ${totalPages}</span>
            <button class="btn btn-outline-secondary btn-sm" ${page === totalPages ? 'disabled' : ''} data-dir="1">Sonraki</button>
        `;
        pager.querySelectorAll('button').forEach((button) => {
            button.addEventListener('click', () => {
                page = Math.min(totalPages, Math.max(1, page + Number(button.dataset.dir)));
                render();
            });
        });
    }
    render();
}
['jingle', 'play', 'actions', 'listeners'].forEach(setupPager);
</script>
<script src="/static/js/mobile-nav.js"></script>
</body>
</html>














