2026-01-13 10:46:40 +07:00
|
|
|
<script setup lang="ts">
|
|
|
|
|
/**
|
|
|
|
|
* @file my-courses.vue
|
|
|
|
|
* @description My Courses Page.
|
|
|
|
|
* Displays enrolled courses with filters for progress/completed.
|
|
|
|
|
* Handles enrollment success modals and certificate downloads.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
definePageMeta({
|
|
|
|
|
layout: 'default',
|
|
|
|
|
middleware: 'auth'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
useHead({
|
|
|
|
|
title: 'คอร์สของฉัน - e-Learning'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const route = useRoute()
|
|
|
|
|
const showEnrollModal = ref(false)
|
|
|
|
|
const activeFilter = ref<'all' | 'progress' | 'completed'>('all')
|
2026-02-12 12:01:37 +07:00
|
|
|
const searchQuery = ref('')
|
2026-02-02 15:34:40 +07:00
|
|
|
|
2026-01-13 10:46:40 +07:00
|
|
|
|
|
|
|
|
// Check URL query parameters to show 'Enrollment Success' modal
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
if (route.query.enrolled) {
|
|
|
|
|
showEnrollModal.value = true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-11 11:25:55 +07:00
|
|
|
const { locale } = useI18n()
|
|
|
|
|
|
2026-01-20 15:13:02 +07:00
|
|
|
// Helper to get localized text
|
|
|
|
|
const getLocalizedText = (text: string | { th: string; en: string } | undefined) => {
|
|
|
|
|
if (!text) return ''
|
|
|
|
|
if (typeof text === 'string') return text
|
2026-02-11 11:25:55 +07:00
|
|
|
|
|
|
|
|
const currentLocale = locale.value as 'th' | 'en'
|
|
|
|
|
return text[currentLocale] || text.th || text.en || ''
|
2026-01-20 15:13:02 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Data Handling
|
2026-01-30 14:34:45 +07:00
|
|
|
const { fetchEnrolledCourses, getCertificate, generateCertificate } = useCourse()
|
2026-01-20 15:13:02 +07:00
|
|
|
const enrolledCourses = ref<any[]>([])
|
|
|
|
|
const isLoading = ref(false)
|
2026-01-30 14:34:45 +07:00
|
|
|
const isDownloadingCert = ref(false)
|
2026-01-20 15:13:02 +07:00
|
|
|
|
|
|
|
|
const loadEnrolledCourses = async () => {
|
|
|
|
|
isLoading.value = true
|
2026-01-29 17:52:52 +07:00
|
|
|
// FIX: For 'progress' tab, we want both ENROLLED and IN_PROGRESS.
|
|
|
|
|
// Since API takes single status, we fetch ALL and filter locally for 'progress'.
|
|
|
|
|
const apiStatus = activeFilter.value === 'completed'
|
2026-01-20 15:13:02 +07:00
|
|
|
? 'COMPLETED'
|
2026-01-29 17:52:52 +07:00
|
|
|
: undefined // 'all' or 'progress' -> fetch all
|
2026-01-26 09:27:31 +07:00
|
|
|
|
2026-01-20 15:13:02 +07:00
|
|
|
const res = await fetchEnrolledCourses({
|
2026-01-29 11:09:29 +07:00
|
|
|
status: apiStatus
|
2026-01-20 15:13:02 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (res.success) {
|
2026-01-29 17:52:52 +07:00
|
|
|
let courses = (res.data || [])
|
|
|
|
|
|
2026-02-06 14:09:02 +07:00
|
|
|
// Local filter to ensure UI consistency regardless of backend filtering
|
2026-01-29 17:52:52 +07:00
|
|
|
if (activeFilter.value === 'progress') {
|
|
|
|
|
courses = courses.filter(c => c.status !== 'COMPLETED')
|
2026-02-06 14:09:02 +07:00
|
|
|
} else if (activeFilter.value === 'completed') {
|
|
|
|
|
courses = courses.filter(c => c.status === 'COMPLETED')
|
2026-01-29 17:52:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enrolledCourses.value = courses.map(item => ({
|
2026-01-26 09:27:31 +07:00
|
|
|
id: item.course_id,
|
2026-01-20 15:13:02 +07:00
|
|
|
enrollment_id: item.id,
|
2026-02-11 15:05:19 +07:00
|
|
|
title: item.course.title,
|
2026-01-29 17:52:52 +07:00
|
|
|
progress: item.progress_percentage || 0,
|
2026-02-12 12:01:37 +07:00
|
|
|
lessons: item.course.total_lessons || 0,
|
2026-01-20 15:13:02 +07:00
|
|
|
completed: item.status === 'COMPLETED',
|
2026-01-26 09:27:31 +07:00
|
|
|
thumbnail_url: item.course.thumbnail_url
|
2026-01-20 15:13:02 +07:00
|
|
|
}))
|
2026-01-13 10:46:40 +07:00
|
|
|
}
|
2026-01-20 15:13:02 +07:00
|
|
|
isLoading.value = false
|
|
|
|
|
}
|
2026-01-13 10:46:40 +07:00
|
|
|
|
2026-01-20 15:13:02 +07:00
|
|
|
// Watch filter changes to reload
|
|
|
|
|
watch(activeFilter, () => {
|
|
|
|
|
loadEnrolledCourses()
|
2026-01-13 10:46:40 +07:00
|
|
|
})
|
|
|
|
|
|
2026-02-12 12:01:37 +07:00
|
|
|
// Client-side Search Filtering
|
|
|
|
|
const filteredEnrolledCourses = computed(() => {
|
|
|
|
|
if (!searchQuery.value) return enrolledCourses.value
|
|
|
|
|
const query = searchQuery.value.toLowerCase()
|
|
|
|
|
return enrolledCourses.value.filter(c => {
|
|
|
|
|
const title = getLocalizedText(c.title).toLowerCase()
|
|
|
|
|
return title.includes(query)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-01-20 15:13:02 +07:00
|
|
|
onMounted(() => {
|
|
|
|
|
if (route.query.enrolled) {
|
|
|
|
|
showEnrollModal.value = true
|
|
|
|
|
}
|
|
|
|
|
loadEnrolledCourses()
|
|
|
|
|
})
|
2026-01-13 10:46:40 +07:00
|
|
|
|
2026-01-28 16:59:44 +07:00
|
|
|
// Certificate Handling
|
2026-01-30 14:34:45 +07:00
|
|
|
const downloadingCourseId = ref<number | null>(null)
|
|
|
|
|
// Certificate Handling
|
2026-01-30 14:42:08 +07:00
|
|
|
|
2026-01-30 14:34:45 +07:00
|
|
|
const downloadCertificate = async (course: any) => {
|
|
|
|
|
if (!course) return
|
|
|
|
|
downloadingCourseId.value = course.id
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 1. Try to GET existing certificate
|
|
|
|
|
let res = await getCertificate(course.id)
|
|
|
|
|
|
|
|
|
|
// 2. If not found (or error), try to GENERATE new one
|
|
|
|
|
if (!res.success) {
|
|
|
|
|
res = await generateCertificate(course.id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Handle Result
|
|
|
|
|
if (res.success && res.data) {
|
|
|
|
|
const cert = res.data
|
|
|
|
|
if (cert.download_url) {
|
|
|
|
|
window.open(cert.download_url, '_blank')
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback if no URL but success (maybe show message)
|
|
|
|
|
console.warn('Certificate ready but no URL')
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Silent fail or minimal log, or maybe use a toast if available, but avoid $q if undefined
|
|
|
|
|
console.error(res.error || 'Failed to get certificate')
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e)
|
|
|
|
|
} finally {
|
|
|
|
|
downloadingCourseId.value = null
|
|
|
|
|
}
|
2026-01-13 10:46:40 +07:00
|
|
|
}
|
2026-02-02 14:37:26 +07:00
|
|
|
|
|
|
|
|
const validCourseId = computed(() => {
|
|
|
|
|
const cid = route.query.course_id
|
|
|
|
|
if (!cid || cid === 'undefined' || cid === 'null' || cid === 'NaN') return null
|
|
|
|
|
return cid
|
|
|
|
|
})
|
2026-01-13 10:46:40 +07:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2026-02-09 10:37:42 +07:00
|
|
|
<div class="page-container">
|
2026-01-26 09:27:31 +07:00
|
|
|
|
|
|
|
|
|
2026-02-12 12:01:37 +07:00
|
|
|
<!-- Page Header & Filters (Unified Layout) -->
|
2026-02-19 17:37:28 +07:00
|
|
|
<div class="mb-8">
|
|
|
|
|
<div class="flex items-start gap-4 mb-2">
|
|
|
|
|
<span class="w-1.5 h-10 md:h-12 bg-blue-600 rounded-full shadow-lg shadow-blue-500/50 mt-1 flex-shrink-0"></span>
|
|
|
|
|
<div>
|
|
|
|
|
<h1 class="text-3xl md:text-4xl font-black text-slate-900 dark:text-white leading-tight">
|
|
|
|
|
{{ $t('sidebar.myCourses') }}
|
|
|
|
|
</h1>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="flex flex-col md:flex-row md:items-center justify-between gap-4 mt-4">
|
|
|
|
|
<!-- Filter Tabs (Horizontal Bar) -->
|
|
|
|
|
<div class="bg-white dark:bg-slate-900/50 p-1.5 rounded-2xl border border-slate-100 dark:border-white/5 inline-flex items-center gap-1 shadow-sm">
|
|
|
|
|
<q-btn
|
|
|
|
|
v-for="filter in ['all', 'progress', 'completed']"
|
|
|
|
|
:key="filter"
|
|
|
|
|
@click="activeFilter = filter as any"
|
|
|
|
|
flat
|
|
|
|
|
rounded
|
|
|
|
|
dense
|
|
|
|
|
class="px-5 py-2 font-bold transition-all text-[11px] uppercase tracking-wider"
|
|
|
|
|
:class="activeFilter === filter ? 'bg-blue-600 text-white shadow-md shadow-blue-600/20' : 'text-slate-600 dark:text-slate-400 hover:bg-slate-50 dark:hover:bg-slate-800'"
|
|
|
|
|
:label="$t(`myCourses.filter${filter.charAt(0).toUpperCase() + filter.slice(1)}`)"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2026-02-12 12:01:37 +07:00
|
|
|
|
|
|
|
|
<!-- Search Input -->
|
2026-02-19 17:37:28 +07:00
|
|
|
<div class="w-full md:w-72">
|
2026-02-12 12:01:37 +07:00
|
|
|
<q-input
|
|
|
|
|
v-model="searchQuery"
|
|
|
|
|
dense
|
|
|
|
|
outlined
|
|
|
|
|
rounded
|
|
|
|
|
:placeholder="$t('discovery.searchPlaceholder')"
|
2026-02-19 17:37:28 +07:00
|
|
|
class="search-input shadow-sm"
|
2026-02-12 12:01:37 +07:00
|
|
|
bg-color="transparent"
|
|
|
|
|
>
|
|
|
|
|
<template v-slot:prepend>
|
|
|
|
|
<q-icon name="search" class="text-slate-400" />
|
|
|
|
|
</template>
|
|
|
|
|
</q-input>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-01-13 10:46:40 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- Courses Grid -->
|
2026-01-20 15:13:02 +07:00
|
|
|
<div v-if="isLoading" class="flex justify-center py-20">
|
2026-01-26 10:06:22 +07:00
|
|
|
<q-spinner size="3rem" color="primary" />
|
2026-01-20 15:13:02 +07:00
|
|
|
</div>
|
2026-01-26 09:27:31 +07:00
|
|
|
<div v-else class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
|
2026-02-12 12:01:37 +07:00
|
|
|
<template v-for="course in filteredEnrolledCourses" :key="course.id">
|
2026-01-13 10:46:40 +07:00
|
|
|
<!-- In Progress Course Card -->
|
|
|
|
|
<CourseCard
|
|
|
|
|
v-if="!course.completed"
|
2026-01-21 17:03:09 +07:00
|
|
|
:id="course.id"
|
2026-01-13 10:46:40 +07:00
|
|
|
:title="course.title"
|
|
|
|
|
:progress="course.progress"
|
2026-01-20 15:13:02 +07:00
|
|
|
:image="course.thumbnail_url"
|
2026-01-13 10:46:40 +07:00
|
|
|
show-continue
|
2026-02-09 14:30:05 +07:00
|
|
|
:show-view-details="false"
|
2026-01-13 10:46:40 +07:00
|
|
|
/>
|
|
|
|
|
<!-- Completed Course Card -->
|
|
|
|
|
<CourseCard
|
|
|
|
|
v-else
|
2026-01-21 17:03:09 +07:00
|
|
|
:id="course.id"
|
2026-01-13 10:46:40 +07:00
|
|
|
:title="course.title"
|
2026-01-20 15:13:02 +07:00
|
|
|
:progress="100"
|
|
|
|
|
:image="course.thumbnail_url"
|
2026-01-13 10:46:40 +07:00
|
|
|
:completed="true"
|
|
|
|
|
show-certificate
|
|
|
|
|
show-study-again
|
2026-02-09 14:30:05 +07:00
|
|
|
:show-view-details="false"
|
2026-01-30 14:34:45 +07:00
|
|
|
:loading="downloadingCourseId === course.id"
|
|
|
|
|
@view-certificate="downloadCertificate(course)"
|
2026-01-13 10:46:40 +07:00
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- Empty State -->
|
2026-02-12 12:01:37 +07:00
|
|
|
<div v-if="!isLoading && filteredEnrolledCourses.length === 0" class="flex flex-col items-center justify-center py-20 bg-slate-50 dark:bg-slate-800/50 rounded-3xl border-2 border-dashed border-slate-200 dark:border-slate-700 mt-4">
|
|
|
|
|
<q-icon v-if="searchQuery" name="search_off" size="64px" class="text-slate-300 dark:text-slate-600 mb-4" />
|
|
|
|
|
<h3 class="text-xl font-bold text-slate-900 dark:text-white mb-2">
|
|
|
|
|
{{ searchQuery ? $t('discovery.emptyTitle') : $t('myCourses.emptyTitle') }}
|
|
|
|
|
</h3>
|
|
|
|
|
<p class="text-slate-500 dark:text-slate-400 text-center max-w-md">
|
|
|
|
|
{{ searchQuery ? $t('discovery.emptyDesc') : $t('myCourses.emptyDesc') }}
|
|
|
|
|
</p>
|
|
|
|
|
<NuxtLink v-if="!searchQuery" to="/browse/discovery" class="mt-6 px-6 py-2 bg-blue-600 text-white rounded-lg font-bold hover:bg-blue-700 transition-colors">{{ $t('myCourses.goToDiscovery') }}</NuxtLink>
|
|
|
|
|
<button v-else class="mt-4 font-bold text-blue-600 hover:text-blue-700 transition-colors" @click="searchQuery = ''">
|
|
|
|
|
{{ $t('discovery.showAll') }}
|
|
|
|
|
</button>
|
2026-01-13 10:46:40 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- MODAL: Enrollment Success -->
|
2026-01-26 10:06:22 +07:00
|
|
|
<q-dialog v-model="showEnrollModal" backdrop-filter="blur(4px)">
|
|
|
|
|
<q-card class="rounded-[1.5rem] shadow-2xl p-8 max-w-sm w-full text-center relative overflow-hidden bg-white dark:bg-slate-800">
|
2026-01-26 09:27:31 +07:00
|
|
|
<div class="absolute top-0 left-0 w-full h-2 bg-gradient-to-r from-green-400 to-emerald-600"></div>
|
|
|
|
|
<div class="w-16 h-16 bg-green-100 dark:bg-green-900/30 text-green-600 dark:text-green-400 rounded-full flex items-center justify-center text-3xl mx-auto mb-6">
|
2026-01-13 10:46:40 +07:00
|
|
|
✓
|
|
|
|
|
</div>
|
2026-01-26 09:27:31 +07:00
|
|
|
<h2 class="text-2xl font-bold mb-2 text-slate-900 dark:text-white">{{ $t('enrollment.successTitle') }}</h2>
|
|
|
|
|
<p class="text-slate-500 dark:text-slate-400 mb-8">{{ $t('enrollment.successDesc') }}</p>
|
|
|
|
|
<div class="flex flex-col gap-3">
|
2026-01-26 10:06:22 +07:00
|
|
|
<q-btn
|
2026-02-02 14:37:26 +07:00
|
|
|
v-if="validCourseId"
|
|
|
|
|
:to="`/classroom/learning?course_id=${validCourseId}`"
|
2026-01-26 10:06:22 +07:00
|
|
|
unelevated
|
|
|
|
|
rounded
|
|
|
|
|
color="primary"
|
|
|
|
|
class="w-full py-3 text-lg font-bold shadow-lg"
|
|
|
|
|
:label="$t('enrollment.startNow')"
|
2026-02-02 14:37:26 +07:00
|
|
|
/>
|
|
|
|
|
<q-btn
|
|
|
|
|
v-else
|
|
|
|
|
unelevated
|
|
|
|
|
rounded
|
|
|
|
|
color="primary"
|
|
|
|
|
class="w-full py-3 text-lg font-bold shadow-lg"
|
|
|
|
|
:label="$t('common.close')"
|
|
|
|
|
@click="showEnrollModal = false"
|
2026-01-26 10:06:22 +07:00
|
|
|
/>
|
|
|
|
|
<q-btn
|
2026-02-02 14:37:26 +07:00
|
|
|
v-if="validCourseId"
|
2026-01-26 10:06:22 +07:00
|
|
|
flat
|
|
|
|
|
rounded
|
|
|
|
|
color="grey-7"
|
|
|
|
|
class="w-full py-3 font-bold"
|
|
|
|
|
:label="$t('enrollment.later')"
|
|
|
|
|
@click="showEnrollModal = false"
|
|
|
|
|
/>
|
2026-01-13 10:46:40 +07:00
|
|
|
</div>
|
2026-01-26 10:06:22 +07:00
|
|
|
</q-card>
|
|
|
|
|
</q-dialog>
|
2026-01-13 10:46:40 +07:00
|
|
|
|
2026-01-26 09:27:31 +07:00
|
|
|
|
2026-01-13 10:46:40 +07:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2026-01-26 09:27:31 +07:00
|
|
|
/* Custom Font for Signature/Name if desired */
|
|
|
|
|
.font-handwriting {
|
|
|
|
|
font-family: 'Dancing Script', cursive, serif; /* Fallback */
|
2026-01-13 10:46:40 +07:00
|
|
|
}
|
|
|
|
|
</style>
|