<template>
  <div class="carousel" @mouseleave="startAutoSlide">
    <div
      v-for="(item, index) in items"
      :key="index"
      class="carousel-item"
      :class="{ active: currentIndex === index }"
      @mouseenter="pauseAutoSlide(index)"
    >
      <img :src="item.img" :alt="item.title" class="carousel-image" />
      <div class="overlay" v-if="currentIndex !== index">
        <h3>{{ item.title }}</h3>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { title: '店铺准入', img: require('@/assets/login/financial-1.png') },
        { title: '企业准入', img: require('@/assets/login/financial-2.png') },
        { title: '申请融资', img: require('@/assets/login/financial-3.png') },
        { title: '资方放款', img: require('@/assets/login/financial-4.png') },
      ],
      currentIndex: 0,
      interval: null,
    };
  },
  mounted() {
    this.startAutoSlide()
  },
  methods: {
    startAutoSlide() {
      this.interval = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.items.length;
      }, 3000)
    },
    pauseAutoSlide(index) {
      clearInterval(this.interval)
      // console.log(index)
      this.currentIndex = index
    }
  },
  beforeDestroy() {
    clearInterval(this.interval)
  }
};
</script>

<style lang="scss" scoped>
.carousel {
  display: flex;
  flex-direction: row;
  position: relative;
  width: 100%;
  height: 650px;
  overflow: hidden;
}

.carousel-item {
  position: relative;
  width: calc(30% / 3);
  transition: width 0.5s ease;
}

.carousel-item.active {
  width: 70%;
  opacity: 1;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.overlay {
  position: absolute;
  width: 100%;
  height: 100%;
  bottom: 0;
  left: 0;
  color: #000;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: .5;
}
</style>
