<template>
  <div class="exams-page">
    <BoxedIconTitleBlock
      class="exams-page__heading"
      title="مدیریت آزمون‌ها"
      desc="در این قسمت می‌توانید آزمون‌های خود را مدیریت کنید"
    >
      <template #icon>
        <img :src="sessionIcon" alt="" />
      </template>
    </BoxedIconTitleBlock>

    <ExamsFilters v-model="filters" @apply="onFilterApply" @reset="onFilterReset" />

    <div class="exams-page__list-header">
      <SimpleTitleIconBlock title="لیست همه آزمون‌ها" class="exams-page__list-title">
        <template #header-icon>
          <SvgIcon name="list-bullets" :size="16" color="#bcbcbc" />
        </template>
      </SimpleTitleIconBlock>
      <BaseButton text="افزودن آزمون جدید" custom-class="exams-page__add-btn" @click="onAdd">
        <template #appendIcon>
          <SvgIcon name="arrow-left" :size="18" color="#fff" />
        </template>
      </BaseButton>
    </div>

    <SkeletonLoaderBlock v-if="isLoading" :rows="6" :cols-per-row="1" />
    <div v-else-if="exams.length > 0">
      <ExamItem
        v-for="exam in exams"
        :key="exam.id"
        :exam="exam"
        @edit="onEdit"
        @delete="onAskDelete"
        @show-details="onShowDetails"
        @show-participants="onShowParticipants"
      />
    </div>
    <NoItems v-else title="متاسفیم" desc="آزمونی برای نمایش وجود ندارد." />

    <PaginationBlock :pagination="paginationMeta" @update:page="setPage" />

    <ExamParticipantsModal v-if="isModal('ExamParticipantsModal')" />
    <ExamParticipantDetailsModal v-if="isModal('ExamParticipantDetailsModal')" />
    <ExamDetailsModal v-if="isModal('ExamDetailsModal')" />
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { gallery } from '@/utils/gallery'
import useModal from '@/composables/useModal'
import { useQueryClient } from '@tanstack/vue-query'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import NoItems from '@/components/blocks/NoItems.vue'
import { usePagination } from '@/composables/usePagination'
import PaginationBlock from '@/components/blocks/PaginationBlock.vue'
import ExamItem from '@/features/admin/exams/components/ExamItem.vue'
import BoxedIconTitleBlock from '@/components/blocks/BoxedIconTitleBlock.vue'
import SkeletonLoaderBlock from '@/components/blocks/SkeletonLoaderBlock.vue'
import ExamsFilters from '@/features/admin/exams/components/ExamsFilters.vue'
import SimpleTitleIconBlock from '@/components/blocks/SimpleTitleIconBlock.vue'
import ExamDetailsModal from '@/features/admin/exams/components/modals/ExamDetailsModal.vue'
import ExamParticipantsModal from '@/features/admin/exams/components/modals/ExamParticipantsModal.vue'
import ExamParticipantDetailsModal from '@/features/admin/exams/components/modals/ExamParticipantDetailsModal.vue'
import {
  adminExamsKeys,
  useAdminExamsListQuery,
  useDeleteAdminExamMutation,
} from '@/services/query/admin-exams'

const router = useRouter()
const queryClient = useQueryClient()
const { openModal, isModal } = useModal()

const sessionIcon = gallery.sessionIcon

const filters = ref({
  title: '',
  termId: '',
  courseId: '',
  sessionId: '',
  fromDate: '',
  toDate: '',
})
const { pagination, setPage, reset: resetPagination } = usePagination({ page: 1, perPage: 10 })

const { data, isLoading } = useAdminExamsListQuery(filters, pagination, {
  keepPreviousData: true,
})

const exams = computed(() => data.value?.data ?? [])
const paginationMeta = computed(() => ({
  page: pagination.value.page,
  perPage: pagination.value.perPage,
  ...data.value?.meta,
}))

const onFilterApply = () => resetPagination()
const onFilterReset = () => resetPagination()

const onAdd = () => {
  router.push({ name: 'admin-add-exam' }).catch(() => {})
}

const onEdit = (exam) => {
  router.push({ name: 'admin-edit-exam', params: { id: exam.id } }).catch(() => {})
}

const onShowDetails = (exam) => {
  openModal('ExamDetailsModal', { id: exam.id })
}

const onShowParticipants = (exam) => {
  openModal('ExamParticipantsModal', { id: exam.id, title: exam.title })
}

const invalidate = () => queryClient.invalidateQueries({ queryKey: adminExamsKeys.all })

const deleteMutation = useDeleteAdminExamMutation()

const onAskDelete = (exam) => {
  openModal('ConfirmModal', {
    title: `حذف ${exam.title}`,
    message: `بعد از حذف امکان بازگشت وجود ندارد، آیا مطمئن هستید که می‌خواهید <strong>${exam.title}</strong> را حذف کنید؟`,
    onConfirm: () => deleteMutation.mutate(exam.id, { onSuccess: invalidate }),
  })
}
</script>

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

  &__heading {
    margin-bottom: 0.75rem;
  }

  &__list-header {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    margin-bottom: 0.375rem;
  }

  &__list-title {
    flex: 1;
    margin-bottom: 0;
  }

  &__add-btn {
    min-width: fit-content;
    padding: 0 1.25rem;
  }
}
</style>
