<!--通用底部弹窗组件-->
<template>
  <view 
    class="bottom-popup" 
    :class="{ 'show': visible }" 
    v-if="visible"
    @touchmove.stop.prevent
  >
    <!-- 遮罩层 -->
    <view 
      class="mask" 
      :class="{ 'show': visible }"
      @click.stop="handleMaskClick"
    ></view>
    
    <!-- 弹窗内容 -->
    <view class="popup-content" :class="{ 'show': visible }" @click.stop="stopPropagation">
      <!-- 顶部拖拽条 -->
      <view class="drag-handle">
        <view class="drag-line"></view>
      </view>
      
      <!-- 标题栏 -->
      <view class="popup-header" v-if="title">
        <view class="popup-title">{{ title }}</view>
        <view class="close-btn" @click.stop="close">
          <text class="close-icon">×</text>
        </view>
      </view>
      
      <!-- 滚动内容区域 -->
      <scroll-view 
        class="popup-body"
        :class="{ 'has-title': title, 'has-footer': hasFooter }"
        :scroll-y="true"
        :enable-passive="true"
        @click.stop="stopPropagation"
      >
        <slot></slot>
      </scroll-view>
      
      <!-- 底部操作按钮区域 -->
      <view class="popup-footer" v-if="hasFooter" @click.stop="stopPropagation">
        <slot name="footer"></slot>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'BottomPopup',
  props: {
    // 是否显示弹窗
    visible: {
      type: Boolean,
      default: false
    },
    // 弹窗标题
    title: {
      type: String,
      default: ''
    },
    // 是否有底部操作区域
    hasFooter: {
      type: Boolean,
      default: false
    },
    // 点击遮罩是否关闭
    maskClosable: {
      type: Boolean,
      default: true
    },
    // 弹窗高度（vh单位）
    height: {
      type: [String, Number],
      default: 70
    }
  },
  
  methods: {
    // 处理遮罩点击
    handleMaskClick() {
      if (this.maskClosable) {
        console.log('遮罩点击，准备关闭弹窗')
        this.close()
      }
    },
    
    // 关闭弹窗
    close() {
      console.log('弹窗关闭事件触发')
      this.$emit('close')
    },
    
    // 阻止事件冒泡的处理方法
    stopPropagation(event) {
      event.stopPropagation()
    }
  }
}
</script>

<style lang="scss" scoped>
.bottom-popup {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1000;
  pointer-events: auto;
}

/* 微信小程序兼容性修复 */
/* #ifdef MP-WEIXIN */
.bottom-popup {
  position: fixed !important;
  z-index: 1000 !important;
}
/* #endif */

.mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: auto;
  
  &.show {
    opacity: 1;
  }
}

/* 微信小程序遮罩兼容性 */
/* #ifdef MP-WEIXIN */
.mask {
  pointer-events: auto !important;
}
/* #endif */

.popup-content {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: v-bind("`${height}vh`");
  background: #2C2C2C;
  border-radius: 24rpx 24rpx 0 0;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  display: flex;
  flex-direction: column;
  
  &.show {
    transform: translateY(0);
  }
}

.drag-handle {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 40rpx;
  padding: 20rpx 0;
  flex-shrink: 0;
}

.drag-line {
  width: 80rpx;
  height: 8rpx;
  background: #666;
  border-radius: 4rpx;
}

.popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40rpx 20rpx;
  border-bottom: 1px solid #404040;
  flex-shrink: 0;
}

.popup-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #ffffff;
}

.close-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  transition: all 0.2s ease;
  cursor: pointer;
  
  &:active {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(0.95);
  }
}

.close-icon {
  font-size: 40rpx;
  color: #ffffff;
  line-height: 1;
}

.popup-body {
  flex: 1;
  padding: 40rpx;
  overflow-y: auto;
  
  &.has-title {
    padding-top: 20rpx;
  }
  
  &.has-footer {
    padding-bottom: 20rpx;
  }
}

.popup-footer {
  padding: 20rpx 40rpx 40rpx;
  background: #2C2C2C;
  border-top: 1px solid #404040;
  flex-shrink: 0;
}

/* 滚动条样式 */
::-webkit-scrollbar {
  width: 6rpx;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #29D3B4;
  border-radius: 3rpx;
}
</style>