<template>
  <div class="term-item">
    <div class="term-item__main">
      <div v-if="term.coverUrl" class="term-item__image">
        <img :src="term.coverUrl" :alt="term.title" />
      </div>
      <div v-else class="term-item__image term-item__image--placeholder">
        <SvgIcon name="book" :size="24" color="#bcbcbc" />
      </div>
      <p class="term-item__title">{{ term.title }}</p>
    </div>

    <div class="term-item__dates">
      <div class="term-item__date">
        <span class="term-item__date-label">تاریخ ایجاد:</span>
        <span class="term-item__date-value">{{ createdDate || '—' }}</span>
      </div>
    </div>

    <div v-if="!term.isActive" class="term-item__status">
      <span class="term-item__status-badge">
        <span class="term-item__status-dot" />
        غیرفعال
      </span>
    </div>

    <div class="term-item__actions">
      <ToggleSwitch
        :model-value="!!term.isActive"
        @update:model-value="emit('change-status', { id: term.id, isActive: !term.isActive })"
      />
      <CircleButton
        tooltip="حذف"
        bg-color="rgba(104, 104, 104, 0.05)"
        size="2.5rem"
        @click="emit('delete', term)"
      >
        <template #icon>
          <SvgIcon name="trash" :size="18" color="var(--color-error)" />
        </template>
      </CircleButton>
      <CircleButton
        tooltip="ویرایش"
        bg-color="rgba(104, 104, 104, 0.05)"
        size="2.5rem"
        @click="emit('edit', term)"
      >
        <template #icon>
          <SvgIcon name="pencil" :size="18" color="var(--color-sec-gray)" />
        </template>
      </CircleButton>
      <BaseButton
        text="جزئیات ترم"
        custom-class="term-item__details-btn"
        @click="emit('show-details', term)"
      >
        <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'
import ToggleSwitch from '@/components/form/ToggleSwitch.vue'

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

const emit = defineEmits(['edit', 'delete', 'clone', 'change-status', 'show-details'])

const createdDate = computed(() => formatJalaaliDate(props.term.createdAt) || '')
</script>

<style lang="scss" scoped>
.term-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: 768px) {
    flex-flow: row wrap;
    align-items: center;
    gap: 0.625rem;
  }

  &__main {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    min-width: 0;
    flex: 1 1 33%;
  }

  &__image {
    width: 3rem;
    height: 3rem;
    min-width: 3rem;
    border-radius: 0.375rem;
    border: 1px solid #eee;
    overflow: hidden;
    flex-shrink: 0;

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

    &--placeholder {
      background: #f5f5f5;
      display: flex;
      align-items: center;
      justify-content: center;
      border: none;
    }
  }

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

  &__dates {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    flex: 1 1 33%;
  }

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

  &__date-label {
    font-family: var(--font-family-fa);
    font-weight: 300;
    font-size: 0.75rem;
    color: #848484;
    margin-inline-end: 0.25rem;
  }

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

  &__status {
    flex: 0 0 auto;
  }

  &__status-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.25rem 1rem;
    border-radius: 0.875rem;
    background: rgba(204, 40, 49, 6%);
    color: var(--color-error);
    font-family: var(--font-family-fa);
    font-size: 0.7rem;
  }

  &__status-dot {
    width: 0.375rem;
    height: 0.375rem;
    border-radius: 9999px;
    background: currentcolor;
  }

  &__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>
