<template>
  <div class="student-terms">
    <BoxedIconTitleBlock
      class="student-terms__heading"
      title="ترم‌های آموزشی"
      desc="ترم‌ها و آموزش‌های گذرانده‌شده خود را مشاهده کنید."
    >
      <template #icon>
        <SvgIcon name="book" :size="24" color="var(--color-primary)" />
      </template>
    </BoxedIconTitleBlock>

    <TabsBlock :tabs="tabs" v-model="activeTab">
      <template #current>
        <SkeletonLoaderBlock v-if="currentPending" :rows="4" :cols-per-row="1" />
        <div v-else-if="currentTerms.length > 0">
          <StudentTermItem
            v-for="enrollment in currentTerms"
            :key="enrollment.id"
            :enrollment="enrollment"
            @show-details="onShowDetails"
          />
        </div>
        <NoItems v-else title="ترمی نیست" desc="در حال حاضر ترمی در حال گذراندن ندارید." />
        <PaginationBlock :pagination="currentMeta" @update:page="setCurrentPage" />
      </template>

      <template #ended>
        <SkeletonLoaderBlock v-if="endedPending" :rows="4" :cols-per-row="1" />
        <div v-else-if="endedTerms.length > 0">
          <StudentTermItem
            v-for="enrollment in endedTerms"
            :key="enrollment.id"
            :enrollment="enrollment"
            :showDetails="false"
            @show-details="onShowDetails"
          />
        </div>
        <NoItems v-else title="موردی نیست" desc="هنوز ترم‌ای را تمام نکرده‌اید." />
        <PaginationBlock :pagination="endedMeta" @update:page="setEndedPage" />
      </template>
    </TabsBlock>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import NoItems from '@/components/blocks/NoItems.vue'
import TabsBlock from '@/components/blocks/TabsBlock.vue'
import { usePagination } from '@/composables/usePagination'
import PaginationBlock from '@/components/blocks/PaginationBlock.vue'
import { useStudentTermsListQuery } from '@/services/query/student-terms'
import BoxedIconTitleBlock from '@/components/blocks/BoxedIconTitleBlock.vue'
import SkeletonLoaderBlock from '@/components/blocks/SkeletonLoaderBlock.vue'
import StudentTermItem from '@/features/student/terms/components/StudentTermItem.vue'

const router = useRouter()

const tabs = [
  { name: 'current', label: 'ترم های در حال گذراندن', icon: 'list-bullets' },
  { name: 'ended', label: 'ترم های تکمیل شده', icon: 'list-bullets' },
]
const activeTab = ref('current')

const currentFilters = ref({ status: 'active' })
const endedFilters = ref({ status: 'completed' })

const { pagination: currentPagination, setPage: setCurrentPage } = usePagination({
  page: 1,
  perPage: 10,
})

const { pagination: endedPagination, setPage: setEndedPage } = usePagination({
  page: 1,
  perPage: 10,
})

const { data: currentData, isLoading: currentPending } = useStudentTermsListQuery(
  currentFilters,
  currentPagination,
  {
    enabled: () => activeTab.value === 'current',
    keepPreviousData: true,
  }
)

const { data: endedData, isLoading: endedPending } = useStudentTermsListQuery(
  endedFilters,
  endedPagination,
  {
    enabled: () => activeTab.value === 'ended',
    keepPreviousData: true,
  }
)

const currentTerms = computed(() => currentData.value?.data ?? [])
const endedTerms = computed(() => endedData.value?.data ?? [])

const currentMeta = computed(() => ({
  page: currentPagination.value.page,
  perPage: currentPagination.value.perPage,
  ...currentData.value?.meta,
}))

const endedMeta = computed(() => ({
  page: endedPagination.value.page,
  perPage: endedPagination.value.perPage,
  ...endedData.value?.meta,
}))

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

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

  &__heading {
    margin-bottom: 0.75rem;
  }
}
</style>
