<template>
  <view class="step-tab">
    <view class="step-container">
      <view class="step-item" :class="getStepClass(0)">
        <view class="step-icon">
          <image
            v-if="mappedStep === 0"
            src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/55a51333-318f-4ae4-8402-aca265bd499d"
            class="step-img active-circle"
            mode="aspectFit"
          />
          <view v-else-if="mappedStep > 0" class="step-done">1</view>
          <view v-else class="step-number">1</view>
        </view>
        <text class="step-text" :class="{ active: mappedStep === 0 }">入驻协议</text>
      </view>

      <view class="line-segment line-1">
        <view class="line-progress" :class="{ active: line1Active, deactivating: line1Deactivating }"></view>
      </view>

      <view class="step-item" :class="getStepClass(1)">
        <view class="step-icon">
          <image
            v-if="mappedStep === 1"
            src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/55a51333-318f-4ae4-8402-aca265bd499d"
            class="step-img active-circle"
            mode="aspectFit"
          />
          <view v-else-if="mappedStep > 1" class="step-done">2</view>
          <view v-else class="step-number">2</view>
        </view>
        <text class="step-text" :class="{ active: mappedStep === 1 }">基本信息</text>
      </view>

      <view class="line-segment line-2">
        <view class="line-progress" :class="{ active: line2Active, deactivating: line2Deactivating }"></view>
      </view>

      <view class="step-item" :class="getStepClass(2)">
        <view class="step-icon">
          <image
            v-if="mappedStep === 2"
            src="https://mrrplus.oss-cn-beijing.aliyuncs.com/photo/system/55a51333-318f-4ae4-8402-aca265bd499d"
            class="step-img active-circle"
            mode="aspectFit"
          />
          <view v-else-if="mappedStep > 2" class="step-done">3</view>
          <view v-else class="step-number">3</view>
        </view>
        <text class="step-text" :class="{ active: mappedStep === 2 }">资质认证</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "StepTab",
  props: {
    mappedStep: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      prevStep: 0,
      line1Active: false,
      line2Active: false,
      line1Deactivating: false,
      line2Deactivating: false
    };
  },
  watch: {
    mappedStep(newVal, oldVal) {
      this.prevStep = oldVal;
      if (newVal > oldVal) {
        if (newVal >= 1) {
          this.line1Active = true;
          this.line1Deactivating = false;
        }
        if (newVal >= 2) {
          this.line2Active = true;
          this.line2Deactivating = false;
        }
      } else if (newVal < oldVal) {
        if (newVal < 2) {
          this.line2Deactivating = true;
          setTimeout(() => {
            this.line2Active = false;
            this.line2Deactivating = false;
          }, 300);
        }
        if (newVal < 1) {
          this.line1Deactivating = true;
          setTimeout(() => {
            this.line1Active = false;
            this.line1Deactivating = false;
          }, 300);
        }
      }
    }
  },
  mounted() {
    this.line1Active = this.mappedStep >= 1;
    this.line2Active = this.mappedStep >= 2;
  },
  methods: {
    getStepClass(stepIndex) {
      if (stepIndex === this.mappedStep) {
        return 'active';
      }
      else if (stepIndex < this.mappedStep) {
        return 'completed';
      }
      return '';
    }
  }
}
</script>

<style scoped>
.step-tab {
  height: 98rpx;
  position: relative;
  padding: 31rpx 0;
  background-color: #fff;
}

.step-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5rpx 60rpx;
  position: relative;
  z-index: 2;
}

.step-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  z-index: 2;
  transition: all 0.3s ease;
  flex: 1;
}

.step-icon {
  width: 50rpx;
  height: 50rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 14rpx;
}

.step-img.active-circle {
  width: 50rpx;
  height: 50rpx;
}

.step-number {
  width: 50rpx;
  height: 50rpx;
  border-radius: 50%;
  background-color: #F4F6FA;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 28rpx;
  color: #999999;
  font-weight: 500;
  transition: all 0.3s ease;
}

.step-done {
  width: 50rpx;
  height: 50rpx;
  border-radius: 50%;
  background-color: #FAD0D3;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 28rpx;
  color: #E8101E;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: inset 0 0 4rpx rgba(232, 16, 30, 0.2);
}

.step-text {
  font-size: 24rpx;
  color: #999;
  font-weight: 400;
  transition: all 0.3s ease;
}

.step-text.active {
  color: #333333;
  font-weight: 500;
}

.step-item.completed .step-text {
  color: #333333;
  font-weight: 500;
}

.line-segment {
  position: relative;
  height: 8rpx;
  background-color: #F4F6FA;
  border-radius: 4rpx;
  top: -25rpx;
  overflow: hidden;
  width: 126rpx;
  transition: all 0.3s ease;
}

.line-1 {
  margin-left: 20rpx;
  margin-right: 20rpx;
}

.line-2 {
  margin-left: 20rpx;
  margin-right: 20rpx;
}

.line-progress {
  height: 100%;
  width: 0%;
  background-color: #F4F6FA;
  border-radius: 4rpx;
  transition: width 0.3s ease;
  transform-origin: left center;
}

.line-progress.active {
  width: 100%;
  background-color: #E8101E;
  transition: width 0.3s ease;
}

.line-progress.deactivating {
  width: 0%;
  background-color: #E8101E;
  transition: width 0.3s ease;
  transform-origin: right center;
}
</style>
