<template>
  <!-- 用强大的功能，开启盈利快车道 - Figma 1455:1490 -->
  <view class="feature-section">
    <text class="section-title">用强大的功能，开启盈利快车道</text>

    <!-- 功能卡片横向滚动
         受控模式：active 由父组件统一管理；scroll-left 跟随 active -->
    <scroll-view
      scroll-x
      class="feat-scroll"
      show-scrollbar="false"
      :scroll-left="featScrollLeft"
      :scroll-with-animation="true"
    >
      <view class="feat-list">
        <view
          class="feat-card"
          :class="{ active: active === index }"
          v-for="(item, index) in features"
          :key="index"
          @tap="onFeatTap(index)"
        >
          <text class="feat-num">{{ item.step }}</text>
          <view class="feat-text">
            <text class="feat-title">{{ item.title }}</text>
            <text class="feat-desc">{{ item.desc }}</text>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 集成平台大图 - 370x257 8px圆角
         跟随 active 联动切换 -->
    <view class="integration-wrap">
      <image
        class="integration-img"
        :src="integrationImg"
        mode="widthFix"
      ></image>
    </view>
  </view>
</template>

<script>
// 卡片尺寸常量，用于计算 scroll-left（与 CSS 保持一致）
// CSS: .feat-card { width:332rpx }, .feat-list gap:16rpx
// 1rpx = 0.5px → 单卡步长 = (332 + 16) * 0.5 = 174px
const CARD_STEP_PX = (332 + 16) * 0.5  // 174

export default {
  name: 'PlatformFeatures',
  props: {
    features: {
      type: Array,
      default: () => []
    },
    // 受控 active：由父组件（index.vue 的统一轮播定时器）传入
    active: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      featScrollLeft: 0
    }
  },
  computed: {
    integrationImg() {
      const f = this.features[this.active]
      return (f && f.img) || ''
    }
  },
  watch: {
    active(idx) {
      // active 变化（无论是自动轮播还是手动点击）都把选中卡片尽量滚到左边
      this.scrollToCard(idx)
    }
  },
  methods: {
    onFeatTap(index) {
      // 子组件不再自管 state，只 emit 给父组件
      this.$emit('change', index)
    },

    /**
     * 把第 idx 张卡片「尽量」滚到左边贴边。
     * scroll-view 的 scroll-left 超过最大可滚值时会自动截断，
     * 因此最后几张卡片虽无法真正贴左，但已经是「最接近左侧」的位置。
     */
    scrollToCard(idx) {
      const target = Math.max(0, idx) * CARD_STEP_PX
      // 处理 0→0 无变化不触发滚动的情况
      if (this.featScrollLeft === target) {
        this.featScrollLeft = target + 0.001
        this.$nextTick(() => {
          this.featScrollLeft = target
        })
      } else {
        this.featScrollLeft = target
      }
    }
  }
}
</script>

<style scoped>
.feature-section {
  width: 100%;
  padding: 96rpx 0 0 0;
  background: #ffffff;
  box-sizing: border-box;
}

/* 区块标题 - 20px Heavy #1A1A1A 居中 */
.section-title {
  display: block;
  text-align: center;
  font-size: 40rpx;
  font-weight: 900;
  color: #1a1a1a;
  line-height: 1.45;
  margin-bottom: 64rpx;
}

/* 功能卡片横向滚动 */
.feat-scroll {
  width: 100%;
  white-space: nowrap;
  padding: 0 20rpx;
  box-sizing: border-box;
}
.feat-list {
  display: inline-flex;
  gap: 16rpx;
}
/* 卡片 - 166x80 圆角6px (332rpx × 160rpx) */
.feat-card {
  flex-shrink: 0;
  width: 332rpx;
  height: 160rpx;
  padding: 20rpx;
  background: #f2f2f2;
  border-radius: 12rpx;
  box-sizing: border-box;
  display: flex;
  align-items: flex-start;
  gap: 16rpx;
}
.feat-card.active {
  background: #ff6800;
}
.feat-num {
  font-size: 32rpx;
  font-weight: 900;
  color: #ff6800;
  line-height: 52rpx;
}
.feat-card.active .feat-num {
  color: #ffffff;
}
.feat-text {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.feat-title {
  font-size: 32rpx;
  font-weight: 500;
  color: #000000;
  line-height: 44rpx;
}
.feat-card.active .feat-title {
  color: #ffffff;
}
.feat-desc {
  margin-top: 6rpx;
  font-size: 20rpx;
  color: #000000;
  opacity: 0.6;
  line-height: 1.45;
  white-space: normal;
}
.feat-card.active .feat-desc {
  color: #ffffff;
  opacity: 1;
}

/* 集成平台大图 - 370x257 8px圆角 */
.integration-wrap {
  margin-top: 20rpx;
  padding: 0 18rpx;
  box-sizing: border-box;
}
.integration-img {
  width: 100%;
  border-radius: 16rpx;
  display: block;
}
</style>
