2026-01-26 17:15:57 +07:00
|
|
|
|
<script setup lang="ts">
|
2026-01-13 10:46:40 +07:00
|
|
|
|
/**
|
|
|
|
|
|
* @file learning.vue
|
2026-01-23 09:47:32 +07:00
|
|
|
|
* @description หน้าเรียนออนไลน์ (Classroom Interface)
|
|
|
|
|
|
* จัดการแสดงผลวิดีโอรายการบทเรียน และติดตามความคืบหน้า
|
|
|
|
|
|
* ออกแบบให้เหมือนระบบ LMS มาตรฐาน
|
2026-01-13 10:46:40 +07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
definePageMeta({
|
2026-02-12 16:05:37 +07:00
|
|
|
|
layout: false,
|
2026-01-13 10:46:40 +07:00
|
|
|
|
middleware: 'auth'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
useHead({
|
|
|
|
|
|
title: 'ห้องเรียนออนไลน์ - e-Learning'
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const route = useRoute()
|
2026-01-29 15:07:45 +07:00
|
|
|
|
const router = useRouter()
|
2026-01-26 17:15:57 +07:00
|
|
|
|
const { t } = useI18n()
|
2026-01-29 14:02:32 +07:00
|
|
|
|
const { user } = useAuth()
|
2026-02-09 11:40:41 +07:00
|
|
|
|
const { fetchCourseLearningInfo, fetchLessonContent, saveVideoProgress, checkLessonAccess, fetchVideoProgress, fetchCourseAnnouncements, markLessonComplete, getLocalizedText } = useCourse()
|
2026-02-12 16:05:37 +07:00
|
|
|
|
const $q = useQuasar()
|
2026-01-20 15:51:58 +07:00
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// State management
|
2026-01-13 10:46:40 +07:00
|
|
|
|
const sidebarOpen = ref(false)
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const courseId = computed(() => Number(route.query.course_id))
|
|
|
|
|
|
|
2026-01-23 09:47:32 +07:00
|
|
|
|
// ==========================================
|
|
|
|
|
|
// 1. ตัวแปร State (สถานะของ UI)
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
|
// courseData: เก็บข้อมูลโครงสร้างคอร์ส (บทเรียนต่างๆ)
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const courseData = ref<any>(null)
|
2026-02-02 14:37:26 +07:00
|
|
|
|
const announcements = ref<any[]>([]) // Announcements state
|
|
|
|
|
|
const showAnnouncementsModal = ref(false) // Modal state
|
|
|
|
|
|
const hasUnreadAnnouncements = ref(false) // Unread state tracking
|
|
|
|
|
|
|
|
|
|
|
|
// Helper for persistent read status
|
|
|
|
|
|
const getAnnouncementStorageKey = () => {
|
|
|
|
|
|
if (!user.value?.id || !courseId.value) return ''
|
|
|
|
|
|
return `read_announcements:${user.value.id}:${courseId.value}`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const checkUnreadAnnouncements = () => {
|
|
|
|
|
|
if (!announcements.value || announcements.value.length === 0) {
|
|
|
|
|
|
hasUnreadAnnouncements.value = false
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') return
|
|
|
|
|
|
|
|
|
|
|
|
const key = getAnnouncementStorageKey()
|
|
|
|
|
|
if (!key) return
|
|
|
|
|
|
|
|
|
|
|
|
const lastRead = localStorage.getItem(key)
|
|
|
|
|
|
if (!lastRead) {
|
|
|
|
|
|
hasUnreadAnnouncements.value = true
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const lastReadDate = new Date(lastRead).getTime()
|
|
|
|
|
|
const hasNew = announcements.value.some(a => {
|
|
|
|
|
|
const annDate = new Date(a.created_at || Date.now()).getTime()
|
|
|
|
|
|
// Check if announcement is strictly newer than last read
|
|
|
|
|
|
return annDate > lastReadDate
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
hasUnreadAnnouncements.value = hasNew
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handler for opening announcements
|
|
|
|
|
|
const handleOpenAnnouncements = () => {
|
|
|
|
|
|
showAnnouncementsModal.value = true
|
|
|
|
|
|
hasUnreadAnnouncements.value = false // Clear unread badge on click
|
|
|
|
|
|
|
|
|
|
|
|
const key = getAnnouncementStorageKey()
|
|
|
|
|
|
if (key) {
|
|
|
|
|
|
localStorage.setItem(key, new Date().toISOString())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-23 09:47:32 +07:00
|
|
|
|
// currentLesson: บทเรียนที่กำลังเรียนอยู่ปัจจุบัน
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const currentLesson = ref<any>(null)
|
2026-01-23 09:47:32 +07:00
|
|
|
|
const isLoading = ref(true) // โหลดข้อมูลคอร์ส
|
|
|
|
|
|
const isLessonLoading = ref(false) // โหลดเนื้อหาบทเรียน
|
2026-01-20 15:51:58 +07:00
|
|
|
|
|
2026-01-23 09:47:32 +07:00
|
|
|
|
// Video Player State (สถานะวิดีโอ)
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const videoRef = ref<HTMLVideoElement | null>(null)
|
|
|
|
|
|
const isPlaying = ref(false)
|
|
|
|
|
|
const videoProgress = ref(0)
|
|
|
|
|
|
const currentTime = ref(0)
|
|
|
|
|
|
const duration = ref(0)
|
2026-02-02 15:34:40 +07:00
|
|
|
|
|
2026-01-20 15:51:58 +07:00
|
|
|
|
|
2026-01-13 10:46:40 +07:00
|
|
|
|
|
|
|
|
|
|
const toggleSidebar = () => {
|
|
|
|
|
|
sidebarOpen.value = !sidebarOpen.value
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// Logic Quiz Attempt Management
|
|
|
|
|
|
const quizStatus = computed(() => {
|
|
|
|
|
|
if (!currentLesson.value || currentLesson.value.type !== 'QUIZ' || !currentLesson.value.quiz) return null
|
|
|
|
|
|
|
|
|
|
|
|
const quiz = currentLesson.value.quiz
|
|
|
|
|
|
const latestAttempt = quiz.latest_attempt
|
|
|
|
|
|
const allowMultiple = quiz.allow_multiple_attempts
|
|
|
|
|
|
|
|
|
|
|
|
// If never attempted
|
|
|
|
|
|
if (!latestAttempt) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
canStart: true,
|
|
|
|
|
|
label: t('quiz.startBtn'),
|
|
|
|
|
|
icon: 'play_arrow',
|
|
|
|
|
|
showScore: false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If multiple attempts allowed
|
|
|
|
|
|
if (allowMultiple) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
canStart: true,
|
|
|
|
|
|
label: t('quiz.retryBtn'),
|
|
|
|
|
|
icon: 'refresh',
|
|
|
|
|
|
showScore: true,
|
|
|
|
|
|
score: latestAttempt.score,
|
|
|
|
|
|
isPassed: latestAttempt.is_passed
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// allowMultiple is false (Single attempt only)
|
|
|
|
|
|
// Lock the quiz regardless of pass/fail once attempted
|
|
|
|
|
|
return {
|
|
|
|
|
|
canStart: false,
|
|
|
|
|
|
label: latestAttempt.is_passed ? t('quiz.passedStatus') : t('quiz.failedStatus'),
|
|
|
|
|
|
icon: latestAttempt.is_passed ? 'check_circle' : 'cancel',
|
|
|
|
|
|
showScore: true,
|
|
|
|
|
|
score: latestAttempt.score,
|
|
|
|
|
|
isPassed: latestAttempt.is_passed
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const handleStartQuiz = () => {
|
|
|
|
|
|
if (!currentLesson.value || !currentLesson.value.quiz) return
|
|
|
|
|
|
|
|
|
|
|
|
const quiz = currentLesson.value.quiz
|
|
|
|
|
|
|
|
|
|
|
|
// If multiple attempts are disabled and it's the first time
|
|
|
|
|
|
if (!quiz.allow_multiple_attempts && !quiz.latest_attempt) {
|
|
|
|
|
|
$q.dialog({
|
|
|
|
|
|
title: `<div class="text-slate-900 dark:text-white font-black text-xl">${t('quiz.warningTitle')}</div>`,
|
|
|
|
|
|
message: `<div class="text-slate-600 dark:text-slate-300 text-base leading-relaxed mt-2">${t('quiz.singleAttemptWarning')}</div>`,
|
|
|
|
|
|
html: true,
|
|
|
|
|
|
persistent: true,
|
|
|
|
|
|
class: 'rounded-[24px]',
|
|
|
|
|
|
ok: {
|
|
|
|
|
|
label: t('quiz.continue'),
|
|
|
|
|
|
color: 'primary',
|
|
|
|
|
|
unelevated: true,
|
|
|
|
|
|
rounded: true,
|
|
|
|
|
|
class: 'px-8 font-black'
|
|
|
|
|
|
},
|
|
|
|
|
|
cancel: {
|
|
|
|
|
|
label: t('common.cancel'),
|
|
|
|
|
|
color: 'grey-7',
|
|
|
|
|
|
flat: true,
|
|
|
|
|
|
rounded: true,
|
|
|
|
|
|
class: 'font-bold'
|
|
|
|
|
|
}
|
|
|
|
|
|
}).onOk(() => {
|
|
|
|
|
|
router.push(`/classroom/quiz?course_id=${courseId.value}&lesson_id=${currentLesson.value.id}`)
|
|
|
|
|
|
})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
router.push(`/classroom/quiz?course_id=${courseId.value}&lesson_id=${currentLesson.value.id}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-09 15:55:36 +07:00
|
|
|
|
// Helper สำหรับรีเซ็ตข้อมูลและย้ายหน้า (Hard Reload)
|
|
|
|
|
|
const resetAndNavigate = (path: string) => {
|
|
|
|
|
|
if (import.meta.client) {
|
|
|
|
|
|
// 1. เก็บข้อมูลที่ต้องการรักษาไว้ (Whitelist)
|
|
|
|
|
|
const whitelist: Record<string, string> = {}
|
|
|
|
|
|
const keepKeys = ['remembered_email', 'theme', 'auth_token'] // เพิ่ม auth_token เพื่อไม่ให้หลุด login
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < localStorage.length; i++) {
|
|
|
|
|
|
const key = localStorage.key(i)
|
|
|
|
|
|
if (!key) continue
|
|
|
|
|
|
|
|
|
|
|
|
// เก็บเฉพาะ key ที่อยู่ใน whitelist หรือเป็นข้อมูลประกาศ/แจ้งเตือน
|
|
|
|
|
|
if (keepKeys.includes(key) || key.startsWith('read_announcements:')) {
|
|
|
|
|
|
const value = localStorage.getItem(key)
|
|
|
|
|
|
if (value !== null) whitelist[key] = value
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// 2. Clear all localStorage
|
2026-02-09 15:55:36 +07:00
|
|
|
|
localStorage.clear()
|
|
|
|
|
|
|
2026-02-13 11:42:10 +07:00
|
|
|
|
// 3. Restore ONLY whitelisted keys
|
|
|
|
|
|
Object.keys(whitelist).forEach(key => {
|
|
|
|
|
|
localStorage.setItem(key, whitelist[key])
|
2026-02-09 15:55:36 +07:00
|
|
|
|
})
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// 4. Force hard reload to the new path
|
2026-02-09 15:55:36 +07:00
|
|
|
|
window.location.href = path
|
|
|
|
|
|
} else {
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// SSR Fallback
|
2026-02-09 15:55:36 +07:00
|
|
|
|
router.push(path)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 11:21:29 +07:00
|
|
|
|
// Logic loadLesson ให้ลื่นไหลแบบ SPA
|
2026-01-23 10:55:26 +07:00
|
|
|
|
const handleLessonSelect = (lessonId: number) => {
|
2026-02-09 15:55:36 +07:00
|
|
|
|
if (currentLesson.value?.id === lessonId) return
|
2026-02-10 11:21:29 +07:00
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// 1. Update URL query params
|
2026-02-10 11:21:29 +07:00
|
|
|
|
router.push({ query: { ...route.query, lesson_id: lessonId.toString() } })
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// 2. Load content without refresh
|
2026-02-10 11:21:29 +07:00
|
|
|
|
loadLesson(lessonId)
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// Close sidebar on mobile
|
2026-02-10 11:21:29 +07:00
|
|
|
|
if (sidebarOpen.value) {
|
|
|
|
|
|
sidebarOpen.value = false
|
|
|
|
|
|
}
|
2026-02-09 15:55:36 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 11:21:29 +07:00
|
|
|
|
// Logic สำหรับการกดย้อนกลับหรือออกจากหน้าเรียน (ใช้ Hard Reload ตามเดิม)
|
2026-02-09 15:55:36 +07:00
|
|
|
|
const handleExit = (path: string) => {
|
|
|
|
|
|
resetAndNavigate(path)
|
2026-01-13 10:46:40 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 15:51:58 +07:00
|
|
|
|
// Data Fetching
|
2026-01-23 09:47:32 +07:00
|
|
|
|
// ==========================================
|
|
|
|
|
|
// 2. ฟังก์ชันโหลดข้อมูล (Data Fetching)
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
// โหลดโครงสร้างคอร์สและบทเรียนทั้งหมด
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const loadCourseData = async () => {
|
|
|
|
|
|
if (!courseId.value) return
|
|
|
|
|
|
isLoading.value = true
|
2026-02-05 09:56:10 +07:00
|
|
|
|
|
2026-01-20 15:51:58 +07:00
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetchCourseLearningInfo(courseId.value)
|
|
|
|
|
|
if (res.success) {
|
|
|
|
|
|
courseData.value = res.data
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// Auto-load logic: Check URL first, fallback to first available lesson
|
2026-02-10 11:21:29 +07:00
|
|
|
|
const urlLessonId = route.query.lesson_id ? Number(route.query.lesson_id) : null
|
|
|
|
|
|
|
|
|
|
|
|
if (urlLessonId) {
|
|
|
|
|
|
loadLesson(urlLessonId)
|
|
|
|
|
|
} else if (!currentLesson.value) {
|
|
|
|
|
|
const firstChapter = res.data.chapters[0]
|
|
|
|
|
|
if (firstChapter && firstChapter.lessons.length > 0) {
|
|
|
|
|
|
const availableLesson = firstChapter.lessons.find((l: any) => !l.is_locked) || firstChapter.lessons[0]
|
|
|
|
|
|
loadLesson(availableLesson.id)
|
|
|
|
|
|
}
|
2026-01-20 15:51:58 +07:00
|
|
|
|
}
|
2026-02-02 14:37:26 +07:00
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
// Fetch Course Announcements
|
2026-02-02 14:37:26 +07:00
|
|
|
|
const annRes = await fetchCourseAnnouncements(courseId.value)
|
|
|
|
|
|
if (annRes.success) {
|
|
|
|
|
|
announcements.value = annRes.data || []
|
|
|
|
|
|
checkUnreadAnnouncements()
|
|
|
|
|
|
}
|
2026-01-20 15:51:58 +07:00
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
2026-02-12 16:05:37 +07:00
|
|
|
|
console.error('Error loading course data:', error)
|
2026-01-20 15:51:58 +07:00
|
|
|
|
} finally {
|
|
|
|
|
|
isLoading.value = false
|
2026-01-13 10:46:40 +07:00
|
|
|
|
}
|
2026-01-20 15:51:58 +07:00
|
|
|
|
}
|
2026-01-13 10:46:40 +07:00
|
|
|
|
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const loadLesson = async (lessonId: number) => {
|
|
|
|
|
|
if (currentLesson.value?.id === lessonId) return
|
|
|
|
|
|
|
2026-02-05 09:56:10 +07:00
|
|
|
|
// Clear previous video state & unload component to force reset
|
2026-01-20 15:51:58 +07:00
|
|
|
|
isPlaying.value = false
|
|
|
|
|
|
videoProgress.value = 0
|
|
|
|
|
|
currentTime.value = 0
|
2026-02-13 11:42:10 +07:00
|
|
|
|
initialSeekTime.value = 0
|
|
|
|
|
|
maxWatchedTime.value = 0
|
|
|
|
|
|
lastSavedTime.value = -1
|
|
|
|
|
|
lastSavedTimestamp.value = 0
|
|
|
|
|
|
lastLocalSaveTimestamp.value = 0
|
|
|
|
|
|
currentDuration.value = 0
|
2026-02-05 09:56:10 +07:00
|
|
|
|
currentLesson.value = null // This will unmount VideoPlayer and hide content
|
|
|
|
|
|
|
2026-01-20 15:51:58 +07:00
|
|
|
|
isLessonLoading.value = true
|
|
|
|
|
|
try {
|
|
|
|
|
|
// Optional: Check access first
|
|
|
|
|
|
const accessRes = await checkLessonAccess(courseId.value, lessonId)
|
|
|
|
|
|
if (accessRes.success && !accessRes.data.is_accessible) {
|
2026-01-29 11:09:29 +07:00
|
|
|
|
let msg = t('classroom.notAvailable')
|
|
|
|
|
|
|
|
|
|
|
|
// Handle specific lock reasons
|
|
|
|
|
|
if (accessRes.data.lock_reason) {
|
|
|
|
|
|
msg = accessRes.data.lock_reason
|
|
|
|
|
|
} else if (accessRes.data.required_quiz_pass && !accessRes.data.required_quiz_pass.is_passed) {
|
|
|
|
|
|
const quizTitle = getLocalizedText(accessRes.data.required_quiz_pass.title)
|
2026-02-12 12:01:37 +07:00
|
|
|
|
msg = t('classroom.quizRequired', { title: quizTitle })
|
2026-01-29 11:09:29 +07:00
|
|
|
|
} else if (accessRes.data.required_lessons && accessRes.data.required_lessons.length > 0) {
|
|
|
|
|
|
const reqLesson = accessRes.data.required_lessons.find((l: any) => !l.is_completed)
|
|
|
|
|
|
if (reqLesson) {
|
2026-02-12 12:01:37 +07:00
|
|
|
|
msg = t('classroom.lessonRequired', { title: getLocalizedText(reqLesson.title) })
|
2026-01-29 11:09:29 +07:00
|
|
|
|
}
|
|
|
|
|
|
} else if (accessRes.data.is_enrolled === false) {
|
2026-02-12 12:01:37 +07:00
|
|
|
|
msg = t('classroom.notEnrolled')
|
2026-01-29 11:09:29 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
alert(msg)
|
2026-01-20 15:51:58 +07:00
|
|
|
|
isLessonLoading.value = false
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-29 11:09:29 +07:00
|
|
|
|
// 1. Fetch content
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const res = await fetchLessonContent(courseId.value, lessonId)
|
|
|
|
|
|
if (res.success) {
|
|
|
|
|
|
currentLesson.value = res.data
|
|
|
|
|
|
|
2026-02-04 16:22:42 +07:00
|
|
|
|
// Initialize progress object if missing (Critical for New Users)
|
|
|
|
|
|
if (!currentLesson.value.progress) {
|
|
|
|
|
|
currentLesson.value.progress = {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-29 13:17:58 +07:00
|
|
|
|
// Update Lesson Completion UI status safely
|
|
|
|
|
|
if (currentLesson.value?.progress?.is_completed && courseData.value) {
|
|
|
|
|
|
for (const chapter of courseData.value.chapters) {
|
|
|
|
|
|
const lesson = chapter.lessons.find((l: any) => l.id === lessonId)
|
|
|
|
|
|
if (lesson) {
|
|
|
|
|
|
if (!lesson.progress) lesson.progress = {}
|
|
|
|
|
|
lesson.progress.is_completed = true
|
2026-02-06 12:57:29 +07:00
|
|
|
|
lesson.is_completed = true // Standardize completion property
|
2026-01-29 13:17:58 +07:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Fetch Initial Progress (Resume Playback)
|
|
|
|
|
|
if (currentLesson.value.type === 'VIDEO') {
|
2026-02-13 11:42:10 +07:00
|
|
|
|
// If already completed, clear local resume point to allow fresh re-watch
|
|
|
|
|
|
const isCompleted = currentLesson.value.progress?.is_completed || false
|
2026-01-29 14:02:32 +07:00
|
|
|
|
|
2026-02-13 11:42:10 +07:00
|
|
|
|
if (isCompleted) {
|
|
|
|
|
|
const key = getLocalProgressKey(lessonId)
|
|
|
|
|
|
if (key && typeof window !== 'undefined') {
|
|
|
|
|
|
localStorage.removeItem(key)
|
|
|
|
|
|
}
|
2026-01-29 13:17:58 +07:00
|
|
|
|
initialSeekTime.value = 0
|
2026-01-29 14:02:32 +07:00
|
|
|
|
maxWatchedTime.value = 0
|
2026-02-13 11:42:10 +07:00
|
|
|
|
currentTime.value = 0
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Not completed? Resume from where we left off
|
|
|
|
|
|
const progressRes = await fetchVideoProgress(lessonId)
|
|
|
|
|
|
let serverProgress = 0
|
|
|
|
|
|
if (progressRes.success && progressRes.data?.video_progress_seconds) {
|
|
|
|
|
|
serverProgress = progressRes.data.video_progress_seconds
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const localProgress = getLocalProgress(lessonId)
|
|
|
|
|
|
const resumeTime = Math.max(serverProgress, localProgress)
|
|
|
|
|
|
|
|
|
|
|
|
if (resumeTime > 0) {
|
|
|
|
|
|
initialSeekTime.value = resumeTime
|
|
|
|
|
|
maxWatchedTime.value = resumeTime
|
|
|
|
|
|
currentTime.value = resumeTime
|
|
|
|
|
|
} else {
|
|
|
|
|
|
initialSeekTime.value = 0
|
|
|
|
|
|
maxWatchedTime.value = 0
|
|
|
|
|
|
}
|
2026-01-20 15:51:58 +07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('Error loading lesson:', error)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
isLessonLoading.value = false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-02 17:13:58 +07:00
|
|
|
|
// Video Player Ref (Component)
|
|
|
|
|
|
const videoPlayerComp = ref(null)
|
|
|
|
|
|
|
2026-01-29 13:17:58 +07:00
|
|
|
|
// Video & Progress State
|
|
|
|
|
|
const initialSeekTime = ref(0)
|
2026-01-29 14:02:32 +07:00
|
|
|
|
const maxWatchedTime = ref(0) // Anti-rewind monotonic tracking
|
2026-01-29 13:17:58 +07:00
|
|
|
|
const lastSavedTime = ref(-1)
|
2026-01-29 14:02:32 +07:00
|
|
|
|
const lastSavedTimestamp = ref(0) // Server throttle timestamp
|
|
|
|
|
|
const lastLocalSaveTimestamp = ref(0) // Local throttle timestamp
|
2026-02-02 17:13:58 +07:00
|
|
|
|
const currentDuration = ref(0) // Track duration for save logic
|
2026-01-29 14:02:32 +07:00
|
|
|
|
|
|
|
|
|
|
// Helper: Get Local Storage Key
|
|
|
|
|
|
const getLocalProgressKey = (lessonId: number) => {
|
|
|
|
|
|
if (!user.value?.id) return null
|
|
|
|
|
|
return `progress:${user.value.id}:${lessonId}`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper: Get Local Progress
|
|
|
|
|
|
const getLocalProgress = (lessonId: number): number => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const key = getLocalProgressKey(lessonId)
|
|
|
|
|
|
if (!key) return 0
|
|
|
|
|
|
const stored = localStorage.getItem(key)
|
|
|
|
|
|
return stored ? parseFloat(stored) : 0
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper: Save to Local Storage
|
|
|
|
|
|
const saveLocalProgress = (lessonId: number, time: number) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const key = getLocalProgressKey(lessonId)
|
|
|
|
|
|
if (key) {
|
|
|
|
|
|
localStorage.setItem(key, time.toString())
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
// Ignore storage errors
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-29 13:17:58 +07:00
|
|
|
|
|
2026-02-02 17:13:58 +07:00
|
|
|
|
// Handler: Video Time Update (from Component)
|
|
|
|
|
|
const handleVideoTimeUpdate = (cTime: number, dur: number) => {
|
|
|
|
|
|
currentDuration.value = dur || 0
|
|
|
|
|
|
|
|
|
|
|
|
// Update Monotonic Progress
|
|
|
|
|
|
if (cTime > maxWatchedTime.value) {
|
|
|
|
|
|
maxWatchedTime.value = cTime
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Logic: Periodic Save
|
|
|
|
|
|
if (currentLesson.value?.id) {
|
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Local Save Throttle (5 seconds)
|
|
|
|
|
|
if (now - lastLocalSaveTimestamp.value > 5000) {
|
|
|
|
|
|
saveLocalProgress(currentLesson.value.id, maxWatchedTime.value)
|
|
|
|
|
|
lastLocalSaveTimestamp.value = now
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Server Save Throttle (handled inside performSaveProgress)
|
|
|
|
|
|
// Note: We don't check isPlaying here because if time is updating, it IS playing.
|
|
|
|
|
|
performSaveProgress(false, false)
|
|
|
|
|
|
}
|
2026-01-20 15:51:58 +07:00
|
|
|
|
}
|
2026-01-13 10:46:40 +07:00
|
|
|
|
|
2026-02-04 16:22:42 +07:00
|
|
|
|
const onVideoMetadataLoaded = (duration: number) => {
|
|
|
|
|
|
if (duration > 0) {
|
|
|
|
|
|
currentDuration.value = duration
|
|
|
|
|
|
}
|
2026-01-13 10:46:40 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-02 14:37:26 +07:00
|
|
|
|
const isCompleting = ref(false) // Flag to prevent race conditions during completion
|
|
|
|
|
|
|
2026-01-29 13:17:58 +07:00
|
|
|
|
// -----------------------------------------------------
|
2026-01-29 14:02:32 +07:00
|
|
|
|
// ROBUST PROGRESS SAVING SYSTEM (Hybrid: Local + Server)
|
2026-01-29 13:17:58 +07:00
|
|
|
|
// -----------------------------------------------------
|
|
|
|
|
|
|
2026-01-29 14:02:32 +07:00
|
|
|
|
// Main Server Save Function
|
2026-01-29 13:17:58 +07:00
|
|
|
|
const performSaveProgress = async (force: boolean = false, keepalive: boolean = false) => {
|
2026-02-02 14:37:26 +07:00
|
|
|
|
const lesson = currentLesson.value
|
2026-02-02 17:13:58 +07:00
|
|
|
|
if (!lesson || lesson.type !== 'VIDEO') return
|
2026-02-04 16:22:42 +07:00
|
|
|
|
|
|
|
|
|
|
// Ensure progress object exists
|
|
|
|
|
|
if (!lesson.progress) lesson.progress = {}
|
2026-02-02 14:37:26 +07:00
|
|
|
|
|
|
|
|
|
|
// 1. Completed Guard: Stop everything if already completed
|
|
|
|
|
|
if (lesson.progress.is_completed) return
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Race Condition Guard: Stop if currently completing
|
|
|
|
|
|
if (isCompleting.value) return
|
2026-01-29 13:17:58 +07:00
|
|
|
|
|
|
|
|
|
|
const now = Date.now()
|
2026-01-29 14:02:32 +07:00
|
|
|
|
const maxSec = Math.floor(maxWatchedTime.value) // Use max watched time
|
2026-02-02 17:13:58 +07:00
|
|
|
|
const durationSec = Math.floor(currentDuration.value || 0)
|
2026-01-29 13:17:58 +07:00
|
|
|
|
|
2026-02-04 16:22:42 +07:00
|
|
|
|
// 3. Monotonic Check: Allow saving 0 if it's the very first save (lastSavedTime is -1)
|
|
|
|
|
|
if (!force) {
|
|
|
|
|
|
if (lastSavedTime.value === -1) {
|
|
|
|
|
|
// First time save: allow 0 or more
|
|
|
|
|
|
if (maxSec < 0) return
|
|
|
|
|
|
} else if (maxSec <= lastSavedTime.value) {
|
|
|
|
|
|
// Subsequent saves: must be greater than last saved
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-29 13:17:58 +07:00
|
|
|
|
|
2026-02-02 14:37:26 +07:00
|
|
|
|
// 4. Throttle Check: Server Throttle (15 seconds)
|
2026-01-29 14:02:32 +07:00
|
|
|
|
if (!force && (now - lastSavedTimestamp.value < 15000)) return
|
2026-01-29 13:17:58 +07:00
|
|
|
|
|
2026-02-02 14:37:26 +07:00
|
|
|
|
// Prepare for Save
|
2026-01-29 14:02:32 +07:00
|
|
|
|
lastSavedTime.value = maxSec
|
2026-01-29 13:17:58 +07:00
|
|
|
|
lastSavedTimestamp.value = now
|
|
|
|
|
|
|
2026-02-04 16:22:42 +07:00
|
|
|
|
// Check if this save might complete the lesson (e.g. 100% or forced end)
|
|
|
|
|
|
const isFinishing = force || (durationSec > 0 && maxSec >= durationSec)
|
2026-01-29 13:17:58 +07:00
|
|
|
|
|
2026-02-02 14:37:26 +07:00
|
|
|
|
if (isFinishing) {
|
|
|
|
|
|
isCompleting.value = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await saveVideoProgress(lesson.id, maxSec, durationSec, keepalive)
|
|
|
|
|
|
|
2026-02-06 12:57:29 +07:00
|
|
|
|
// Handle Completion (Frontend-only strategy: 95% threshold)
|
|
|
|
|
|
// This ensures the checkmark appears at 95% to match backend.
|
|
|
|
|
|
const progressPercentage = durationSec > 0 ? (maxSec / durationSec) : 0
|
|
|
|
|
|
const isCompletedNow = res.success && (res.data?.is_completed || progressPercentage >= 0.95)
|
|
|
|
|
|
|
|
|
|
|
|
if (isCompletedNow) {
|
2026-02-10 11:08:10 +07:00
|
|
|
|
const wasAlreadyCompleted = lesson.progress?.is_completed
|
2026-02-02 14:37:26 +07:00
|
|
|
|
markLessonAsCompletedLocally(lesson.id)
|
|
|
|
|
|
if (lesson.progress) lesson.progress.is_completed = true
|
2026-02-10 11:08:10 +07:00
|
|
|
|
|
|
|
|
|
|
// If newly completed, reload course data to unlock next lesson in sidebar
|
|
|
|
|
|
if (!wasAlreadyCompleted) {
|
|
|
|
|
|
await loadCourseData()
|
|
|
|
|
|
}
|
2026-02-02 14:37:26 +07:00
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error('Save progress failed', err)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (isFinishing) {
|
|
|
|
|
|
isCompleting.value = false
|
|
|
|
|
|
}
|
2026-01-29 13:17:58 +07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper to update Sidebar UI
|
|
|
|
|
|
const markLessonAsCompletedLocally = (lessonId: number) => {
|
|
|
|
|
|
if (courseData.value) {
|
|
|
|
|
|
for (const chapter of courseData.value.chapters) {
|
|
|
|
|
|
const lesson = chapter.lessons.find((l: any) => l.id === lessonId)
|
|
|
|
|
|
if (lesson) {
|
2026-01-29 16:26:30 +07:00
|
|
|
|
// Compatible with API structure
|
|
|
|
|
|
lesson.is_completed = true
|
2026-02-02 14:37:26 +07:00
|
|
|
|
if (!lesson.progress) lesson.progress = {}
|
|
|
|
|
|
lesson.progress.is_completed = true
|
2026-01-29 13:17:58 +07:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 17:03:09 +07:00
|
|
|
|
const videoSrc = computed(() => {
|
|
|
|
|
|
if (!currentLesson.value) return ''
|
2026-02-04 16:22:42 +07:00
|
|
|
|
let url = ''
|
2026-01-29 11:09:29 +07:00
|
|
|
|
|
2026-02-04 16:22:42 +07:00
|
|
|
|
// Use explicit video_url from API first
|
|
|
|
|
|
if (currentLesson.value.video_url) {
|
|
|
|
|
|
url = currentLesson.value.video_url
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Fallback (deprecated logic)
|
|
|
|
|
|
const content = getLocalizedText(currentLesson.value.content)
|
|
|
|
|
|
if (content && (content.startsWith('http') || content.startsWith('/')) && !content.includes(' ')) {
|
|
|
|
|
|
url = content
|
|
|
|
|
|
}
|
2026-01-21 17:03:09 +07:00
|
|
|
|
}
|
2026-02-04 16:22:42 +07:00
|
|
|
|
|
|
|
|
|
|
if (!url) return ''
|
|
|
|
|
|
|
|
|
|
|
|
// Support Resume for YouTube
|
|
|
|
|
|
const isYoutube = url.toLowerCase().includes('youtube.com') || url.toLowerCase().includes('youtu.be')
|
|
|
|
|
|
if (isYoutube && initialSeekTime.value > 0) {
|
|
|
|
|
|
const separator = url.includes('?') ? '&' : '?'
|
|
|
|
|
|
return `${url}${separator}t=${Math.floor(initialSeekTime.value)}`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return url
|
2026-01-21 17:03:09 +07:00
|
|
|
|
})
|
|
|
|
|
|
|
2026-01-29 11:09:29 +07:00
|
|
|
|
// เมื่อวิดีโอจบ ให้บันทึกว่าเรียนจบ (Complete)
|
2026-01-20 15:51:58 +07:00
|
|
|
|
const onVideoEnded = async () => {
|
2026-02-02 14:37:26 +07:00
|
|
|
|
const lesson = currentLesson.value
|
2026-02-13 11:42:10 +07:00
|
|
|
|
if (!lesson) return
|
|
|
|
|
|
|
|
|
|
|
|
// Clear local storage on end since it's completed
|
|
|
|
|
|
const key = getLocalProgressKey(lesson.id)
|
|
|
|
|
|
if (key && typeof window !== 'undefined') {
|
|
|
|
|
|
localStorage.removeItem(key)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (lesson.progress?.is_completed || isCompleting.value) return
|
2026-01-29 17:17:40 +07:00
|
|
|
|
|
2026-02-04 16:22:42 +07:00
|
|
|
|
isCompleting.value = true
|
|
|
|
|
|
try {
|
|
|
|
|
|
await performSaveProgress(true, false)
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error('Failed to save progress on end:', err)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
isCompleting.value = false
|
2026-01-29 17:52:52 +07:00
|
|
|
|
}
|
2026-01-13 10:46:40 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 15:51:58 +07:00
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
loadCourseData()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
2026-02-05 09:56:10 +07:00
|
|
|
|
// Clear state when leaving the page to ensure fresh start on return
|
|
|
|
|
|
courseData.value = null
|
|
|
|
|
|
currentLesson.value = null
|
2026-01-20 15:51:58 +07:00
|
|
|
|
})
|
2026-01-13 10:46:40 +07:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
2026-01-27 11:31:08 +07:00
|
|
|
|
<q-layout view="hHh LpR lFf" class="bg-[var(--bg-body)] text-[var(--text-main)]">
|
2026-01-26 09:27:31 +07:00
|
|
|
|
|
|
|
|
|
|
<!-- Header -->
|
2026-01-27 11:31:08 +07:00
|
|
|
|
<q-header bordered class="bg-[var(--bg-surface)] border-b border-gray-200 dark:border-white/5 text-[var(--text-main)] h-14">
|
2026-01-26 09:27:31 +07:00
|
|
|
|
<q-toolbar>
|
2026-02-12 12:01:37 +07:00
|
|
|
|
<!-- Exit/Back Button -->
|
|
|
|
|
|
<q-btn
|
|
|
|
|
|
flat
|
|
|
|
|
|
rounded
|
|
|
|
|
|
no-caps
|
|
|
|
|
|
color="primary"
|
|
|
|
|
|
class="mr-4 bg-slate-100 dark:bg-slate-800 text-slate-900 dark:text-white font-bold hover:bg-slate-200"
|
|
|
|
|
|
@click="handleExit('/dashboard/my-courses')"
|
|
|
|
|
|
>
|
|
|
|
|
|
<q-icon name="close" size="18px" class="mr-1.5" />
|
|
|
|
|
|
<span class="hidden sm:inline">{{ $t('common.close') }}</span>
|
|
|
|
|
|
<q-tooltip>{{ $t('classroom.backToDashboard') }}</q-tooltip>
|
|
|
|
|
|
</q-btn>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Sidebar Toggle (Clearer for Course Content) -->
|
|
|
|
|
|
<q-btn
|
|
|
|
|
|
flat
|
|
|
|
|
|
rounded
|
|
|
|
|
|
no-caps
|
|
|
|
|
|
class="mr-2 text-slate-900 dark:text-white hover:bg-slate-100 dark:hover:bg-slate-800 transition-colors font-bold px-3"
|
|
|
|
|
|
@click="toggleSidebar"
|
|
|
|
|
|
>
|
|
|
|
|
|
<q-icon name="format_list_bulleted" size="18px" class="mr-1.5" />
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<span class="hidden md:inline">{{ $t('classroom.curriculum') }}</span>
|
2026-02-12 12:01:37 +07:00
|
|
|
|
</q-btn>
|
2026-01-26 09:27:31 +07:00
|
|
|
|
|
2026-02-09 10:37:42 +07:00
|
|
|
|
<q-toolbar-title class="text-base font-bold text-left truncate text-slate-900 dark:text-white">
|
2026-01-26 17:15:57 +07:00
|
|
|
|
{{ courseData ? getLocalizedText(courseData.course.title) : $t('classroom.loadingTitle') }}
|
2026-01-26 09:27:31 +07:00
|
|
|
|
</q-toolbar-title>
|
|
|
|
|
|
|
2026-02-09 10:37:42 +07:00
|
|
|
|
<div class="flex items-center gap-2 pr-2">
|
|
|
|
|
|
<!-- Announcements Button -->
|
|
|
|
|
|
<q-btn
|
|
|
|
|
|
flat
|
|
|
|
|
|
round
|
|
|
|
|
|
dense
|
|
|
|
|
|
icon="campaign"
|
|
|
|
|
|
@click="handleOpenAnnouncements"
|
|
|
|
|
|
class="text-slate-600 dark:text-slate-300 hover:text-blue-600 transition-colors"
|
|
|
|
|
|
>
|
|
|
|
|
|
<q-badge v-if="hasUnreadAnnouncements" color="red" floating rounded />
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<q-tooltip>{{ $t('classroom.announcements') }}</q-tooltip>
|
2026-02-09 10:37:42 +07:00
|
|
|
|
</q-btn>
|
2026-01-26 09:27:31 +07:00
|
|
|
|
</div>
|
|
|
|
|
|
</q-toolbar>
|
|
|
|
|
|
</q-header>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Sidebar (Curriculum) -->
|
2026-02-02 17:13:58 +07:00
|
|
|
|
<!-- Sidebar (Curriculum) -->
|
|
|
|
|
|
<CurriculumSidebar
|
2026-01-26 09:27:31 +07:00
|
|
|
|
v-model="sidebarOpen"
|
2026-02-02 17:13:58 +07:00
|
|
|
|
:courseData="courseData"
|
|
|
|
|
|
:currentLessonId="currentLesson?.id"
|
|
|
|
|
|
:isLoading="isLoading"
|
|
|
|
|
|
:hasUnreadAnnouncements="hasUnreadAnnouncements"
|
|
|
|
|
|
@select-lesson="handleLessonSelect"
|
|
|
|
|
|
@open-announcements="handleOpenAnnouncements"
|
|
|
|
|
|
/>
|
2026-01-26 09:27:31 +07:00
|
|
|
|
|
|
|
|
|
|
<!-- Main Content -->
|
|
|
|
|
|
<q-page-container class="bg-white dark:bg-slate-900">
|
|
|
|
|
|
<q-page class="flex flex-col h-full bg-slate-50 dark:bg-[#0B0F1A]">
|
|
|
|
|
|
<!-- Video Player & Content Area -->
|
|
|
|
|
|
<div class="w-full max-w-7xl mx-auto p-4 md:p-6 flex-grow">
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<!-- 1. LOADING STATE (Comprehensive Skeleton) -->
|
|
|
|
|
|
<div v-if="isLessonLoading" class="animate-fade-in">
|
|
|
|
|
|
<!-- Video Skeleton -->
|
|
|
|
|
|
<div class="aspect-video bg-slate-200 dark:bg-slate-800 rounded-3xl animate-pulse flex items-center justify-center mb-10 overflow-hidden relative shadow-xl focus:outline-none">
|
|
|
|
|
|
<img
|
|
|
|
|
|
v-if="courseData?.course?.thumbnail_url"
|
|
|
|
|
|
:src="courseData.course.thumbnail_url"
|
|
|
|
|
|
class="absolute inset-0 w-full h-full object-cover opacity-20 blur-md"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<div class="absolute inset-0 bg-gradient-to-br from-slate-200/50 to-slate-300/50 dark:from-slate-900/80 dark:to-slate-800/80"></div>
|
|
|
|
|
|
<div class="z-10 flex flex-col items-center">
|
|
|
|
|
|
<q-spinner size="3.5rem" color="primary" :thickness="2" />
|
|
|
|
|
|
<p class="mt-4 text-slate-500 font-bold text-xs uppercase tracking-[0.2em]">{{ $t('common.loading') }}</p>
|
2026-02-12 12:01:37 +07:00
|
|
|
|
</div>
|
2026-02-12 16:05:37 +07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Info Skeleton -->
|
|
|
|
|
|
<div class="bg-white dark:bg-slate-800/50 p-8 rounded-3xl border border-slate-100 dark:border-white/5 shadow-sm">
|
|
|
|
|
|
<div class="h-10 bg-slate-200 dark:bg-slate-800 rounded-xl w-3/4 mb-4 animate-pulse"></div>
|
|
|
|
|
|
<div class="h-4 bg-slate-100 dark:bg-slate-800 rounded-lg w-full mb-2 animate-pulse"></div>
|
|
|
|
|
|
<div class="h-4 bg-slate-100 dark:bg-slate-800 rounded-lg w-2/3 animate-pulse"></div>
|
2026-02-05 09:56:10 +07:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<!-- 2. READY STATE (Real Lesson Content) -->
|
|
|
|
|
|
<div v-else-if="currentLesson" class="animate-fade-in">
|
|
|
|
|
|
<!-- Video Player -->
|
|
|
|
|
|
<VideoPlayer
|
|
|
|
|
|
v-if="videoSrc"
|
|
|
|
|
|
ref="videoPlayerComp"
|
|
|
|
|
|
:src="videoSrc"
|
|
|
|
|
|
:poster="courseData?.course?.thumbnail_url"
|
|
|
|
|
|
:initialSeekTime="initialSeekTime"
|
|
|
|
|
|
@timeupdate="handleVideoTimeUpdate"
|
|
|
|
|
|
@ended="onVideoEnded"
|
|
|
|
|
|
@loadedmetadata="(d: number) => onVideoMetadataLoaded(d)"
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Lesson Info -->
|
|
|
|
|
|
<div class="bg-[var(--bg-surface)] p-6 md:p-8 rounded-3xl shadow-sm border border-[var(--border-color)]">
|
2026-01-27 14:02:07 +07:00
|
|
|
|
<!-- ใช้สีจากตัวแปรกลาง: จะแยกโหมดให้อัตโนมัติ (สว่าง=ดำ / มืด=ขาว) -->
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div class="flex items-start justify-between gap-4 mb-4">
|
2026-02-12 12:01:37 +07:00
|
|
|
|
<h1 class="text-3xl md:text-5xl font-black text-slate-900 dark:text-white leading-tight tracking-tight font-display">{{ getLocalizedText(currentLesson.title) }}</h1>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
</div>
|
2026-01-27 14:02:07 +07:00
|
|
|
|
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<p class="text-slate-600 dark:text-slate-400 text-base md:text-lg leading-relaxed mb-6" v-if="currentLesson.description">{{ getLocalizedText(currentLesson.description) }}</p>
|
2026-01-26 17:15:57 +07:00
|
|
|
|
|
2026-01-29 11:09:29 +07:00
|
|
|
|
<!-- Lesson Content Area (Text/HTML) -->
|
2026-02-10 13:14:01 +07:00
|
|
|
|
<div v-if="currentLesson.type === 'QUIZ'" class="p-8 bg-gradient-to-br from-blue-50/50 to-indigo-50/50 dark:from-slate-800/50 dark:to-slate-900/50 rounded-2xl border border-blue-100 dark:border-white/5 text-center">
|
|
|
|
|
|
<div class="bg-white dark:bg-slate-800 w-20 h-20 rounded-full flex items-center justify-center mx-auto mb-4 shadow-sm text-blue-500 dark:text-blue-400 border dark:border-white/10">
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<q-icon name="quiz" size="40px" />
|
|
|
|
|
|
</div>
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<h2 class="text-xl font-bold mb-2 text-slate-900 dark:text-white">{{ $t('quiz.startTitle') }}</h2>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<p class="text-slate-500 dark:text-slate-400 mb-6 max-w-md mx-auto">{{ getLocalizedText(currentLesson.quiz?.description || currentLesson.description) }}</p>
|
2026-01-29 13:17:58 +07:00
|
|
|
|
|
2026-02-10 13:14:01 +07:00
|
|
|
|
<div class="flex justify-center flex-wrap gap-3 text-sm mb-8">
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<span v-if="currentLesson.quiz?.questions?.length" class="px-4 py-1.5 bg-white dark:bg-slate-800 rounded-full border border-gray-100 dark:border-white/5 shadow-sm flex items-center gap-2 text-slate-700 dark:text-slate-300 font-bold">
|
|
|
|
|
|
<q-icon name="format_list_numbered" size="14px" class="text-blue-500" /> {{ currentLesson.quiz.questions.length }} {{ $t('quiz.questions') }}
|
2026-02-10 13:14:01 +07:00
|
|
|
|
</span>
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<span v-if="currentLesson.quiz?.time_limit" class="px-4 py-1.5 bg-white dark:bg-slate-800 rounded-full border border-gray-100 dark:border-white/5 shadow-sm flex items-center gap-2 text-slate-700 dark:text-slate-300 font-bold">
|
|
|
|
|
|
<q-icon name="schedule" size="14px" class="text-orange-500" /> {{ currentLesson.quiz.time_limit }} {{ $t('quiz.minutes') }}
|
2026-02-10 13:14:01 +07:00
|
|
|
|
</span>
|
2026-01-29 13:17:58 +07:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<div v-if="quizStatus?.showScore" class="mb-8 p-6 bg-white dark:!bg-slate-800/80 rounded-[32px] border border-blue-50 dark:border-white/5 shadow-xl max-w-sm mx-auto backdrop-blur-md">
|
|
|
|
|
|
<div class="text-[10px] uppercase font-black tracking-[0.2em] text-slate-400 dark:text-slate-500 mb-4">{{ $t('quiz.latestScore') }}</div>
|
|
|
|
|
|
<div class="flex items-center justify-center gap-6">
|
|
|
|
|
|
<div class="text-5xl font-black" :class="quizStatus.isPassed ? 'text-emerald-500' : 'text-rose-500'">
|
|
|
|
|
|
{{ quizStatus.score }}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="h-12 w-px bg-slate-100 dark:bg-white/10"></div>
|
|
|
|
|
|
<div class="flex flex-col items-start gap-1">
|
|
|
|
|
|
<span class="text-[10px] font-black px-2.5 py-1 rounded-lg uppercase tracking-wider" :class="quizStatus.isPassed ? 'bg-emerald-500/10 text-emerald-500' : 'bg-rose-500/10 text-rose-500'">
|
|
|
|
|
|
{{ quizStatus.isPassed ? $t('quiz.passedStatus') : $t('quiz.failedStatus') }}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span class="text-[10px] text-slate-400 dark:text-slate-500 font-bold">{{ $t('quiz.passingScore') }} {{ currentLesson.quiz.passing_score }}%</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-29 13:17:58 +07:00
|
|
|
|
<q-btn
|
2026-02-12 16:05:37 +07:00
|
|
|
|
v-if="quizStatus?.canStart"
|
2026-02-02 14:37:26 +07:00
|
|
|
|
class="bg-blue-600 text-white shadow-lg shadow-blue-600/30 hover:shadow-blue-600/50 transition-all font-bold px-8"
|
2026-01-29 13:17:58 +07:00
|
|
|
|
size="lg"
|
|
|
|
|
|
rounded
|
2026-02-02 14:37:26 +07:00
|
|
|
|
no-caps
|
2026-02-12 16:05:37 +07:00
|
|
|
|
:label="quizStatus.label"
|
|
|
|
|
|
:icon="quizStatus.icon"
|
|
|
|
|
|
@click="handleStartQuiz"
|
2026-01-29 13:17:58 +07:00
|
|
|
|
/>
|
2026-02-12 16:05:37 +07:00
|
|
|
|
<div v-else-if="quizStatus"
|
|
|
|
|
|
class="inline-flex items-center gap-2 px-6 py-3 rounded-full font-bold"
|
|
|
|
|
|
:class="quizStatus.isPassed ? 'bg-emerald-50 dark:bg-emerald-500/10 text-emerald-600 dark:text-emerald-400' : 'bg-rose-50 dark:bg-rose-500/10 text-rose-600 dark:text-rose-400'"
|
|
|
|
|
|
>
|
|
|
|
|
|
<q-icon :name="quizStatus.icon" size="20px" />
|
|
|
|
|
|
{{ quizStatus.label }}
|
|
|
|
|
|
</div>
|
2026-01-29 13:17:58 +07:00
|
|
|
|
</div>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div v-else-if="currentLesson.content" class="prose prose-lg dark:prose-invert max-w-none p-6 md:p-8 bg-gray-50 dark:bg-slate-800/50 rounded-2xl border border-gray-100 dark:border-white/5">
|
|
|
|
|
|
<div v-html="getLocalizedText(currentLesson.content)" class="leading-relaxed text-slate-800 dark:text-slate-200"></div>
|
2026-01-26 09:27:31 +07:00
|
|
|
|
</div>
|
2026-01-29 11:09:29 +07:00
|
|
|
|
|
|
|
|
|
|
<!-- Attachments Section -->
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div v-if="currentLesson.attachments && currentLesson.attachments.length > 0" class="mt-8 pt-6 border-t border-gray-100 dark:border-white/5">
|
2026-01-29 11:09:29 +07:00
|
|
|
|
<h3 class="text-lg font-bold mb-4 text-slate-900 dark:text-white flex items-center gap-2">
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div class="w-8 h-8 rounded-lg bg-orange-100 dark:bg-orange-900/30 text-orange-600 flex items-center justify-center">
|
|
|
|
|
|
<q-icon name="attach_file" size="18px" />
|
|
|
|
|
|
</div>
|
2026-02-12 12:01:37 +07:00
|
|
|
|
{{ $t('classroom.attachments') }}
|
2026-01-29 11:09:29 +07:00
|
|
|
|
</h3>
|
|
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
|
|
|
|
<a
|
|
|
|
|
|
v-for="file in currentLesson.attachments"
|
|
|
|
|
|
:key="file.file_name"
|
|
|
|
|
|
:href="file.presigned_url"
|
|
|
|
|
|
target="_blank"
|
2026-02-02 14:37:26 +07:00
|
|
|
|
class="flex items-center gap-4 p-4 rounded-xl border border-slate-200 dark:!border-white/10 bg-white dark:!bg-slate-800 hover:border-blue-300 dark:hover:border-blue-700 hover:shadow-md transition-all group relative overflow-hidden"
|
2026-01-29 11:09:29 +07:00
|
|
|
|
>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div class="w-12 h-12 rounded-xl bg-red-50 dark:bg-red-900/20 text-red-500 flex items-center justify-center flex-shrink-0">
|
2026-01-29 11:09:29 +07:00
|
|
|
|
<q-icon name="picture_as_pdf" size="24px" />
|
|
|
|
|
|
</div>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div class="flex-1 min-w-0 z-10">
|
|
|
|
|
|
<div class="font-bold text-slate-900 dark:text-slate-200 truncate group-hover:text-blue-600 transition-colors text-sm md:text-base">
|
2026-01-29 11:09:29 +07:00
|
|
|
|
{{ file.file_name }}
|
|
|
|
|
|
</div>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div class="text-xs text-slate-500 dark:text-slate-400 mt-0.5">
|
2026-01-29 11:09:29 +07:00
|
|
|
|
{{ (file.file_size / 1024 / 1024).toFixed(2) }} MB
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<div class="absolute inset-0 bg-blue-50/50 dark:bg-blue-900/10 opacity-0 group-hover:opacity-100 transition-opacity"></div>
|
|
|
|
|
|
<q-icon name="download" class="text-slate-300 group-hover:text-blue-500 z-10" />
|
2026-01-29 11:09:29 +07:00
|
|
|
|
</a>
|
|
|
|
|
|
</div>
|
2026-02-12 16:05:37 +07:00
|
|
|
|
</div> <!-- End Attachments -->
|
|
|
|
|
|
</div> <!-- End Lesson Info -->
|
|
|
|
|
|
</div> <!-- End Ready State Wrapper -->
|
|
|
|
|
|
</div> <!-- End Main Content Wrapper -->
|
2026-01-26 09:27:31 +07:00
|
|
|
|
</q-page>
|
|
|
|
|
|
</q-page-container>
|
2026-01-13 10:46:40 +07:00
|
|
|
|
|
2026-02-02 14:37:26 +07:00
|
|
|
|
<!-- Announcements Modal -->
|
2026-02-02 17:13:58 +07:00
|
|
|
|
<AnnouncementModal
|
|
|
|
|
|
v-model="showAnnouncementsModal"
|
|
|
|
|
|
:announcements="announcements"
|
|
|
|
|
|
/>
|
2026-02-02 14:37:26 +07:00
|
|
|
|
|
2026-01-26 09:27:31 +07:00
|
|
|
|
</q-layout>
|
2026-01-13 10:46:40 +07:00
|
|
|
|
</template>
|
|
|
|
|
|
|
2026-01-26 09:27:31 +07:00
|
|
|
|
<style>
|
|
|
|
|
|
.mobile-hide-label .q-btn__content span {
|
|
|
|
|
|
display: none;
|
2026-01-13 10:46:40 +07:00
|
|
|
|
}
|
2026-01-26 09:27:31 +07:00
|
|
|
|
@media (min-width: 768px) {
|
|
|
|
|
|
.mobile-hide-label .q-btn__content span {
|
|
|
|
|
|
display: inline;
|
|
|
|
|
|
margin-left: 8px;
|
|
|
|
|
|
}
|
2026-01-13 10:46:40 +07:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|