<template>
  <transition name="toast-fade">
    <div v-if="visible" class="mobile-toast" :class="toastClass">
      <div class="toast-icon" v-if="showIcon">
        <i :class="iconClass"></i>
      </div>
      <div class="toast-message">{{ message }}</div>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'MobileToast',
  data() {
    return {
      visible: false,
      message: '',
      type: 'info', // info, success, error, warning
      duration: 3000,
      timer: null
    }
  },
  
  computed: {
    /**
     * 获取Toast样式类
     * @returns {Object} 样式类对象
     */
    toastClass() {
      return {
        [`toast-${this.type}`]: true
      }
    },
    
    /**
     * 是否显示图标
     * @returns {boolean} 是否显示图标
     */
    showIcon() {
      return this.type !== 'info'
    },
    
    /**
     * 获取图标类名
     * @returns {string} 图标类名
     */
    iconClass() {
      const iconMap = {
        success: 'el-icon-success',
        error: 'el-icon-error',
        warning: 'el-icon-warning'
      }
      return iconMap[this.type] || ''
    }
  },
  
  methods: {
    /**
     * 显示Toast提示
     * @param {Object} options 配置选项
     */
    show(options = {}) {
      const { message = '', type = 'info', duration = 3000 } = options
      
      this.message = message
      this.type = type
      this.duration = duration
      this.visible = true
      
      // 清除之前的定时器
      if (this.timer) {
        clearTimeout(this.timer)
      }
      
      // 设置自动隐藏
      this.timer = setTimeout(() => {
        this.hide()
      }, this.duration)
    },
    
    /**
     * 隐藏Toast提示
     */
    hide() {
      this.visible = false
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
    }
  },
  
  beforeDestroy() {
    // 组件销毁前清理定时器
    if (this.timer) {
      clearTimeout(this.timer)
    }
  }
}
</script>

<style lang="scss" scoped>
.mobile-toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 9999;
  max-width: 280px;
  min-width: 120px;
  padding: 16px 20px;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
  text-align: center;
  word-wrap: break-word;
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.toast-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.toast-message {
  flex: 1;
}

// 不同类型的样式
.toast-success {
  background: rgba(76, 175, 80, 0.9);
}

.toast-error {
  background: rgba(244, 67, 54, 0.9);
}

.toast-warning {
  background: rgba(255, 152, 0, 0.9);
}

.toast-info {
  background: rgba(0, 0, 0, 0.8);
}

// 过渡动画
.toast-fade-enter-active,
.toast-fade-leave-active {
  transition: all 0.3s ease;
}

.toast-fade-enter-from {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.8);
}

.toast-fade-leave-to {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.8);
}

// 响应式适配
@media (max-width: 480px) {
  .mobile-toast {
    max-width: calc(100vw - 40px);
  }
}
</style> 