<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Akkurtcast Paneli</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">
    <style>
        :root {
            --sidebar-bg: #1a1d21;
            --sidebar-hover: #2a2e35;
            --sidebar-active: #0d6efd;
            --sidebar-text: #a8b2c0;
            --topbar-bg: #fff;
            --page-bg: #f0f2f5;
            --card-bg: #fff;
            --card-border: #e2e8f0;
            --accent: #0d6efd;
            --accent-soft: #e8f0ff;
            --green: #16a34a;
            --red: #dc2626;
        }
        * { box-sizing: border-box; }
        body { background: var(--page-bg); margin: 0; font-family: system-ui, -apple-system, sans-serif; }

        /* Sidebar */
        .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 */
        .topbar {
            background: var(--topbar-bg);
            border-bottom: 1px solid var(--card-border);
            padding: 12px 24px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        .topbar-title { font-weight: 700; font-size: 0.95rem; color: #1a202c; }
        .topbar-status {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            font-size: 0.8rem;
            font-weight: 600;
            padding: 4px 10px;
            border-radius: 20px;
        }
        .topbar-status.on { background: #dcfce7; color: #15803d; }
        .topbar-status.off { background: #fee2e2; color: #b91c1c; }
        .topbar-status::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

        /* Cards */
        .card-panel {
            background: var(--card-bg);
            border: 1px solid var(--card-border);
            border-radius: 12px;
            overflow: hidden;
        }
        .card-panel-header {
            padding: 14px 20px;
            border-bottom: 1px solid var(--card-border);
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
        }
        .card-panel-header .title { font-weight: 700; font-size: 0.9rem; color: #1a202c; }
        .card-panel-header .subtitle { font-size: 0.78rem; color: #64748b; margin-top: 2px; }
        .card-panel-body { padding: 20px; }

        /* Stat boxes */
        .stat-box {
            background: var(--page-bg);
            border: 1px solid var(--card-border);
            border-radius: 8px;
            padding: 14px 16px;
        }
        .stat-box .stat-label { font-size: 0.72rem; color: #64748b; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px; }
        .stat-box .stat-value { font-size: 1.1rem; font-weight: 700; color: #1a202c; }
        .stat-box .stat-value.good { color: var(--green); }
        .stat-box .stat-value.bad { color: var(--red); }

        /* Now playing */
        .now-playing-card { background: linear-gradient(135deg, #1a1d21 0%, #2d3748 100%); color: #fff; border-radius: 12px; padding: 20px; }
        .now-playing-card .np-label { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; color: #94a3b8; margin-bottom: 6px; }
        .now-playing-card .np-title { font-size: 1.15rem; font-weight: 700; line-height: 1.3; margin-bottom: 4px; }
        .now-playing-card .np-artist { color: #94a3b8; font-size: 0.875rem; margin-bottom: 14px; }
        .progress-bar-wrap { background: rgba(255,255,255,0.1); border-radius: 4px; height: 5px; overflow: hidden; margin-bottom: 6px; }
        .progress-bar-fill { height: 100%; background: #4d9fff; border-radius: 4px; transition: width 0.5s; }
        .np-time { font-size: 0.75rem; color: #94a3b8; display: flex; justify-content: space-between; }
        .next-track-box { margin-top: 18px; padding-top: 16px; border-top: 1px solid rgba(255,255,255,0.12); }
        .next-track-box .next-label { font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: #60a5fa; margin-bottom: 6px; }
        .next-track-box .next-title { font-size: 0.95rem; font-weight: 700; line-height: 1.25; color: #f8fafc; }
        .next-track-box .next-artist { color: #94a3b8; font-size: 0.8rem; margin-top: 3px; }
        .next-track-box .next-path { color: #64748b; font-size: 0.72rem; margin-top: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
        .planned-box { margin-top: 12px; padding: 12px; border: 1px solid rgba(96,165,250,.28); border-radius: 8px; background: rgba(96,165,250,.08); }
        .planned-box .planned-label { font-size: .66rem; font-weight: 800; letter-spacing: 1px; text-transform: uppercase; color: #93c5fd; margin-bottom: 4px; }
        .planned-box .planned-title { font-size: .9rem; font-weight: 700; color: #f8fafc; }
        .planned-box .planned-meta { font-size: .74rem; color: #94a3b8; margin-top: 2px; }

        /* History list */
        .history-item { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--card-border); }
        .history-item:last-child { border-bottom: none; }
        .history-num { width: 24px; height: 24px; border-radius: 50%; background: var(--page-bg); display: flex; align-items: center; justify-content: center; font-size: 0.7rem; font-weight: 700; color: #94a3b8; flex-shrink: 0; }
        .history-info .hi-title { font-size: 0.875rem; font-weight: 600; color: #1a202c; }
        .history-info .hi-artist { font-size: 0.78rem; color: #64748b; }
        .history-time { margin-left: auto; font-size: 0.75rem; color: #94a3b8; white-space: nowrap; }
        .history-filter { border-radius: 999px; font-size: .75rem; font-weight: 700; padding: 4px 9px; }
        .history-type { font-size: .68rem; font-weight: 700; border-radius: 999px; padding: 2px 7px; margin-top: 4px; display: inline-flex; width: fit-content; }
        .history-type.music { background: #e0f2fe; color: #0369a1; }
        .history-type.normal { background: #dcfce7; color: #15803d; }
        .history-type.hour { background: #fef3c7; color: #b45309; }

        /* Actions */
        .action-btn { border-radius: 8px; font-weight: 600; font-size: 0.875rem; }
        .broadcast-controls .action-btn {
            position: relative;
            transition: transform .16s ease, box-shadow .16s ease, filter .16s ease;
        }
        .broadcast-controls .action-btn:hover {
            transform: translateY(-2px);
            filter: brightness(1.04);
            box-shadow: 0 10px 22px rgba(15, 23, 42, .18);
        }
        .broadcast-controls .btn-success:hover {
            box-shadow: 0 10px 24px rgba(22, 163, 74, .28);
        }
        .broadcast-controls .btn-danger:hover {
            box-shadow: 0 10px 24px rgba(220, 38, 38, .30);
        }
        .broadcast-controls .btn-outline-primary:hover {
            box-shadow: 0 10px 24px rgba(13, 110, 253, .24);
        }
        .broadcast-controls .action-btn:active {
            transform: translateY(0);
        }

        /* Disk bar */
        .disk-bar { height: 8px; border-radius: 4px; background: #e2e8f0; overflow: hidden; margin-bottom: 6px; }
        .disk-bar-fill { height: 100%; border-radius: 4px; background: linear-gradient(90deg, #0d6efd, #60a5fa); }

        @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; }
            .card-panel-body { padding: 14px; }
        }
    
        /* 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 class="active" 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 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>
            <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">Ana Panel</div>
                <span class="topbar-status {{ 'on' if durum == 'YAYINDA' else 'off' }}">{{ durum }}</span>
            </div>
            <div class="p-4">
                {% if warnings %}
                <div class="row g-3 mb-4">
                    {% for warning in warnings %}
                    <div class="col-md-6 col-xl-3">
                        <div class="alert alert-{{ warning.level }} mb-0 py-2 h-100">
                            <div class="fw-bold small">{{ warning.title }}</div>
                            <div class="small">{{ warning.text }}</div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% endif %}



                <!-- Yayın Kontrol -->
                <div class="card-panel mb-4">
                    <div class="card-panel-header">
                        <div>
                            <div class="title">Yayın Kontrolü</div>
                            <div class="subtitle">Sunucuyu başlat, durdur veya şarkıyı atla</div>
                        </div>
                        <button class="btn btn-sm btn-outline-secondary action-btn" data-bs-toggle="collapse" data-bs-target="#serverSettings">Sunucu düzenle</button>
                    </div>
                    <div class="card-panel-body">
                        <div class="broadcast-controls d-flex flex-wrap gap-2">
                            <form method="post" action="/baslat">
                                <button class="btn btn-success action-btn">▶ Başlat</button>
                            </form>
                            <form method="post" action="/durdur" onsubmit="return prompt('Yayını durdurmak için DURDUR yazın:') === 'DURDUR';">
                                <input type="hidden" name="stop_confirm" value="DURDUR">
                                <button class="btn btn-danger action-btn">■ Durdur</button>
                            </form>
                            <form method="post" action="/sonraki-sarki">
                                <button class="btn btn-outline-primary action-btn">⏭ Sonraki şarkı</button>
                            </form>
                        </div>
                        <div class="collapse mt-3" id="serverSettings">
                            <form method="post" action="/sunucu-ayarlari" class="row g-3 mt-1 pt-3 border-top">
                                <div class="col-md-6">
                                    <label class="form-label small fw-semibold">Başlık</label>
                                    <input class="form-control form-control-sm" name="station_name" value="{{ ayarlar.station_name }}">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label small fw-semibold">Müzik tarzı</label>
                                    <input class="form-control form-control-sm" name="station_genre" value="{{ ayarlar.station_genre }}">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label small fw-semibold">URL adresi</label>
                                    <input class="form-control form-control-sm" name="station_url" value="{{ ayarlar.station_url }}">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label small fw-semibold">Tanım</label>
                                    <textarea class="form-control form-control-sm" name="station_description" rows="1">{{ ayarlar.station_description }}</textarea>
                                </div>
                                <div class="col-12">
                                    <button class="btn btn-primary btn-sm action-btn">Kaydet ve yenile</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- Stream linki -->
                <div class="card-panel mb-4">
                    <div class="card-panel-body py-3">
                        <div class="d-flex flex-column flex-md-row align-items-md-center gap-2">
                            <span class="text-muted small">Yayın linki:</span>
                            <a href="{{ stream_url }}" target="_blank" class="fw-semibold text-decoration-none">{{ stream_url }}</a>
                            <div class="ms-md-auto d-flex gap-2 flex-wrap">
                                <a class="btn btn-sm btn-outline-primary action-btn" href="/yayini-indir.m3u">↓ Player dosyası</a>
                                <a class="btn btn-sm btn-outline-secondary action-btn" href="{{ stream_url }}" target="_blank">Tarayıcıda aç ↗</a>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Sağlık + Disk + Şu an çalıyor -->
                <div class="row g-4 mb-4">
                    <div class="col-12">
                        <div class="card-panel">
                            <div class="card-panel-header">
                                <div>
                                    <div class="title">Yayın Sağlığı</div>
                                    <div class="subtitle">Motor, dinleyici ve zamanlayıcı durumu</div>
                                </div>
                                <a class="btn btn-sm btn-outline-secondary action-btn" href="/raporlar">Rapor →</a>
                            </div>
                            <div class="card-panel-body">
                                <div class="row g-3">
                                    <div class="col-6 col-sm-4 col-lg-2">
                                        <div class="stat-box">
                                            <div class="stat-label">Sunucu</div>
                                            <div class="stat-value {{ 'good' if health.container == 'Çalışıyor' else 'bad' }}">{{ health.container }}</div>
                                        </div>
                                    </div>
                                    <div class="col-6 col-sm-4 col-lg-2">
                                        <div class="stat-box">
                                            <div class="stat-label">Dinleyici</div>
                                            <div class="stat-value">{{ health.listeners }}</div>
                                        </div>
                                    </div>
                                    <div class="col-6 col-sm-4 col-lg-2">
                                        <div class="stat-box">
                                            <div class="stat-label">Kalite</div>
                                            <div class="stat-value">{{ health.bitrate }}</div>
                                        </div>
                                    </div>
                                    <div class="col-6 col-sm-4 col-lg-2">
                                        <div class="stat-box">
                                            <div class="stat-label">Kalan</div>
                                            <div id="stat-remaining" class="stat-value">{{ health.remaining }}</div>
                                        </div>
                                    </div>
                                    <div class="col-6 col-sm-4 col-lg-2">
                                        <div class="stat-box">
                                            <div class="stat-label">Zamanlayıcı</div>
                                            <div class="stat-value {{ 'good' if health.scheduler == 'Aktif' else 'bad' }}">{{ health.scheduler }}</div>
                                        </div>
                                    </div>
                                    <div class="col-6 col-sm-4 col-lg-2">
                                        <div class="stat-box">
                                            <div class="stat-label">Son jingle</div>
                                            <div class="stat-value" style="font-size:.8rem; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">{{ health.last_jingle or '-' }}</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row g-4">
                    <!-- Şu an çalıyor -->
                    <div class="col-lg-5">
                        <div class="now-playing-card">
                            <div class="np-label">Şu an çalıyor</div>
                            <div id="now-title" class="np-title">{{ now.title or now.filename or 'Bilgi bekleniyor' }}</div>
                            <div id="now-artist" class="np-artist">{{ now.artist or '' }}</div>
                            <div class="progress-bar-wrap">
                                <div class="progress-bar-fill" id="now-progress-bar" style="width: {{ now.progress or 0 }}%"></div>
                            </div>
                            <div class="np-time">
                                <span id="now-elapsed">{{ now.elapsed or '0:00' }}</span>
                                <span id="now-duration">{{ now.duration or '' }}</span>
                            </div>
                            <div id="now-remaining" class="mt-2" style="font-size:.78rem; color:#64748b;">
                                {% if now.remaining %}Bitmesine kalan: {{ now.remaining }}{% endif %}
                            </div>
                            <div class="next-track-box">
                                <div class="next-label">Sıradaki şarkı</div>
                                <div id="next-title" class="next-title">{{ next_track.title or 'Playlist bekleniyor' }}</div>
                                <div id="next-artist" class="next-artist">{{ next_track.artist or '' }}</div>
                                <div id="next-path" class="next-path">{{ next_track.relative or '' }}</div>
                            </div>
                            <div class="planned-box">
                                <div class="planned-label">Sıradaki planlı anons</div>
                                <div id="planned-title" class="planned-title">{{ planned_next.name or 'Planlı anons yok' }}</div>
                                <div id="planned-meta" class="planned-meta">{% if planned_next.time %}{{ planned_next.time }} · {{ planned_next.rule }}{% else %}Yakın zamanda tetik beklenmiyor{% endif %}</div>
                            </div>
                        </div>
                    </div>
                    <!-- Son çalınanlar -->
                    <div class="col-lg-7">
                        <div class="card-panel h-100">
                            <div class="card-panel-header">
                                <div class="title">Son Çalınanlar</div>
                                <div class="d-flex flex-wrap gap-1" id="history-filters">
                                    <button type="button" class="btn btn-primary btn-sm history-filter" data-history-filter="all">Tümü</button>
                                    <button type="button" class="btn btn-outline-secondary btn-sm history-filter" data-history-filter="music">Müzik</button>
                                    <button type="button" class="btn btn-outline-secondary btn-sm history-filter" data-history-filter="normal">Anons</button>
                                    <button type="button" class="btn btn-outline-secondary btn-sm history-filter" data-history-filter="hour">Saat Anonsu</button>
                                </div>
                            </div>
                            <div class="card-panel-body">
                                <div id="history-list">
                                    {% for item in history[:25] %}
                                    <div class="history-item">
                                        <div class="history-num">{{ loop.index }}</div>
                                        <div class="history-info">
                                            <div class="hi-title">{{ item.title or item.filename }}</div>
                                            <div class="hi-artist">{{ item.artist or '' }}</div>
                                        </div>
                                        <div class="history-time">{{ item.time or '' }}</div>
                                    </div>
                                    {% else %}
                                    <div class="text-muted small">Henüz kayıt yok.</div>
                                    {% endfor %}
                                </div>
                                <div id="history-pagination" class="d-flex flex-wrap gap-1 mt-3"></div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Disk -->
                <div class="card-panel mt-4">
                    <div class="card-panel-body py-3">
                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <span class="small fw-semibold text-muted">Disk Kullanımı</span>
                            <span class="small text-muted">{{ disk.used_gb }} / {{ disk.total_gb }} GB</span>
                        </div>
                        <div class="disk-bar"><div class="disk-bar-fill" style="width: {{ disk.percent }}%"></div></div>
                        <div class="small text-muted">{{ disk.free_gb }} GB boş</div>
                    </div>
                </div>

                <div class="card-panel mt-4">
                    <div class="card-panel-header">
                        <div>
                            <div class="title">Yakındaki Yayın Akışı</div>
                            <div class="subtitle">Bugün için sıradaki anons, jingle ve takvim kuralları</div>
                        </div>
                        <a class="btn btn-sm btn-outline-primary action-btn" href="/akis">Tüm akış</a>
                    </div>
                    <div class="card-panel-body py-2">
                        <div class="table-responsive">
                            <table class="table table-sm align-middle mb-0">
                                <tbody>
                                {% for item in flow %}
                                <tr>
                                    <td class="fw-bold" style="width:90px;">{{ item.time }}</td>
                                    <td>{{ item.title }}{% if item.conflict %}<span class="badge text-bg-warning ms-2">Çakışma</span>{% endif %}<br><span class="text-muted small">{{ item.type }} · {{ item.detail }}</span></td>
                                    <td class="text-end"><span class="badge {{ 'text-bg-success' if item.status == 'Aktif' else 'text-bg-secondary' }}">{{ item.status }}</span></td>
                                </tr>
                                {% else %}
                                <tr><td class="text-muted small">Bugün için planlı akış bulunamadı.</td></tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

            </div>
        </main>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
const historyPageSize = 25;
let historyPage = 1;
let historyItems = {{ history|tojson }};
let historyFilter = 'all';

function escapeHtml(value) {
    return String(value || '').replace(/[&<>"']/g, (char) => ({
        '&': '&amp;',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        "'": '&#39;'
    }[char]));
}

function renderHistory() {
    const history = document.getElementById('history-list');
    const pagination = document.getElementById('history-pagination');
    const filteredItems = (historyItems || []).filter((item) => historyFilter === 'all' || historyItemType(item).type === historyFilter);
    const totalPages = Math.max(1, Math.ceil(filteredItems.length / historyPageSize));
    historyPage = Math.min(Math.max(1, historyPage), totalPages);
    const start = (historyPage - 1) * historyPageSize;
    const pageItems = filteredItems.slice(start, start + historyPageSize);
    if (!pageItems.length) {
        history.innerHTML = '<div class="text-muted small">Henüz kayıt yok.</div>';
        pagination.innerHTML = '';
        return;
    }
    history.innerHTML = pageItems.map((item, i) => {
        const itemType = historyItemType(item);
        return `<div class="history-item">
            <div class="history-num">${start + i + 1}</div>
            <div class="history-info">
                <div class="hi-title">${escapeHtml(item.title || item.filename || '')}</div>
                <div class="hi-artist">${escapeHtml(item.artist || '')}</div>
                <span class="history-type ${itemType.type}">${itemType.label}</span>
            </div>
            <div class="history-time">${escapeHtml(item.time || '')}</div>
        </div>`;
    }).join('');
    pagination.innerHTML = Array.from({length: totalPages}, (_, i) => {
        const page = i + 1;
        const active = page === historyPage ? 'btn-primary' : 'btn-outline-secondary';
        return `<button type="button" class="btn btn-sm ${active}" data-history-page="${page}">${page}</button>`;
    }).join('');
    pagination.querySelectorAll('[data-history-page]').forEach((button) => {
        button.addEventListener('click', () => {
            historyPage = Number(button.dataset.historyPage || 1);
            renderHistory();
        });
    });
}

function historyItemType(item) {
    const filename = String(item.filename || '').toLowerCase();
    const source = String(item.source || '').toLowerCase();
    const title = String(item.title || '').toLowerCase();
    if (filename.includes('/jingle/saat/') || title.includes('saat ')) return {type: 'hour', label: 'Saat anonsu'};
    if (filename.includes('/jingle/') || source.includes('scheduled') || source.includes('jingle')) return {type: 'normal', label: 'Anons'};
    return {type: 'music', label: 'Müzik'};
}

document.querySelectorAll('[data-history-filter]').forEach((button) => {
    button.addEventListener('click', () => {
        historyFilter = button.dataset.historyFilter || 'all';
        historyPage = 1;
        document.querySelectorAll('[data-history-filter]').forEach((item) => {
            const active = item === button;
            item.classList.toggle('btn-primary', active);
            item.classList.toggle('btn-outline-secondary', !active);
        });
        renderHistory();
    });
});

async function yenile() {
    try {
        const res = await fetch('/api/now-playing');
        const data = await res.json();
        const now = data.now || {};
        document.getElementById('now-title').textContent = now.title || now.filename || 'Bilgi bekleniyor';
        document.getElementById('now-artist').textContent = now.artist || '';
        document.getElementById('now-progress-bar').style.width = (now.progress || 0) + '%';
        document.getElementById('now-elapsed').textContent = now.elapsed || '0:00';
        document.getElementById('now-duration').textContent = now.duration || '';
        document.getElementById('now-remaining').textContent = now.remaining ? 'Bitmesine kalan: ' + now.remaining : '';
        document.getElementById('stat-remaining').textContent = now.remaining || '-';
        const next = data.next || {};
        document.getElementById('next-title').textContent = next.title || 'Playlist bekleniyor';
        document.getElementById('next-artist').textContent = next.artist || '';
        document.getElementById('next-path').textContent = next.relative || '';
        const planned = data.planned_next || {};
        document.getElementById('planned-title').textContent = planned.name || 'Planlı anons yok';
        document.getElementById('planned-meta').textContent = planned.time ? `${planned.time} · ${planned.rule || ''}` : 'Yakın zamanda tetik beklenmiyor';
        if (data.history && data.history.length) {
            historyItems = data.history || [];
            renderHistory();
        }
    } catch (e) {}
}
renderHistory();
setInterval(yenile, 1000);
</script>
<script src="/static/js/mobile-nav.js"></script>
</body>
</html>










