<template>
  <div class="student-term-details">
    <BoxedIconTitleBlock
      class="student-term-details__heading"
      title="جزئیات دوره‌ آموزشی"
      desc="اطلاعات کامل این دوره را مشاهده کنید."
    >
      <template #icon>
        <SvgIcon name="book" :size="24" color="var(--color-primary)" />
      </template>
    </BoxedIconTitleBlock>

    <SkeletonLoaderBlock v-if="isLoading && !enrollment" :rows="2" :cols-per-row="1" />
    <StudentTermSummary v-else-if="enrollment" :enrollment="enrollment" />

    <SimpleTitleIconBlock title="همه درس ها" class="student-term-details__list-title">
      <template #header-icon>
        <SvgIcon name="list-bullets" :size="16" color="#bcbcbc" />
      </template>
    </SimpleTitleIconBlock>

    <SkeletonLoaderBlock
      v-if="lessonsLoading && lessons.length === 0"
      :rows="3"
      :cols-per-row="1"
    />
    <div v-else-if="lessons.length > 0">
      <StudentLessonItem
        v-for="lesson in lessons"
        :key="lesson.id"
        :lesson="lesson"
        @show-details="onShowLesson"
      />
    </div>
    <NoItems v-else title="درسی نیست" desc="هنوز درسی برای این دوره ثبت نشده است." />
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import NoItems from '@/components/blocks/NoItems.vue'
import { StudentLessonItem } from '@/features/student/lessons'
import { useStudentTermQuery } from '@/services/query/student-terms'
import { useStudentCoursesListQuery } from '@/services/query/student-courses'
import BoxedIconTitleBlock from '@/components/blocks/BoxedIconTitleBlock.vue'
import SkeletonLoaderBlock from '@/components/blocks/SkeletonLoaderBlock.vue'
import SimpleTitleIconBlock from '@/components/blocks/SimpleTitleIconBlock.vue'
import StudentTermSummary from '@/features/student/terms/components/StudentTermSummary.vue'

const route = useRoute()
const router = useRouter()

const termId = computed(() => route.params.id)

const { data: enrollment, isLoading } = useStudentTermQuery(termId, {
  enabled: () => !!termId.value,
})

const coursesFilters = computed(() => ({ termId: termId.value }))
const coursesPagination = ref({ page: 1, perPage: 10 })
const { data: coursesData, isLoading: lessonsLoading } = useStudentCoursesListQuery(
  coursesFilters,
  coursesPagination,
  { enabled: () => !!termId.value, keepPreviousData: true }
)

const lessons = computed(() => coursesData.value?.data?.items ?? [])

const onShowLesson = (lesson) => {
  router.push({ name: 'student-lesson-details', params: { id: lesson.id } }).catch(() => {})
}
</script>

<style lang="scss" scoped>
.student-term-details {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;

  &__heading {
    margin-bottom: 0.75rem;
  }

  &__list-title {
    margin-top: 0.5rem;
  }
}
</style>
