<template>
  <view class="capsule-tab-container">
    <view v-for="(item, index) in tabList" :key="index" class="capsule-tab-item" :class="{
      'capsule-tab-active': activeIndex == item.value,
      'capsule-tab-first': index == 0,
      'capsule-tab-last': index == tabList.length - 1
    }" @click="handleTabClick(index, item)">
      <text class="capsule-tab-text">{{ item.label || item.text || item.name }}</text>
    </view>
  </view>
</template>

<script>
export default {
  name: 'SwitchTab',
  props: {
    // tab数据数组
    tabList: {
      type: Array,
      default: () => [],
      required: true
    },
    // 默认激活的索引
    defaultActive: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      activeIndex: this.defaultActive
    }
  },
  watch: {
    defaultActive: {
      handler(newVal) {

        this.activeIndex = newVal
      },
      immediate: true
    }
  },
  methods: {
    // 处理tab点击事件
    handleTabClick(index, item) {
      if (this.activeIndex === item.value) {
        return
      }

      this.activeIndex = item.value;
      
      // 向外传递被点击的对象
      this.$emit('tabChange', {
        index,
        item,
        value: item.value || item.id || index
      })
    },

    // 外部调用的方法：切换tab
    switchTab(index) {
      if (index >= 0 && index < this.tabList.length && index !== this.activeIndex) {
        this.activeIndex = index
        const item = this.tabList[index]
        this.$emit('tabChange', {
          index,
          item,
          value: item.value || item.id || index
        })
      }
    },

    // 外部调用的方法：获取当前激活的tab
    getCurrentTab() {
      return {
        index: this.activeIndex,
        item: this.tabList[this.activeIndex],
        value: this.tabList[this.activeIndex]?.value || this.tabList[this.activeIndex]?.id || this.activeIndex
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.capsule-tab-container {
  display: inline-flex;
  background: #f5f5f5;
  border-radius: 32rpx;
  padding: 2rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.capsule-tab-item {
  position: relative;
  padding: 6rpx 12rpx;
  border-radius: 30rpx;
  cursor: pointer;
  transition: all 0.3s ease;
  user-select: none;

  // 第一个和最后一个的特殊圆角处理
  //   &.capsule-tab-first {
  //     border-top-right-radius: 0;
  //     border-bottom-right-radius: 0;
  //   }

  //   &.capsule-tab-last {
  //     border-top-left-radius: 0;
  //     border-bottom-left-radius: 0;
  //   }

  // 非激活状态
  &:not(.capsule-tab-active) {
    background: transparent;

    .capsule-tab-text {
      color: #666;
      font-weight: normal;
    }


  }

  // 激活状态
  &.capsule-tab-active {
    background: #5896FC;
    box-shadow: 0 2rpx 8rpx rgba(24, 144, 255, 0.3);

    .capsule-tab-text {
      color: #ffffff;
      font-weight: bold;
    }
  }
}

.capsule-tab-text {
  font-size: 24rpx;
  line-height: 1;
  transition: all 0.3s ease;
}

// 响应式适配
@media (max-width: 768px) {
  .capsule-tab-container {
    width: auto;
    justify-content: center;
  }

  .capsule-tab-item {
    flex: none;
    text-align: center;
    padding: 8rpx 18rpx;

    // &.capsule-tab-first {
    //   border-radius: 30rpx 0 0 30rpx;
    // }

    // &.capsule-tab-last {
    //   border-radius: 0 30rpx 30rpx 0;
    // }
  }
}
</style>