<template>
  <view class="tabs-container" :class="containerClass">
    <!-- 标签栏 -->
    <scroll-view 
      class="tabs-scroll" 
      :class="tabsClass"
      scroll-x 
      :scroll-left="scrollLeft"
      :show-scrollbar="false"
    >
      <view class="tabs-wrapper">
        <view 
          class="tab-item" 
          :class="getTabClass(index)"
          v-for="(tab, index) in tabs" 
          :key="index"
          @click="handleTabClick(index)"
        >
          <!-- 图标 -->
          <view class="tab-icon" v-if="tab.icon">
            <text class="icon">{{ tab.icon }}</text>
          </view>
          
          <!-- 标题 -->
          <view class="tab-title">
            <text class="title-text">{{ tab.title || tab.name || tab.label }}</text>
            <!-- 徽标 -->
            <view class="tab-badge" v-if="tab.badge">
              <text class="badge-text">{{ tab.badge }}</text>
            </view>
          </view>
          
          <!-- 下划线 -->
          <view class="tab-line" v-if="showLine && index === activeIndex"></view>
        </view>
      </view>
    </scroll-view>
    
    <!-- 内容区域 -->
    <view class="tabs-content" v-if="showContent">
      <slot></slot>
    </view>
  </view>
</template>

<script>
export default {
  name: 'Tabs',
  props: {
    // 标签数据
    tabs: {
      type: Array,
      default: () => [],
      required: true
    },
    // 当前激活的标签索引
    activeIndex: {
      type: Number,
      default: 0
    },
    // 标签类型
    type: {
      type: String,
      default: 'line', // line, card, button
      validator: (value) => ['line', 'card', 'button'].includes(value)
    },
    // 标签大小
    size: {
      type: String,
      default: 'normal', // large, normal, small
      validator: (value) => ['large', 'normal', 'small'].includes(value)
    },
    // 是否显示下划线
    showLine: {
      type: Boolean,
      default: true
    },
    // 是否显示内容区域
    showContent: {
      type: Boolean,
      default: true
    },
    // 是否可滚动
    scrollable: {
      type: Boolean,
      default: false
    },
    // 标签宽度（固定宽度时使用）
    tabWidth: {
      type: String,
      default: ''
    },
    // 激活颜色
    activeColor: {
      type: String,
      default: '#2e8b57'
    },
    // 非激活颜色
    inactiveColor: {
      type: String,
      default: '#666'
    },
    // 背景色
    backgroundColor: {
      type: String,
      default: '#fff'
    },
    // 自定义样式类
    customClass: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      scrollLeft: 0
    }
  },
  computed: {
    containerClass() {
      return [
        this.customClass,
        `tabs-${this.type}`,
        `tabs-${this.size}`
      ]
    },
    tabsClass() {
      return {
        'tabs-scrollable': this.scrollable,
        'tabs-fixed': !this.scrollable
      }
    }
  },
  watch: {
    activeIndex: {
      handler(newIndex) {
        this.scrollToActiveTab(newIndex)
      },
      immediate: true
    }
  },
  methods: {
    // 获取标签样式类
    getTabClass(index) {
      return {
        'tab-active': index === this.activeIndex,
        'tab-inactive': index !== this.activeIndex
      }
    },
    // 标签点击事件
    handleTabClick(index) {
      if (index === this.activeIndex) return
      this.$emit('change', index)
      this.$emit('click', { index, tab: this.tabs[index] })
    },
    // 滚动到激活的标签
    scrollToActiveTab(index) {
      if (!this.scrollable) return
      
      this.$nextTick(() => {
        const query = uni.createSelectorQuery().in(this)
        query.selectAll('.tab-item').boundingClientRect((rects) => {
          if (rects && rects[index]) {
            const tabRect = rects[index]
            const containerWidth = uni.getSystemInfoSync().windowWidth
            const scrollLeft = tabRect.left - containerWidth / 2 + tabRect.width / 2
            this.scrollLeft = Math.max(0, scrollLeft)
          }
        }).exec()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tabs-container {
  width: 100%;
  background-color: #fff;
  
  // 类型样式
  &.tabs-line {
    .tab-item {
      border-bottom: 4rpx solid transparent;
      
      &.tab-active {
        border-bottom-color: #2e8b57;
      }
    }
  }
  
  &.tabs-card {
    .tabs-wrapper {
      background-color: #f5f5f5;
      border-radius: 12rpx;
      padding: 8rpx;
    }
    
    .tab-item {
      background-color: transparent;
      border-radius: 8rpx;
      
      &.tab-active {
        background-color: #fff;
        box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
      }
    }
  }
  
  &.tabs-button {
    .tab-item {
      border: 2rpx solid #e5e5e5;
      border-radius: 8rpx;
      margin-right: 16rpx;
      
      &:last-child {
        margin-right: 0;
      }
      
      &.tab-active {
        border-color: #2e8b57;
        background-color: #2e8b57;
        
        .title-text {
          color: #fff;
        }
      }
    }
  }
  
  // 大小样式
  &.tabs-large {
    .tab-item {
      height: 96rpx;
      padding: 0 32rpx;
    }
    
    .title-text {
      font-size: 32rpx;
    }
  }
  
  &.tabs-normal {
    .tab-item {
      height: 80rpx;
      padding: 0 24rpx;
    }
    
    .title-text {
      font-size: 28rpx;
    }
  }
  
  &.tabs-small {
    .tab-item {
      height: 64rpx;
      padding: 0 20rpx;
    }
    
    .title-text {
      font-size: 24rpx;
    }
  }
}

.tabs-scroll {
  width: 100%;
  white-space: nowrap;
  
  &.tabs-scrollable {
    .tabs-wrapper {
      display: inline-flex;
      min-width: 100%;
    }
  }
  
  &.tabs-fixed {
    .tabs-wrapper {
      display: flex;
      width: 100%;
    }
    
    .tab-item {
      flex: 1;
    }
  }
}

.tabs-wrapper {
  display: flex;
  align-items: center;
}

.tab-item {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: all 0.3s ease;
  white-space: nowrap;
  
  &.tab-active {
    .title-text {
      color: #2e8b57;
      font-weight: 600;
    }
  }
  
  &.tab-inactive {
    .title-text {
      color: #666;
    }
  }
}

.tab-icon {
  margin-right: 8rpx;
  
  .icon {
    font-size: 32rpx;
  }
}

.tab-title {
  display: flex;
  align-items: center;
  position: relative;
}

.title-text {
  transition: all 0.3s ease;
}

.tab-badge {
  position: absolute;
  top: -16rpx;
  right: -24rpx;
  min-width: 32rpx;
  height: 32rpx;
  background-color: #ff4757;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  
  .badge-text {
    font-size: 20rpx;
    color: #fff;
    line-height: 1;
    padding: 0 8rpx;
  }
}

.tab-line {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60rpx;
  height: 4rpx;
  background-color: #2e8b57;
  border-radius: 2rpx;
}

.tabs-content {
  flex: 1;
  overflow: hidden;
}
</style>