<template>
  <div class="exam-item">
    <div class="exam-item__main">
      <p class="exam-item__title">{{ exam.title || '—' }}</p>
      <div class="exam-item__sub">
        <span class="exam-item__sub-label">دوره:</span>
        <span class="exam-item__sub-value">
          {{ exam?.session?.course?.title || '—' }}
        </span>
        <span class="exam-item__dot">|</span>
        <span class="exam-item__sub-label">جلسه:</span>
        <span class="exam-item__sub-value">
          {{ exam.session?.title || '—' }}
        </span>
      </div>
    </div>

    <div class="exam-item__meta">
      <div class="exam-item__pill">
        <span class="exam-item__pill-label">مدت آزمون:</span>
        <span class="exam-item__pill-value">{{ duration }}</span>
      </div>
      <div class="exam-item__pill">
        <span class="exam-item__pill-label">تعداد سوالات:</span>
        <span class="exam-item__pill-value">{{ questions }}</span>
      </div>
      <div class="exam-item__pill">
        <span class="exam-item__pill-label">تاریخ ثبت:</span>
        <span class="exam-item__pill-value">{{ createdAt }}</span>
      </div>
    </div>

    <div class="exam-item__actions">
      <CircleButton
        tooltip="شرکت‌کنندگان"
        bg-color="rgba(0, 112, 116, 0.08)"
        size="2.5rem"
        @click="emit('show-participants', exam)"
      >
        <template #icon>
          <SvgIcon name="users-three" :size="18" color="#007074" />
        </template>
      </CircleButton>
      <CircleButton
        tooltip="حذف"
        bg-color="rgba(104, 104, 104, 0.05)"
        size="2.5rem"
        @click="emit('delete', exam)"
      >
        <template #icon>
          <SvgIcon name="trash" :size="18" color="var(--color-error)" />
        </template>
      </CircleButton>
      <BaseButton
        text="جزئیات آزمون"
        custom-class="exam-item__details-btn"
        @click="emit('show-details', exam)"
      >
        <template #appendIcon>
          <SvgIcon name="caret-left" :size="16" color="#fff" />
        </template>
      </BaseButton>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import { formatJalaaliDate } from '@/utils/date-utils'
import CircleButton from '@/components/CircleButton.vue'

const props = defineProps({
  exam: { type: Object, required: true },
})

const emit = defineEmits(['edit', 'delete', 'show-details', 'show-participants'])

const duration = computed(() =>
  props.exam.durationMinutes == null ? '—' : `${props.exam.durationMinutes} دقیقه`
)

const questions = computed(() =>
  props.exam.questionsCount == null ? '—' : `${props.exam.questionsCount} سوال`
)

const createdAt = computed(
  () => props.exam.faCreatedAt || formatJalaaliDate(props.exam.createdAt) || '—'
)
</script>

<style lang="scss" scoped>
.exam-item {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  padding: 0.75rem;
  background: rgba(255, 255, 255, 50%);
  box-shadow: 0 4px 10px -6px rgba(241, 241, 241, 70%);
  border-radius: 0.875rem;
  margin-bottom: 0.625rem;

  @media (min-width: 1024px) {
    flex-flow: row wrap;
    align-items: center;
  }

  &__main {
    flex: 1 1 25%;
    min-width: 0;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-size: 1rem;
    color: #4b4b4b;
    margin: 0 0 0.25rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &__sub {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem;
    font-family: var(--font-family-fa);
    font-size: 0.7rem;
    color: #535353;
  }

  &__sub-label {
    font-weight: 300;
  }

  &__sub-value {
    font-weight: 500;
  }

  &__dot {
    color: #c4c4c4;
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    flex: 1 1 41%;
  }

  &__pill {
    background: rgba(107, 107, 107, 5%);
    padding: 0.25rem 1rem;
    border-radius: 0.875rem;
    line-height: 1.5;
    white-space: nowrap;
  }

  &__pill-label {
    font-family: var(--font-family-fa);
    font-weight: 300;
    font-size: 0.7rem;
    color: #535353;
    margin-inline-end: 0.25rem;
  }

  &__pill-value {
    font-family: var(--font-family-en);
    font-size: 0.7rem;
    font-weight: 500;
    color: #535353;
  }

  &__actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 0.375rem;
    flex: 1 1 100%;

    @media (min-width: 1024px) {
      flex: 1 1 auto;
    }
  }

  &__details-btn {
    min-width: 8rem;
    padding: 0 0.875rem;
  }
}
</style>
