<template>
  <div class="sti">
    <div class="sti__box">
      <div class="sti__box__image">
        <img v-if="term.coverUrl" :src="term.coverUrl" :alt="term.title" />
        <SvgIcon v-else name="book" :size="32" color="#bcbcbc" />
      </div>
      <div class="sti__info">
        <p class="sti__title">{{ term.title || '—' }}</p>
        <div class="sti__teacher">
          <span class="sti__teacher-label">تعداد دوره ها:</span>
          <span class="sti__teacher-name">{{ term.coursesCount ?? 0 }}</span>
        </div>
      </div>
    </div>

    <div class="sti__meta">
      <div class="sti__dates">
        <div class="sti__date">
          <span class="sti__date-label">تاریخ پایان :</span>
          <span class="sti__date-value">{{ endDate }}</span>
        </div>
        <span class="sti__date-divider" />
        <div class="sti__date">
          <span class="sti__date-label">تاریخ شروع :</span>
          <span class="sti__date-value">{{ startDate }}</span>
        </div>
      </div>
      <div class="sti__status" :class="`sti__status--${status}`">
        <span class="sti__status-label">وضعیت دوره :</span>
        <span class="sti__status-value">{{ statusLabel }}</span>
      </div>
    </div>

    <div v-if="showDetails" class="sti__actions">
      <BaseButton
        text="جزئیات ترم"
        custom-class="sti__details-btn"
        @click="emit('show-details', enrollment)"
      >
        <template #appendIcon>
          <SvgIcon name="arrow-left" :size="14" color="#fff" />
        </template>
      </BaseButton>
    </div>
  </div>
</template>

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

const props = defineProps({
  enrollment: { type: Object, required: true },
  showDetails: { type: Boolean, default: true },
})

const emit = defineEmits(['show-details'])

const term = computed(() => props.enrollment.term ?? {})

const status = computed(() => props.enrollment.status || 'active')

const statusLabel = computed(() => STUDENT_COURSE_STATUS[status.value] || '—')

const startDate = computed(() => formatJalaaliDate(term.value.startsAt) || '—')

const endDate = computed(() => formatJalaaliDate(term.value.endsAt) || '—')
</script>

<style lang="scss" scoped>
.sti {
  display: flex;
  flex-direction: column-reverse;
  gap: 0.625rem;
  padding: 0.875rem;
  background: rgba(255, 255, 255, 58%);
  box-shadow: 0 6.75px 19.425px rgba(0, 0, 0, 4%);
  border-radius: 0.75rem;
  margin-bottom: 0.625rem;

  @media (min-width: 1024px) {
    flex-direction: row;
    align-items: center;
    gap: 1rem;
  }

  &__box {
    display: flex;
    align-items: center;

    &__image {
      width: 2.5rem;
      height: 2.5rem;
      border-radius: 0.5rem;
      background: #f5f5f5;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }

  &__info {
    flex: 1 1 25%;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    text-align: end;
    margin-right: 4px;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-weight: 400;
    font-size: 0.95rem;
    color: #4b4b4b;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
    text-align: right;
  }

  &__teacher {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-family: var(--font-family-fa);
    font-size: 0.65rem;
    color: #4b4b4b;
  }

  &__teacher-label {
    color: #4b4b4b;
  }

  &__teacher-name {
    color: #4b4b4b;
  }

  &__meta {
    flex: 1 1 40%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
  }

  &__status {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 1rem;
    border-radius: 0.75rem;
    font-family: var(--font-family-fa);
    font-weight: 500;
    font-size: 0.75rem;
    white-space: nowrap;

    &--active {
      background: rgba(0, 112, 116, 4%);
      color: #007074;
    }

    &--completed {
      background: rgba(0, 154, 18, 6%);
      color: #009a12;
    }
  }

  &__status-label {
    font-weight: 300;
    font-size: 0.7rem;
    color: inherit;
    opacity: 0.85;
  }

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

  &__dates {
    display: inline-flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.375rem 1rem;
    border-radius: 0.75rem;
    background: rgba(107, 107, 107, 4%);
    white-space: nowrap;
  }

  &__date {
    display: inline-flex;
    align-items: baseline;
    gap: 0.375rem;
  }

  &__date-label {
    font-family: var(--font-family-fa);
    font-weight: 300;
    font-size: 0.7rem;
    color: #535353;
  }

  &__date-value {
    font-family: var(--font-family-en);
    font-size: 0.75rem;
    color: #5d5d5d;
  }

  &__date-divider {
    width: 1px;
    height: 0.625rem;
    background: #dadada;
  }

  &__actions {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.625rem;
  }

  &__accent {
    width: 3.25rem;
    height: 3.35rem;
    border-radius: 0.75rem;
    background: #f4f4f4;
    display: none;

    @media (min-width: 1024px) {
      display: block;
    }
  }

  &__details-btn {
    min-width: 6rem;
    padding: 0 1rem;
    height: 2rem;
  }
}
</style>
