<template>
  <view class="collapsible-container">
    <!-- 标题栏 -->
    <view class="collapsible-header" @click="toggleCollapse">
      <view class="header-content">
        <text class="header-title">{{ title }}</text>
        
        <!-- 右侧额外内容插槽 -->
        <view class="header-extra">
          <slot name="extra"></slot>
        </view>
      </view>
      
      <!-- 折叠图标 -->
      <uni-icons 
        :type="isCollapsed ? 'right' : 'down'" 
        size="20" 
        color="#666"
        class="collapse-icon"
        :animation="true"
      ></uni-icons>
    </view>
    
    <!-- 内容区域（带过渡动画） -->
    <view 
      class="collapsible-content"
      :style="{ 
        maxHeight: isCollapsed ? '0' : contentHeight,
        opacity: isCollapsed ? 0 : 1,
        padding: isCollapsed ? '0 16rpx' : '16rpx'
      }"
    >
      <slot></slot>
    </view>
  </view>
</template>

<script>
export default {
  name: 'CollapsibleContainer',
  props: {
    // 容器标题
    title: {
      type: String,
      required: true,
      default: '折叠容器'
    },
    // 初始是否折叠
    initCollapsed: {
      type: Boolean,
      default: false
    },
    // 内容区域最大高度（展开时）
    maxContentHeight: {
      type: String,
      default: '1000rpx'
    }
  },
  data() {
    return {
      // 是否折叠状态
      isCollapsed: this.initCollapsed,
      // 内容高度（用于动画）
      contentHeight: this.initCollapsed ? '0' : this.maxContentHeight
    };
  },
  watch: {
    // 监听初始折叠状态变化
    initCollapsed(newVal) {
      this.isCollapsed = newVal;
      this.contentHeight = newVal ? '0' : this.maxContentHeight;
    },
    // 监听最大高度变化
    maxContentHeight(newVal) {
      if (!this.isCollapsed) {
        this.contentHeight = newVal;
      }
    }
  },
  methods: {
    // 切换折叠状态
    toggleCollapse() {
      this.isCollapsed = !this.isCollapsed;
      this.contentHeight = this.isCollapsed ? '0' : this.maxContentHeight;
      // 触发状态变化事件
      this.$emit('collapse-change', this.isCollapsed);
    }
  }
};
</script>

<style scoped>
.collapsible-container {
  width: 100%;
  background-color: #ffffff;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  margin-bottom: 20rpx;
  overflow: hidden;
}

/* 标题栏样式 */
.collapsible-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 16rpx;
  background-color: #f9f9f9;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.header-title {
  font-size: 28rpx;
  color: #333333;
  font-weight: 500;
}

.header-extra {
  display: flex;
  align-items: center;
}

/* 折叠图标 */
.collapse-icon {
  transition: transform 0.3s ease;
  margin-left: 10rpx;
}

/* 内容区域样式 */
.collapsible-content {
  width: 100%;
  overflow: hidden;
  transition: all 0.3s ease;
  box-sizing: border-box;
}
</style>
