<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Akkurtcast - Jingle Grupları</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-text: #a8b2c0;
            --accent: #0d6efd; --page-bg: #f0f2f5; --card-bg: #fff; --card-border: #e2e8f0;
        }
        * { 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; 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; 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; }
        .card-panel { background: var(--card-bg); border: 1px solid var(--card-border); border-radius: 8px; 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; }
        .mode-box { background: var(--page-bg); border: 1px solid var(--card-border); border-radius: 8px; padding: 14px; }
        .action-btn { border-radius: 7px; font-weight: 600; font-size: 0.875rem; }
        .jingle-row td { vertical-align: middle; font-size: 0.875rem; }
        .jingle-preview { width: min(260px, 100%); height: 32px; display: block; }
        .badge-mode { font-size: 0.72rem; padding: 3px 8px; border-radius: 20px; font-weight: 600; }
        .badge-mode.songs { background: #dcfce7; color: #15803d; }
        .badge-mode.minutes { background: #dbeafe; color: #1d4ed8; }
        .badge-mode.hour { background: #fef9c3; color: #92400e; }
        @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; }
            .jingle-groups-table thead { display: none; }
            .jingle-groups-table,
            .jingle-groups-table tbody,
            .jingle-groups-table td { display: block; width: 100%; }
            .jingle-groups-table {
                border-collapse: separate;
                border-spacing: 0;
            }
            .jingle-row {
                display: grid;
                grid-template-columns: minmax(0, 1.15fr) minmax(108px, .85fr);
                gap: 10px 12px;
                margin: 0 0 12px;
                padding: 12px;
                border: 1px solid var(--card-border);
                border-radius: 10px;
                overflow: hidden;
                background: #fff;
            }
            .jingle-row td {
                border: 0 !important;
                padding: 0 !important;
                white-space: normal;
            }
            .jingle-row td[data-label="Grup"],
            .jingle-row td[data-label="İşlemler"] {
                grid-column: 1 / -1;
            }
            .jingle-row td[data-label="Durum"] {
                align-self: end;
            }
            .jingle-row td[data-label]::before {
                content: attr(data-label);
                display: block;
                color: #64748b;
                font-size: .64rem;
                font-weight: 700;
                text-transform: uppercase;
                letter-spacing: .4px;
                margin-bottom: 3px;
            }
            .jingle-row td[data-label="İşlemler"]::before { display: none; }
            .jingle-row .badge-mode {
                display: inline-flex;
                white-space: normal;
                line-height: 1.25;
                max-width: 100%;
            }
            .jingle-preview {
                width: 200px;
                max-width: 100%;
                height: 34px;
                margin-top: 6px !important;
            }
            .jingle-actions {
                display: flex;
                justify-content: flex-end;
                align-items: center;
                flex-wrap: nowrap;
                gap: 6px;
                text-align: stretch !important;
                margin-top: 4px;
            }
            .jingle-actions form {
                flex: 0 0 auto;
                margin: 0 !important;
                display: flex;
            }
            .jingle-actions .action-btn {
                width: auto;
                min-width: 82px;
                min-height: 34px;
                padding: 5px 8px;
                font-size: .8rem;
                line-height: 1.15;
            }
        }
    </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 class="active" href="/jingle"><span class="nav-icon nav-icon-jingle"><i class="bi bi-bell-fill"></i></span> Jingle listesi</a>
            <a href="/kutuphane?tip=jingle"><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>
            <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">Jingle Grupları</div>
                <span class="topbar-status {{ 'on' if durum == 'YAYINDA' else 'off' }}">{{ durum }}</span>
            </div>
            <div class="p-4">
                <div class="card-panel mb-4">
                    <div class="card-panel-header">
                        <div>
                            <div class="title">Jingle Grubu Ekle</div>
                            <div class="subtitle">Bu ekrandaki kayıtlar çalma listesi motoruyla çalışır ve normal çapraz geçişe girer.</div>
                        </div>
                        <button class="btn btn-primary btn-sm action-btn" data-bs-toggle="collapse" data-bs-target="#jingleGroupForm">+ Yeni grup</button>
                    </div>
                    <div id="jingleGroupForm" class="collapse">
                        <div class="card-panel-body">
                            <form method="post" action="/jingle-ekle" class="row g-3">
                                <input type="hidden" name="playlist_group" value="1">
                                <div class="col-md-6">
                                    <label class="form-label fw-semibold small">Grup adı</label>
                                    <input class="form-control form-control-sm" name="name" placeholder="Örn: jingle sabah" required>
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label fw-semibold small">Sıralama</label>
                                    <select class="form-select form-select-sm" name="playback_order">
                                        <option value="shuffle">Karışık</option>
                                        <option value="sequential">Sıralı</option>
                                        <option value="random">Rastgele</option>
                                    </select>
                                </div>
                                <div class="col-12">
                                    <label class="form-label fw-semibold small">Mevcut müzik/jingle havuzu</label>
                                    <select class="form-select" name="selected_media" multiple size="12" required>
                                        {% for media in available_media %}
                                        <option value="{{ media.path }}">{{ media.name }}</option>
                                        {% endfor %}
                                    </select>
                                    <div class="form-text">Ctrl tuşuyla birden fazla dosya seçebilirsin. Dosya kopyalanmaz; bu gruba bağlanır.</div>
                                </div>
                                <div class="col-12">
                                    <div class="mode-box">
                                        <label class="form-label fw-bold small mb-2">Ne zaman çalsın?</label>
                                        <div class="row g-3">
                                            <div class="col-lg-4">
                                                <label class="d-block">
                                                    <input class="form-check-input me-2" type="radio" name="playback_type" value="once_per_x_songs" checked data-playback-type>
                                                    Her X şarkıdan sonra
                                                </label>
                                            </div>
                                            <div class="col-lg-4">
                                                <label class="d-block">
                                                    <input class="form-check-input me-2" type="radio" name="playback_type" value="once_per_x_minutes" data-playback-type>
                                                    Her X dakikada
                                                </label>
                                            </div>
                                            <div class="col-lg-4">
                                                <label class="d-block">
                                                    <input class="form-check-input me-2" type="radio" name="playback_type" value="once_per_hour" data-playback-type>
                                                    Saatin X. dakikasında
                                                </label>
                                            </div>
                                        </div>
                                        <div class="row g-3 mt-1">
                                            <div class="col-md-4">
                                                <label class="form-label small" id="intervalLabel">Kaç şarkıda bir çalınsın?</label>
                                                <input class="form-control form-control-sm" type="number" name="interval_value" min="1" max="240" value="1">
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small">Başlangıç saati</label>
                                                <input class="form-control form-control-sm" type="time" name="start_time">
                                            </div>
                                            <div class="col-md-4">
                                                <label class="form-label small">Bitiş saati</label>
                                                <input class="form-control form-control-sm" type="time" name="end_time">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="mode-box">
                                        <div class="d-flex flex-wrap gap-3 align-items-center">
                                            <div class="form-check mb-0">
                                                <input class="form-check-input" type="checkbox" name="active" id="ruleActive" checked>
                                                <label class="form-check-label fw-bold" for="ruleActive">Aktif</label>
                                            </div>
                                            <div class="form-check mb-0">
                                                <input class="form-check-input" type="checkbox" name="interrupt_other" id="interruptOther">
                                                <label class="form-check-label" for="interruptOther">Beklemeden hemen girsin</label>
                                            </div>
                                        </div>
                                        <div class="mt-3">
                                            <label class="form-label small fw-semibold">Günler</label>
                                            <div class="d-flex flex-wrap gap-2">
                                                {% for value, label in gunler %}
                                                <div class="form-check form-check-inline me-0">
                                                    <input class="form-check-input" type="checkbox" name="days" id="day{{ value }}" value="{{ value }}">
                                                    <label class="form-check-label small" for="day{{ value }}">{{ label }}</label>
                                                </div>
                                                {% endfor %}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-12 text-end">
                                    <button class="btn btn-success action-btn">Kaydet ve yayına uygula</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="card-panel mb-4">
                    <div class="card-panel-header">
                        <div>
                            <div class="title">Jingle Grupları</div>
                            <div class="subtitle">{{ jingle_groups|length }} grup tanımlı</div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table align-middle mb-0 jingle-groups-table" style="font-size:.875rem;">
                            <thead style="background:#f8fafc;">
                                <tr>
                                    <th class="ps-4 py-3">Grup</th>
                                    <th class="py-3">İçerik</th>
                                    <th class="py-3">Kural</th>
                                    <th class="py-3">Gün / Saat</th>
                                    <th class="py-3">Durum</th>
                                    <th class="py-3 text-end pe-4">İşlemler</th>
                                </tr>
                            </thead>
                            <tbody>
                            {% for j in jingle_groups %}
                                <tr class="jingle-row {{ '' if j.active else 'table-secondary opacity-75' }}">
                                    <td class="ps-4 py-3" data-label="Grup">
                                        <div class="fw-semibold">{{ j.name }}</div>
                                        {% if j.description %}<div class="small text-muted">{{ j.description }}</div>{% endif %}
                                    </td>
                                    <td class="py-3" data-label="İçerik">
                                        <div>{{ j.count }} dosya</div>
                                        <div class="small text-muted">{% if j.playback_order == 'sequential' %}Sıralı{% elif j.playback_order == 'random' %}Rastgele{% else %}Karışık{% endif %}</div>
                                        {% if j.preview_url %}
                                        <audio class="mt-2 jingle-preview" controls preload="none">
                                            <source src="{{ j.preview_url }}">
                                        </audio>
                                        {% endif %}
                                    </td>
                                    <td class="py-3" data-label="Kural">
                                        {% if j.playback_type == 'once_per_x_minutes' %}
                                            <span class="badge-mode minutes">Her {{ j.interval_value }} dakika</span>
                                        {% elif j.playback_type == 'once_per_hour' %}
                                            <span class="badge-mode hour">Saatin {{ j.interval_value }}. dakikası</span>
                                        {% else %}
                                            <span class="badge-mode songs">Her {{ j.interval_value }} şarkı</span>
                                        {% endif %}
                                    </td>
                                    <td class="py-3" data-label="Gün / Saat">
                                        <div class="small">
                                            {% if j.schedule.days %}
                                                {% for day in j.schedule.days %}{{ gun_adlari[day|string] }}{% if not loop.last %}, {% endif %}{% endfor %}
                                            {% else %}Her gün{% endif %}
                                        </div>
                                        {% if j.schedule.start_time or j.schedule.end_time %}
                                        <div class="small text-muted">{{ j.schedule.start_time or '00:00' }} - {{ j.schedule.end_time or '23:59' }}</div>
                                        {% endif %}
                                    </td>
                                    <td class="py-3" data-label="Durum">
                                        {% if j.active %}<span class="badge text-bg-success">Aktif</span>{% else %}<span class="badge text-bg-secondary">Pasif</span>{% endif %}
                                    </td>
                                    <td class="py-3 text-end pe-4 jingle-actions" data-label="İşlemler">
                                        <button class="btn btn-sm btn-outline-secondary action-btn" data-bs-toggle="modal" data-bs-target="#editJingle{{ loop.index }}">Düzenle</button>
                                        <form method="post" action="/jingle-toggle/{{ j.key }}" class="d-inline">
                                            <button class="btn btn-sm btn-outline-primary action-btn">{{ 'Pasifleştir' if j.active else 'Aktifleştir' }}</button>
                                        </form>
                                        <form method="post" action="/jingle-sil/{{ j.key }}" class="d-inline ms-1" onsubmit="return confirm('{{ j.name }} jingle grubunu silmek istiyor musun?');">
                                            <button class="btn btn-sm btn-outline-danger action-btn">Sil</button>
                                        </form>
                                    </td>
                                </tr>
                                <div class="modal fade" id="editJingle{{ loop.index }}" tabindex="-1" aria-hidden="true">
                                    <div class="modal-dialog modal-dialog-centered modal-lg">
                                        <div class="modal-content">
                                            <form method="post" action="/jingle-duzenle/{{ j.key }}">
                                                <div class="modal-header">
                                                    <h5 class="modal-title">{{ j.name }} düzenle</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Kapat"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="row g-3">
                                                        <div class="col-md-6">
                                                            <label class="form-label small fw-semibold">Grup adı</label>
                                                            <input class="form-control form-control-sm" name="name" value="{{ j.name }}" required>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <label class="form-label small fw-semibold">Sıralama</label>
                                                            <select class="form-select form-select-sm" name="playback_order">
                                                                <option value="shuffle" {% if j.playback_order == 'shuffle' %}selected{% endif %}>Karışık</option>
                                                                <option value="sequential" {% if j.playback_order == 'sequential' %}selected{% endif %}>Sıralı</option>
                                                                <option value="random" {% if j.playback_order == 'random' %}selected{% endif %}>Rastgele</option>
                                                            </select>
                                                        </div>
                                                        <div class="col-12">
                                                            <label class="form-label small fw-semibold">Mevcut müzik/jingle havuzu</label>
                                                            <select class="form-select" name="selected_media" multiple size="12" required>
                                                                {% for media in available_media %}
                                                                <option value="{{ media.path }}" {% if media.path in j.manual_tracks %}selected{% endif %}>{{ media.name }}</option>
                                                                {% endfor %}
                                                            </select>
                                                        </div>
                                                        <div class="col-md-4">
                                                            <label class="form-label small fw-semibold">Kural</label>
                                                            <select class="form-select form-select-sm" name="playback_type">
                                                                <option value="once_per_x_songs" {% if j.playback_type == 'once_per_x_songs' %}selected{% endif %}>Her X şarkıdan sonra</option>
                                                                <option value="once_per_x_minutes" {% if j.playback_type == 'once_per_x_minutes' %}selected{% endif %}>Her X dakikada</option>
                                                                <option value="once_per_hour" {% if j.playback_type == 'once_per_hour' %}selected{% endif %}>Saatin X. dakikasında</option>
                                                            </select>
                                                        </div>
                                                        <div class="col-md-2">
                                                            <label class="form-label small fw-semibold">X</label>
                                                            <input class="form-control form-control-sm" type="number" name="interval_value" min="0" max="240" value="{{ j.interval_value }}">
                                                        </div>
                                                        <div class="col-md-3">
                                                            <label class="form-label small fw-semibold">Başlangıç</label>
                                                            <input class="form-control form-control-sm" type="time" name="start_time" value="{{ j.schedule.start_time }}">
                                                        </div>
                                                        <div class="col-md-3">
                                                            <label class="form-label small fw-semibold">Bitiş</label>
                                                            <input class="form-control form-control-sm" type="time" name="end_time" value="{{ j.schedule.end_time }}">
                                                        </div>
                                                        <div class="col-12">
                                                            <div class="d-flex flex-wrap gap-3 align-items-center">
                                                                <div class="form-check mb-0">
                                                                    <input class="form-check-input" type="checkbox" name="active" id="editActive{{ loop.index }}" {% if j.active %}checked{% endif %}>
                                                                    <label class="form-check-label fw-bold" for="editActive{{ loop.index }}">Aktif</label>
                                                                </div>
                                                                <div class="form-check mb-0">
                                                                    <input class="form-check-input" type="checkbox" name="interrupt_other" id="editInterrupt{{ loop.index }}" {% if j.interrupt_other %}checked{% endif %}>
                                                                    <label class="form-check-label" for="editInterrupt{{ loop.index }}">Beklemeden hemen girsin</label>
                                                                </div>
                                                            </div>
                                                            <label class="form-label small fw-semibold mt-3">Günler</label>
                                                            <div class="d-flex flex-wrap gap-2">
                                                                {% for value, label in gunler %}
                                                                <div class="form-check form-check-inline me-0">
                                                                    <input class="form-check-input" type="checkbox" name="days" id="edit{{ loop.index }}day{{ value }}" value="{{ value }}" {% if value in j.schedule.days %}checked{% endif %}>
                                                                    <label class="form-check-label small" for="edit{{ loop.index }}day{{ value }}">{{ label }}</label>
                                                                </div>
                                                                {% endfor %}
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Kapat</button>
                                                    <button class="btn btn-success">Kaydet</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            {% else %}
                                <tr><td colspan="6" class="text-muted ps-4 py-4">Henüz jingle grubu yok.</td></tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="card-panel mb-4">
                    <div class="card-panel-header">
                        <div class="title">Hemen Çal</div>
                    </div>
                    <div class="card-panel-body">
                        <form method="post" action="/jingle-hemen" class="row g-3 align-items-end">
                            <div class="col-md-8">
                                <label class="form-label small fw-semibold">Dosya</label>
                                <select name="path" class="form-select form-select-sm">
                                    {% for media in available_media %}
                                    <option value="{{ media.path }}">{{ media.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                            <div class="col-md-2">
                                <label class="form-label small fw-semibold">Giriş</label>
                                <select name="cut_policy" class="form-select form-select-sm">
                                    <option value="after">Şarkı bitince</option>
                                    <option value="cut">Hemen kes</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <button class="btn btn-primary w-100 action-btn">Çal</button>
                            </div>
                        </form>
                    </div>
                </div>

                {% if jingles %}
                <div class="card-panel">
                    <div class="card-panel-header">
                        <div>
                            <div class="title">Eski Jingle Kuralları</div>
                            <div class="subtitle">Bunlar eski jingle motorundan kalmış kayıtlar.</div>
                        </div>
                    </div>
                    <div class="card-panel-body">
                        {% for j in jingles %}
                        <form method="post" action="/jingle-sil/{{ j.id }}" class="d-flex justify-content-between align-items-center border-bottom py-2">
                            <span class="small">{{ j.name }}</span>
                            <button class="btn btn-sm btn-outline-danger action-btn">Sil</button>
                        </form>
                        {% endfor %}
                    </div>
                </div>
                {% endif %}
            </div>
        </main>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function updateIntervalLabel(scope) {
    const checked = scope.querySelector('[name="playback_type"]:checked');
    const select = scope.querySelector('select[name="playback_type"]');
    const type = checked ? checked.value : (select ? select.value : 'once_per_x_songs');
    const label = scope.querySelector('#intervalLabel');
    const input = scope.querySelector('[name="interval_value"]');
    if (!input) return;
    if (type === 'once_per_hour') {
        if (label) label.textContent = 'Saatin hangi dakikasında çalınsın?';
        input.min = '0';
        input.max = '59';
    } else if (type === 'once_per_x_minutes') {
        if (label) label.textContent = 'Kaç dakikada bir çalınsın?';
        input.min = '1';
        input.max = '240';
    } else {
        if (label) label.textContent = 'Kaç şarkıda bir çalınsın?';
        input.min = '1';
        input.max = '240';
    }
}
document.querySelectorAll('form').forEach((form) => {
    form.querySelectorAll('[name="playback_type"]').forEach((field) => {
        field.addEventListener('change', () => updateIntervalLabel(form));
    });
    updateIntervalLabel(form);
});
</script>
<script src="/static/js/mobile-nav.js"></script>
</body>
</html>









