/* ── Series episode selector ──────────────────────────────────── */
/* TITLE, COVER, SEASONS injected by server as window vars before this script */
var tabs = document.getElementById('tabs');
var sdiv = document.getElementById('seasons');
Object.keys(SEASONS).sort(function(a,b){return +a-+b;}).forEach(function(s) {
var btn = document.createElement('button');
btn.className = 'btn b'; btn.textContent = 'S' + s.padStart(2,'0');
btn.style.marginRight = '6px';
btn.onclick = function() { showSeason(s); };
tabs.appendChild(btn);
var div = document.createElement('div'); div.id = 's-' + s; div.style.display = 'none';
var html = '
'
+ ' '
+ '
';
html += '';
div.innerHTML = html;
sdiv.appendChild(div);
});
var firstS = Object.keys(SEASONS).sort(function(a,b){return +a-+b;})[0];
if (firstS) showSeason(firstS);
function showSeason(s) {
document.querySelectorAll('[id^=s-]').forEach(function(d) { d.style.display = 'none'; });
var d = document.getElementById('s-' + s); if (d) d.style.display = '';
}
function selAll(s, v) {
var all = Array.from(document.querySelectorAll('.s-cb-' + s));
var checked = v === undefined ? !all.every(function(x){return x.checked;}) : v;
all.forEach(function(c) { c.checked = checked; });
}
function dlSeason(s) { selAll(s, true); dlSelected(); }
function dlSelected() {
var eps = Array.from(document.querySelectorAll('.ep-cb:checked')).map(function(c) {
return {id: c.dataset.id, ep: c.dataset.ep, s: c.dataset.s, ext: c.dataset.ext};
});
if (!eps.length) { alert('Select episodes first'); return; }
var bySeason = {};
eps.forEach(function(e) { (bySeason[e.s] = bySeason[e.s]||[]).push({id:e.id, episode_num:e.ep, ext:e.ext}); });
Promise.all(Object.keys(bySeason).map(function(s) {
return fetch(BASE + '/api/download', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({episodes: bySeason[s], series_title: TITLE, season: s, cover_url: COVER})
}).then(function(r) { return r.json(); });
})).then(function() { alert('Queued!'); location.href = BASE + '/downloads'; });
}