<template>
  <div class="course-item">
    <div class="course-item__main">
      <div v-if="course.image" class="course-item__image">
        <img :src="course.image" :alt="course.title" />
      </div>
      <div v-else class="course-item__image course-item__image--placeholder">
        <SvgIcon name="book" :size="22" color="#bcbcbc" />
      </div>
      <div class="course-item__title-block">
        <p class="course-item__title">
          <span>دوره</span>
          <strong>{{ course.title }}</strong>
        </p>
        <div class="course-item__teacher">
          <SvgIcon name="user" :size="11" color="#bcbcbc" />
          <span class="course-item__teacher-label">استاد:</span>
          <span class="course-item__teacher-name">{{ teacherName }}</span>
        </div>
      </div>
    </div>

    <div class="course-item__meta">
      <Badge
        variant="cyan"
        size="sm"
        icon="calendar"
        :label="`وضعیت دوره :`"
        :value="`در حال گذراندن`"
      />
    </div>

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

    <div class="course-item__actions">
      <CircleButton
        tooltip="حذف"
        bg-color="rgba(104, 104, 104, 0.05)"
        size="2.5rem"
        @click="emit('delete', course)"
      >
        <template #icon>
          <SvgIcon name="trash" :size="18" color="var(--color-error)" />
        </template>
      </CircleButton>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import Badge from '@/components/Badge.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import CircleButton from '@/components/CircleButton.vue'

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

const emit = defineEmits(['delete'])

const teacherName = computed(() => {
  const t = props.course.teacher
  if (!t) return '—'
  return t.name
})

const isActive = computed(() => props.course.isActive ?? false)
</script>

<style lang="scss" scoped>
.course-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, 90%);
  border-radius: 0.875rem;
  margin-bottom: 0.625rem;

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

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

  &__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-block {
    min-width: 0;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-size: 0.95rem;
    color: #4b4b4b;
    margin: 0;
    display: flex;
    gap: 0.25rem;

    strong {
      font-weight: 700;
    }
  }

  &__teacher {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    margin-top: 0.25rem;
  }

  &__teacher-label {
    font-family: var(--font-family-fa);
    font-size: 0.65rem;
    color: #838383;
  }

  &__teacher-name {
    font-family: var(--font-family-fa);
    font-size: 0.75rem;
    color: #4b4b4b;
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    flex: 1 1 33%;
    justify-content: flex-start;
  }

  &__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.75rem;
    color: #848484;
    margin-inline-end: 0.25rem;
  }

  &__pill-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: 1280px) {
      flex: 1 1 auto;
    }
  }

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