<template>
  <view class="wheel-selector">
    <image class="wheel-bg" :src="bgImg" mode="aspectFit" />
    <view
      class="wheel-options"
      @touchstart="onTouchStart"
      @touchmove.prevent="onTouchMove"
      @touchend="onTouchEnd"
    >
      <view
        v-for="opt in visibleOptions"
        :key="opt.idx + '-' + opt.pos" 
        class="wheel-option"
        :class="{ selected: opt.pos === 0 }"
        :style="getOptionStyle(opt)"
      >
        <image
          class="wheel-icon"
          :class="{ 'wheel-icon-selected': opt.pos === 0 }"
          :src="opt.pos === 0 ? iconSelected : iconNormal"
          mode="aspectFit"
        />
        <text class="wheel-label">{{ opt.item.label }}</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, watch, computed } from "vue";

const props = defineProps({
  options: { type: Array, required: true }, // [{label: '七月龄'}, ...]
  selectedIndex: { type: Number, default: 0 },
  bgImg: { type: String, required: true },
  iconNormal: { type: String, required: true },
  iconSelected: { type: String, required: true },
});
const emits = defineEmits(["update:selectedIndex", "change"]);

const currentIndex = ref(props.selectedIndex);

// 计算最多展示三个 option
const visibleOptions = computed(() => {
  const total = props.options.length;
  const arr = [];
  for (let i = 0; i < 3; i++) {
    if (currentIndex.value + i < total) {
      arr.push({
        item: props.options[currentIndex.value + i],
        idx: currentIndex.value + i,
        pos: i,
      });
    }
  }
  return arr;
});

watch(
  () => props.selectedIndex,
  (val) => {
    currentIndex.value = val;
  }
);

let startY = 0;


function onTouchStart(e) {
  startY = e.touches[0].clientY;
}
function onTouchMove(e) {
  const deltaY = e.touches[0].clientY - startY;
  if (Math.abs(deltaY) > 30) {
    // 滑动阈值
    if (deltaY > 0 && currentIndex.value > 0) {
      currentIndex.value -= 1;
      startY = e.touches[0].clientY;
    } else if (
      deltaY < 0 &&
      currentIndex.value < props.options.length - 1
    ) {
      currentIndex.value += 1;
      startY = e.touches[0].clientY;
    }
    emits("update:selectedIndex", currentIndex.value);
    emits("change", currentIndex.value);
  }
}
function onTouchEnd() {
  // nothing
}

function getOptionStyle(opt) {
  const positions = [
    { x: 34, y: 94 },
    { x: 118, y: 278 },
    { x: 50, y: 446 },
  ];
  const pos = positions[opt.pos] || positions[positions.length - 1];
  const _oo =  {
    position: "absolute",
    left: `${pos.x}rpx`,
    top: `${pos.y}rpx`,
    opacity: 1,
    zIndex: 10 - opt.pos,
  }
  return _oo;
}
</script>

<style scoped lang="less">
.wheel-selector {
  position: absolute;
  top: 128rpx;
  left: 0;
  width: 340rpx;
  height: 540rpx;
}
.wheel-bg {
  position: absolute;
  left: 0;
  top: 94rpx;
  width: 143rpx;
  height: 436rpx;
  z-index: 0;
  display: block;
}
.wheel-options {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  pointer-events: auto;
  touch-action: none;
}
.wheel-option {
  position: absolute;
  width: 220rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  transition: all 0.7s, opacity 0.2s;
  .wheel-icon {
    width: 24rpx;
    height: 24rpx;
    display: block;
  }
  .wheel-icon-selected {
    width: 56rpx;
    height: 56rpx;
    display: block;
  }
  .wheel-label {
    font-size: 28rpx;
    color: #FFFFFF;
    text-shadow: 0 2rpx 8rpx #333333;
    font-weight: bold;
  }
}
</style> 