<template>
  <div class="assignments-page">
    <BoxedIconTitleBlock
      class="assignments-page__heading"
      title="مدیریت تکالیف"
      desc="در این قسمت می‌توانید تکالیف خود را مدیریت کنید"
    >
      <template #icon>
        <img :src="assignmentIcon" alt="" />
      </template>
    </BoxedIconTitleBlock>

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

    <div class="assignments-page__list-header">
      <SimpleTitleIconBlock title="لیست همه تکالیف" class="assignments-page__list-title">
        <template #header-icon>
          <SvgIcon name="list-bullets" :size="16" color="#bcbcbc" />
        </template>
      </SimpleTitleIconBlock>
      <BaseButton text="افزودن تکلیف جدید" custom-class="assignments-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="assignments.length > 0">
      <AssignmentItem
        v-for="assignment in assignments"
        :key="assignment.id"
        :assignment="assignment"
        @edit="onEdit"
        @delete="onAskDelete"
        @show-details="onShowDetails"
        @show-submissions="onShowSubmissions"
      />
    </div>
    <NoItems v-else title="متاسفیم" desc="تکلیفی برای نمایش وجود ندارد." />

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

    <AddAssignmentModal v-if="isModal('AddAssignmentModal')" />
    <AssignmentSubmissionsModal v-if="isModal('AssignmentSubmissionsModal')" />
    <AssignmentSubmissionDetailsModal v-if="isModal('AssignmentSubmissionDetailsModal')" />
    <AssignmentDetailsModal v-if="isModal('AssignmentDetailsModal')" />
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
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 BoxedIconTitleBlock from '@/components/blocks/BoxedIconTitleBlock.vue'
import SkeletonLoaderBlock from '@/components/blocks/SkeletonLoaderBlock.vue'
import SimpleTitleIconBlock from '@/components/blocks/SimpleTitleIconBlock.vue'
import AssignmentItem from '@/features/admin/assignments/components/AssignmentItem.vue'
import AssignmentsFilters from '@/features/admin/assignments/components/AssignmentsFilters.vue'
import AddAssignmentModal from '@/features/admin/assignments/components/modals/AddAssignmentModal.vue'
import AssignmentDetailsModal from '@/features/admin/assignments/components/modals/AssignmentDetailsModal.vue'
import AssignmentSubmissionsModal from '@/features/admin/assignments/components/modals/AssignmentSubmissionsModal.vue'
import AssignmentSubmissionDetailsModal from '@/features/admin/assignments/components/modals/AssignmentSubmissionDetailsModal.vue'
import {
  adminAssignmentsKeys,
  useAdminAssignmentsListQuery,
  useDeleteAdminAssignmentMutation,
} from '@/services/query/admin-assignments'

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

const assignmentIcon = gallery.assignmentIcon

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

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

const assignments = 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 = () => openModal('AddAssignmentModal', { mode: 'add' })
const onEdit = (assignment) => openModal('AddAssignmentModal', { mode: 'edit', id: assignment.id })

const onShowDetails = (assignment) => openModal('AssignmentDetailsModal', { id: assignment.id })

const onShowSubmissions = (assignment) =>
  openModal('AssignmentSubmissionsModal', { id: assignment.id, title: assignment.title })

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

const deleteMutation = useDeleteAdminAssignmentMutation()

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

<style lang="scss" scoped>
.assignments-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>
