BeKultur/public/js/private-lessons.js

219 lines
7.6 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 selectCourse = document.getElementById('selectCourse');
const selectUnit = document.getElementById('selectUnit');
const selectTopic = document.getElementById('selectTopic');
const unitLoading = document.getElementById('unitLoading');
const topicLoading = document.getElementById('topicLoading');
const btnSubmit = document.getElementById('btnSubmitRequest');
const form = document.getElementById('privateLessonForm');
const formFeedback = document.getElementById('formFeedback');
const btnSync = document.getElementById('btnSyncPrivateLessons');
const alertBox = document.getElementById('privateLessonsAlert');
const subjectPills = document.querySelectorAll('.subject-pill-btn');
const lessonCards = document.querySelectorAll('.lesson-card');
const sinifIdInput = document.getElementById('sinifId');
function showAlert(message, type = 'info', timeout = 4000) {
if (!alertBox) return;
alertBox.className = `alert alert-${type}`;
alertBox.textContent = message;
alertBox.classList.remove('hidden');
if (timeout > 0) {
setTimeout(() => {
alertBox.classList.add('hidden');
}, timeout);
}
}
function showFeedback(message, type = 'success') {
if (!formFeedback) return;
formFeedback.className = `form-feedback feedback-${type}`;
formFeedback.textContent = message;
formFeedback.classList.remove('hidden');
}
function hideFeedback() {
if (formFeedback) formFeedback.classList.add('hidden');
}
// 1. Course changed -> Load Units
if (selectCourse) {
selectCourse.addEventListener('change', async () => {
hideFeedback();
const dersId = selectCourse.value;
const sinifId = sinifIdInput ? sinifIdInput.value : '';
selectUnit.innerHTML = '<option value="">Önce ders seçiniz...</option>';
selectUnit.disabled = true;
selectTopic.innerHTML = '<option value="">Önce ünite seçiniz...</option>';
selectTopic.disabled = true;
if (btnSubmit) btnSubmit.disabled = true;
if (!dersId) return;
if (unitLoading) unitLoading.classList.remove('hidden');
try {
const res = await fetch(`/api/private-lessons/units?dersId=${encodeURIComponent(dersId)}&sinifId=${encodeURIComponent(sinifId)}`);
const data = await res.json();
if (data.success && data.units && data.units.length > 0) {
selectUnit.innerHTML = '<option value="">Seçiniz...</option>' +
data.units.map(u => `<option value="${u.id}">${u.name}</option>`).join('');
selectUnit.disabled = false;
} else {
selectUnit.innerHTML = '<option value="">Ünite bulunamadı</option>';
}
} catch (err) {
console.error('Error fetching units:', err);
selectUnit.innerHTML = '<option value="">Hata oluştu</option>';
} finally {
if (unitLoading) unitLoading.classList.add('hidden');
}
});
}
// 2. Unit changed -> Load Topics
if (selectUnit) {
selectUnit.addEventListener('change', async () => {
hideFeedback();
const uniteId = selectUnit.value;
selectTopic.innerHTML = '<option value="">Önce ünite seçiniz...</option>';
selectTopic.disabled = true;
if (btnSubmit) btnSubmit.disabled = true;
if (!uniteId) return;
if (topicLoading) topicLoading.classList.remove('hidden');
try {
const res = await fetch(`/api/private-lessons/topics?uniteId=${encodeURIComponent(uniteId)}`);
const data = await res.json();
if (data.success && data.topics && data.topics.length > 0) {
selectTopic.innerHTML = '<option value="">Seçiniz...</option>' +
data.topics.map(t => `<option value="${t.id}">${t.name}</option>`).join('');
selectTopic.disabled = false;
} else {
selectTopic.innerHTML = '<option value="">Konu bulunamadı</option>';
}
} catch (err) {
console.error('Error fetching topics:', err);
selectTopic.innerHTML = '<option value="">Hata oluştu</option>';
} finally {
if (topicLoading) topicLoading.classList.add('hidden');
}
});
}
// 3. Topic changed -> Enable Submit
if (selectTopic) {
selectTopic.addEventListener('change', () => {
hideFeedback();
const hasValue = Boolean(selectTopic.value);
if (btnSubmit) btnSubmit.disabled = !hasValue;
});
}
// 4. Form Submit
if (form) {
form.addEventListener('submit', async (e) => {
e.preventDefault();
hideFeedback();
const ogrenciDonemId = document.getElementById('ogrenciDonemId')?.value || '';
const okulId = document.getElementById('okulId')?.value || '';
const sinifId = document.getElementById('sinifId')?.value || '';
const dersId = selectCourse.value;
const uniteId = selectUnit.value;
const konuId = selectTopic.value;
if (!dersId || !uniteId || !konuId) {
showFeedback('Lütfen ders, ünite ve konu seçimlerini tamamlayınız.', 'error');
return;
}
if (btnSubmit) {
btnSubmit.disabled = true;
btnSubmit.textContent = 'Gönderiliyor...';
}
try {
const res = await fetch('/api/private-lessons/request', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ogrenciDonemId, okulId, sinifId, dersId, uniteId, konuId })
});
const data = await res.json();
if (data.success) {
showFeedback(data.message || 'Özel ders talebiniz başarıyla iletildi!', 'success');
// Reset select elements
selectTopic.innerHTML = '<option value="">Önce ünite seçiniz...</option>';
selectTopic.disabled = true;
selectUnit.innerHTML = '<option value="">Önce ders seçiniz...</option>';
selectUnit.disabled = true;
selectCourse.value = '';
} else {
showFeedback(data.message || 'Talep iletilemedi. Lütfen tekrar deneyiniz.', 'error');
}
} catch (err) {
console.error('Request error:', err);
showFeedback('Bağlantı hatası oluştu. Lütfen tekrar deneyiniz.', 'error');
} finally {
if (btnSubmit) {
btnSubmit.textContent = 'Submit Request (Gönder)';
}
}
});
}
// 5. Subject Filter Pills
subjectPills.forEach(pill => {
pill.addEventListener('click', () => {
const targetSubject = pill.dataset.subject;
subjectPills.forEach(p => p.classList.remove('active'));
pill.classList.add('active');
lessonCards.forEach(card => {
const cardSubject = card.dataset.subject;
if (targetSubject === 'all' || cardSubject === targetSubject) {
card.style.display = '';
} else {
card.style.display = 'none';
}
});
});
});
// 6. Sync Lessons
if (btnSync) {
btnSync.addEventListener('click', async () => {
btnSync.disabled = true;
btnSync.textContent = 'Syncing...';
try {
const res = await fetch('/api/private-lessons/sync', { method: 'POST' });
const data = await res.json();
if (data.success) {
showAlert(`Successfully refreshed ${data.count} private lesson records.`, 'success');
setTimeout(() => {
window.location.reload();
}, 800);
} else {
showAlert(data.error || 'Sync failed.', 'danger');
}
} catch (err) {
showAlert('Network error while syncing private lessons.', 'danger');
} finally {
btnSync.disabled = false;
btnSync.textContent = 'Sync Lessons';
}
});
}
});