BeKultur/public/js/private-lessons.js

245 lines
8.5 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 t(key, params) {
if (typeof window.BeKulturT === 'function') {
return window.BeKulturT(key, params);
}
return key;
}
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="">${t('privateLessons.selectCourseFirst')}</option>`;
selectUnit.disabled = true;
selectTopic.innerHTML = `<option value="">${t('privateLessons.selectUnitFirst')}</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="">${t('privateLessons.selectPrompt')}</option>`;
data.units.forEach(u => {
const opt = document.createElement('option');
opt.value = u.id;
opt.textContent = u.name;
selectUnit.appendChild(opt);
});
selectUnit.disabled = false;
} else {
selectUnit.innerHTML = `<option value="">${t('privateLessons.noUnitFound')}</option>`;
}
} catch (err) {
console.error('Error fetching units:', err);
selectUnit.innerHTML = `<option value="">${t('privateLessons.errorOccurred')}</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="">${t('privateLessons.selectUnitFirst')}</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="">${t('privateLessons.selectPrompt')}</option>`;
data.topics.forEach(tItem => {
const opt = document.createElement('option');
opt.value = tItem.id;
opt.textContent = tItem.name;
selectTopic.appendChild(opt);
});
selectTopic.disabled = false;
} else {
selectTopic.innerHTML = `<option value="">${t('privateLessons.noTopicFound')}</option>`;
}
} catch (err) {
console.error('Error fetching topics:', err);
selectTopic.innerHTML = `<option value="">${t('privateLessons.errorOccurred')}</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();
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
showFeedback(t('dashboard.offlineSyncAlert'), 'error');
return;
}
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 = t('privateLessons.submittingBtn');
}
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="">${t('privateLessons.selectUnitFirst')}</option>`;
selectTopic.disabled = true;
selectUnit.innerHTML = `<option value="">${t('privateLessons.selectCourseFirst')}</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 = t('privateLessons.submitBtn');
}
}
});
}
// 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 () => {
if (window.BeKulturPWA && window.BeKulturPWA.isOffline()) {
showAlert(t('dashboard.offlineSyncAlert'), 'danger');
return;
}
btnSync.disabled = true;
btnSync.textContent = t('privateLessons.syncing');
try {
const res = await fetch('/api/private-lessons/sync', { method: 'POST' });
const data = await res.json();
if (data.success) {
showAlert(t('privateLessons.syncSuccess', { count: data.count }), 'info');
setTimeout(() => {
window.location.reload();
}, 800);
} else {
showAlert(data.error || t('dashboard.syncFailed'), 'danger');
}
} catch (err) {
showAlert(t('dashboard.networkError'), 'danger');
} finally {
btnSync.disabled = false;
btnSync.textContent = t('privateLessons.syncBtn');
}
});
}
});