<template>
  <view class="loading-container" v-if="show" :class="{ 'with-mask': mask }">
    <!-- 遮罩层 -->
    <view class="loading-mask" v-if="mask" @tap="onMaskTap"></view>
    
    <!-- 加载内容 -->
    <view class="loading-content" :class="[size, type]">
      <!-- 旋转加载器 -->
      <view class="loading-spinner" v-if="type === 'spinner'" :style="{ borderTopColor: color }">
      </view>
      
      <!-- 点状加载器 -->
      <view class="loading-dots" v-if="type === 'dots'">
        <view class="dot" :style="{ backgroundColor: color }"></view>
        <view class="dot" :style="{ backgroundColor: color }"></view>
        <view class="dot" :style="{ backgroundColor: color }"></view>
      </view>
      
      <!-- 脉冲加载器 -->
      <view class="loading-pulse" v-if="type === 'pulse'" :style="{ backgroundColor: color }">
      </view>
      
      <!-- 波浪加载器 -->
      <view class="loading-wave" v-if="type === 'wave'">
        <view class="wave-bar" :style="{ backgroundColor: color }"></view>
        <view class="wave-bar" :style="{ backgroundColor: color }"></view>
        <view class="wave-bar" :style="{ backgroundColor: color }"></view>
        <view class="wave-bar" :style="{ backgroundColor: color }"></view>
        <view class="wave-bar" :style="{ backgroundColor: color }"></view>
      </view>
      
      <!-- 加载文本 -->
      <text class="loading-text" v-if="text">{{ text }}</text>
    </view>
  </view>
</template>

<script>
export default {
  name: 'Loading',
  
  props: {
    // 是否显示
    show: {
      type: Boolean,
      default: false
    },
    // 加载文本
    text: {
      type: String,
      default: '加载中...'
    },
    // 加载类型：spinner, dots, pulse, wave
    type: {
      type: String,
      default: 'spinner',
      validator: (value) => ['spinner', 'dots', 'pulse', 'wave'].includes(value)
    },
    // 大小：small, medium, large
    size: {
      type: String,
      default: 'medium',
      validator: (value) => ['small', 'medium', 'large'].includes(value)
    },
    // 颜色
    color: {
      type: String,
      default: '#2E8B57'
    },
    // 是否显示遮罩
    mask: {
      type: Boolean,
      default: true
    },
    // 是否可以点击遮罩关闭
    maskClosable: {
      type: Boolean,
      default: false
    }
  },
  
  methods: {
    // 点击遮罩
    onMaskTap() {
      if (this.maskClosable) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.loading-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  
  &.with-mask {
    .loading-mask {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.5);
    }
  }
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16rpx;
  padding: 40rpx;
  backdrop-filter: blur(10rpx);
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
}

/* 旋转加载器 */
.loading-spinner {
  border: 4rpx solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.small .loading-spinner {
  width: 32rpx;
  height: 32rpx;
  border-width: 3rpx;
}

.medium .loading-spinner {
  width: 48rpx;
  height: 48rpx;
  border-width: 4rpx;
}

.large .loading-spinner {
  width: 64rpx;
  height: 64rpx;
  border-width: 5rpx;
}

/* 点状加载器 */
.loading-dots {
  display: flex;
  gap: 8rpx;
  
  .dot {
    border-radius: 50%;
    animation: pulse 1.4s ease-in-out infinite both;
    
    &:nth-child(1) { animation-delay: -0.32s; }
    &:nth-child(2) { animation-delay: -0.16s; }
    &:nth-child(3) { animation-delay: 0s; }
  }
}

.small .loading-dots .dot {
  width: 8rpx;
  height: 8rpx;
}

.medium .loading-dots .dot {
  width: 12rpx;
  height: 12rpx;
}

.large .loading-dots .dot {
  width: 16rpx;
  height: 16rpx;
}

/* 脉冲加载器 */
.loading-pulse {
  border-radius: 50%;
  animation: pulse-scale 1s ease-in-out infinite;
}

.small .loading-pulse {
  width: 32rpx;
  height: 32rpx;
}

.medium .loading-pulse {
  width: 48rpx;
  height: 48rpx;
}

.large .loading-pulse {
  width: 64rpx;
  height: 64rpx;
}

/* 波浪加载器 */
.loading-wave {
  display: flex;
  align-items: flex-end;
  gap: 4rpx;
  
  .wave-bar {
    border-radius: 2rpx;
    animation: wave 1.2s ease-in-out infinite;
    
    &:nth-child(1) { animation-delay: -1.2s; }
    &:nth-child(2) { animation-delay: -1.1s; }
    &:nth-child(3) { animation-delay: -1.0s; }
    &:nth-child(4) { animation-delay: -0.9s; }
    &:nth-child(5) { animation-delay: -0.8s; }
  }
}

.small .loading-wave .wave-bar {
  width: 4rpx;
  height: 20rpx;
}

.medium .loading-wave .wave-bar {
  width: 6rpx;
  height: 30rpx;
}

.large .loading-wave .wave-bar {
  width: 8rpx;
  height: 40rpx;
}

/* 加载文本 */
.loading-text {
  margin-top: 24rpx;
  font-size: 28rpx;
  color: #666;
  text-align: center;
}

/* 动画 */
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes pulse {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 1;
  }
  40% {
    transform: scale(1);
    opacity: 0.5;
  }
}

@keyframes pulse-scale {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.7;
  }
}

@keyframes wave {
  0%, 40%, 100% {
    transform: scaleY(0.4);
  }
  20% {
    transform: scaleY(1);
  }
}
</style>