<template>
  <transition name="toast-fade">
    <div v-if="show" class="toast-container" :class="type">
      <div class="toast-content">
        <i v-if="type === 'success'" class="bi-check-circle-fill"></i>
        <i v-else-if="type === 'error'" class="bi-exclamation-circle-fill"></i>
        <i v-else-if="type === 'warning'" class="bi-exclamation-triangle-fill"></i>
        <i v-else class="bi-info-circle-fill"></i>
        <span>{{ message }}</span>
      </div>
    </div>
  </transition>
</template>

<script>
import { ref, onMounted, onBeforeUnmount } from 'vue';

export default {
  name: 'Toast',
  props: {
    message: {
      type: String,
      required: true
    },
    duration: {
      type: Number,
      default: 3000
    },
    type: {
      type: String,
      default: 'info',
      validator(value) {
        return ['info', 'success', 'warning', 'error'].includes(value);
      }
    }
  },
  emits: ['close'],
  setup(props, { emit }) {
    const show = ref(false);
    let timer = null;

    const startTimer = () => {
      timer = setTimeout(() => {
        show.value = false;
        setTimeout(() => emit('close'), 300); // Даем время для завершения анимации
      }, props.duration);
    };

    onMounted(() => {
      show.value = true;
      startTimer();
    });

    onBeforeUnmount(() => {
      clearTimeout(timer);
    });

    return {
      show
    };
  }
};
</script>

<style scoped>
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 12px 20px;
  border-radius: 8px;
  color: white;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
  z-index: 1000;
  display: flex;
  align-items: center;
  max-width: 350px;
}

.toast-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.toast-content i {
  font-size: 1.2rem;
}

.success {
  background-color: #4caf50;
}

.info {
  background-color: #2196f3;
}

.warning {
  background-color: #ff9800;
}

.error {
  background-color: #f44336;
}

/* Анимация появления и исчезновения */
.toast-fade-enter-active,
.toast-fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.toast-fade-enter-from,
.toast-fade-leave-to {
  opacity: 0;
  transform: translateY(20px);
}
</style>