<template>
  <div class="mreq" @click="emit('show-details', request)">
    <div class="mreq__heading">
      <span class="mreq__code">{{ request.code }}</span>
      <div class="mreq__title-wrap">
        <p class="mreq__title">{{ request.title }}</p>
        <p class="mreq__subtitle">شماره درخواست : {{ request.requestNumber }}</p>
      </div>
    </div>

    <div class="mreq__meta">
      <div class="mreq__pill mreq__pill--date">
        <span class="mreq__pill-label">تاریخ درخواست :</span>
        <span class="mreq__pill-value">{{ request.requestDate }}</span>
      </div>
      <div class="mreq__pill mreq__pill--name">
        <span class="mreq__pill-label">نام درخواست کننده :</span>
        <span class="mreq__pill-value">{{ request.requesterName }}</span>
      </div>
      <div class="mreq__pill mreq__pill--status" :class="`mreq__pill--${tone}`">
        <span class="mreq__dot" />
        <span class="mreq__pill-value mreq__pill-value--status">{{ request.statusLabel }}</span>
      </div>
    </div>

    <div class="mreq__actions" @click.stop>
      <CircleButton
        v-if="canAccept"
        tooltip="پذیرش درخواست"
        bg-color="rgba(0, 112, 116, 0.08)"
        @click="emit('change-status', { id: request.id, status: 'accepted' })"
      >
        <template #icon>
          <SvgIcon name="check" :size="18" color="#007074" />
        </template>
      </CircleButton>
      <CircleButton
        v-if="canReject"
        tooltip="رد درخواست"
        bg-color="rgba(243, 102, 117, 0.06)"
        @click="emit('change-status', { id: request.id, status: 'rejected' })"
      >
        <template #icon>
          <SvgIcon name="close" :size="18" color="var(--color-error)" />
        </template>
      </CircleButton>
      <BaseButton
        text="جزئیات درخواست"
        custom-class="mreq__btn"
        @click="emit('show-details', request)"
      >
        <template #appendIcon>
          <SvgIcon name="arrow-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 CircleButton from '@/components/CircleButton.vue'

const TONE_MAP = {
  pending: 'warning',
  seen: 'neutral',
  accepted: 'success',
  rejected: 'danger',
}

const props = defineProps({
  request: { type: Object, required: true },
  alwaysActionable: { type: Boolean, default: false },
})

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

const tone = computed(() => TONE_MAP[props.request.status] || 'neutral')

const canAct = computed(() => props.alwaysActionable || props.request.status === 'pending')
const canAccept = computed(() => canAct.value && props.request.status !== 'accepted')
const canReject = computed(() => canAct.value && props.request.status !== 'rejected')
</script>

<style lang="scss" scoped>
.mreq {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  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;
  cursor: pointer;
  transition: background 0.15s ease;

  &:hover {
    background: rgba(255, 255, 255, 75%);
  }

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

  &__heading {
    display: flex;
    align-items: center;
    gap: 0.625rem;

    @media (min-width: 1024px) {
      min-width: 9rem;
    }
  }

  &__title-wrap {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: end;
    gap: 0.125rem;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-weight: 400;
    font-size: 0.95rem;
    color: #4b4b4b;
    margin: 0;
  }

  &__subtitle {
    font-family: var(--font-family-fa);
    font-weight: 400;
    font-size: 0.7rem;
    color: #4b4b4b;
    margin: 0;
  }

  &__code {
    font-family: var(--font-family-en);
    font-weight: 800;
    font-size: 2.75rem;
    line-height: 1;
    color: #f0f0f0;
  }

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

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

  &__pill {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.3rem 0.875rem;
    border-radius: 0.75rem;
    background: rgba(107, 107, 107, 4%);
    font-family: var(--font-family-fa);
    white-space: nowrap;
  }

  &__pill-label {
    font-weight: 400;
    font-size: 0.65rem;
    color: #535353;
  }

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

  &__pill-value--status {
    font-family: var(--font-family-fa);
    font-weight: 400;
    font-size: 0.65rem;
  }

  &__dot {
    width: 0.32rem;
    height: 0.32rem;
    border-radius: 9999px;
    flex-shrink: 0;
  }

  &__pill--status {
    padding: 0.3rem 0.75rem;
  }

  &__pill--danger {
    background: rgba(243, 102, 117, 6%);

    .mreq__pill-value {
      color: #cc2831;
    }

    .mreq__dot {
      background: #cc2831;
    }
  }

  &__pill--success {
    background: rgba(0, 112, 116, 6%);

    .mreq__pill-value {
      color: #007074;
    }

    .mreq__dot {
      background: #007074;
    }
  }

  &__pill--warning {
    background: rgba(182, 132, 45, 7%);

    .mreq__pill-value {
      color: #b6842d;
    }

    .mreq__dot {
      background: #b6842d;
    }
  }

  &__pill--neutral {
    background: rgba(107, 107, 107, 6%);

    .mreq__pill-value {
      color: #535353;
    }

    .mreq__dot {
      background: #989898;
    }
  }

  &__actions {
    display: flex;
    justify-content: center;
    gap: 6px;

    @media (min-width: 1024px) {
      justify-content: flex-start;
    }
  }

  &__btn {
    min-width: 8rem;
    height: 2.5rem;
    padding: 0 1.25rem;
  }
}
</style>
