// Utility functions
function getFromStorage(key) {
return JSON.parse(localStorage.getItem(key));
}
function setToStorage(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}
function logout() {
localStorage.removeItem('daMua'); // Đăng xuất và xóa trạng thái
location.reload();
}
function muaKhoaHoc() {
setToStorage('daMua', true); // Lưu trạng thái đã mua khóa học
closeModal();
alert("Bạn đã mua khóa học thành công!");
renderCourseList(); // Cập nhật lại danh sách bài học
}
function closeModal() {
document.getElementById("upgradeModal").style.display = 'none';
}
function openModal() {
document.getElementById("upgradeModal").style.display = 'flex';
}
// Render danh sách bài học
function renderCourseList() {
courseList.innerHTML = '';
let foundResults = false;
khoaHoc.baiHoc.forEach(baiHoc => {
if (searchInputEl.value && !baiHoc.tieuDe.toLowerCase().includes(searchInputEl.value.toLowerCase())) {
return; // Bỏ qua nếu không khớp với tìm kiếm
}
foundResults = true;
const courseItem = document.createElement("div");
courseItem.classList.add("video-item", "bg-white", "p-4", "rounded", "shadow-md");
// Kiểm tra bài học đã mua
if (!khoaHoc.daMua && !baiHoc.mienPhi) {
courseItem.classList.add("highlighted-course");
}
courseItem.innerHTML = `
<div class="flex justify-between items-center">
<span>${baiHoc.tieuDe}</span>
<button class="text-blue-500 underline" onclick="handleVideoClick(${baiHoc.id}, '${baiHoc.video}')">Xem</button>
</div>
`;
if (!baiHoc.mienPhi && !khoaHoc.daMua) {
const watchedBadge = document.createElement('span');
watchedBadge.classList.add('watched-badge');
watchedBadge.textContent = 'Nâng cấp';
courseItem.appendChild(watchedBadge);
}
courseList.appendChild(courseItem);
});
noResultsMessageEl.style.display = foundResults ? 'none' : 'block';
}
// Hàm xử lý sự kiện xem video
function handleVideoClick(baiHocId, videoUrl) {
if (baiHocId && !khoaHoc.daMua && !khoaHoc.baiHoc.find(baiHoc => baiHoc.id === baiHocId).mienPhi) {
openModal();
} else {
// Chạy video hoặc chuyển đến trang chi tiết bài học
alert(`Mở video bài ${baiHocId} - ${videoUrl}`);
}
}
// Cập nhật lời chào người dùng
function setWelcomeMessage() {
const username = getFromStorage('username') || 'Người học';
welcomeMessageEl.innerHTML = `Chào mừng ${username}`;
}
// Lắng nghe sự kiện tìm kiếm
searchInputEl.addEventListener("input", function () {
renderCourseList(); // Render lại danh sách bài học khi người dùng tìm kiếm
});
// Gọi hàm render khi trang tải xong
setWelcomeMessage();
renderCourseList();
</script>
0 Nhận xét