BeKultur/public/js/syllabus.js
2026-10-21 21:07:54 +00:00

115 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

document.addEventListener('DOMContentLoaded', () => {
const stationPills = document.getElementById('stationPills');
const viewTogglePills = document.getElementById('viewTogglePills');
const dayTabs = document.getElementById('dayTabs');
const dayViewContainer = document.getElementById('dayViewContainer');
const weekViewContainer = document.getElementById('weekViewContainer');
const btnSyncSyllabus = document.getElementById('btnSyncSyllabus');
const syllabusAlert = document.getElementById('syllabusAlert');
// 1. Station Pill Selection (AJAX)
if (stationPills) {
stationPills.addEventListener('click', async (e) => {
const btn = e.target.closest('.station-btn');
if (!btn) return;
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
showAlert('Çevrimdışı moddasınız. İstasyon seçimi için internet bağlantısı gereklidir.', 'danger');
return;
}
const station = btn.dataset.station;
stationPills.querySelectorAll('.station-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
try {
const res = await fetch('/api/syllabus/station', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ station })
});
const data = await res.json();
if (data.success) {
// Fast reload to render both day & week grids accurately
window.location.reload();
}
} catch (err) {
console.error('Failed to change station:', err);
}
});
}
// 2. View Toggle (Day View vs Full Week Grid)
if (viewTogglePills) {
viewTogglePills.addEventListener('click', (e) => {
const btn = e.target.closest('.view-btn');
if (!btn) return;
const viewMode = btn.dataset.view;
viewTogglePills.querySelectorAll('.view-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
if (viewMode === 'day') {
if (dayViewContainer) dayViewContainer.classList.remove('hidden');
if (weekViewContainer) weekViewContainer.classList.add('hidden');
} else {
if (dayViewContainer) dayViewContainer.classList.add('hidden');
if (weekViewContainer) weekViewContainer.classList.remove('hidden');
}
});
}
// 3. Day Tabs Switching
if (dayTabs) {
dayTabs.addEventListener('click', (e) => {
const btn = e.target.closest('.day-tab-btn');
if (!btn) return;
const dayNumber = btn.dataset.day;
dayTabs.querySelectorAll('.day-tab-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
// Hide all panels, show target panel
document.querySelectorAll('.day-schedule-panel').forEach(p => p.classList.add('hidden'));
const activePanel = document.getElementById(`panelDay${dayNumber}`);
if (activePanel) activePanel.classList.remove('hidden');
});
}
// 4. Sync Syllabus
if (btnSyncSyllabus) {
btnSyncSyllabus.addEventListener('click', async () => {
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
showAlert('Çevrimdışı moddasınız. Senkronizasyon için internet bağlantısı gereklidir.', 'danger');
return;
}
btnSyncSyllabus.disabled = true;
btnSyncSyllabus.textContent = 'Syncing...';
showAlert('Fetching weekly timetable across all 5 days...', 'info');
try {
const res = await fetch('/api/syllabus/sync', { method: 'POST' });
const data = await res.json();
if (data.success) {
showAlert('Syllabus updated successfully!', 'info');
window.location.reload();
} else {
showAlert(`Sync failed: ${data.error}`, 'danger');
btnSyncSyllabus.disabled = false;
btnSyncSyllabus.textContent = 'Sync Syllabus';
}
} catch (err) {
showAlert('Network error while syncing syllabus.', 'danger');
btnSyncSyllabus.disabled = false;
btnSyncSyllabus.textContent = 'Sync Syllabus';
}
});
}
function showAlert(msg, type = 'info') {
if (!syllabusAlert) return;
syllabusAlert.className = `alert alert-${type}`;
syllabusAlert.textContent = msg;
syllabusAlert.classList.remove('hidden');
}
});