<template>
  <div class="avator-swiper" @click="onSlidePrevChange">
    <swiper ref="mySwiper" :options="swiperOptions" class="avator-swiper_container">
      <!-- @slide-change="slideChange"
      @set-translate="setTranslate" -->
      <swiper-slide v-for="(item, index) in avatorData" :key="index" class="avator-swiper_item">
        <div class="avator-swiper_item">
          <img :src="item" />
        </div>
      </swiper-slide>
    </swiper>
    <div class="avator-swiper_right">
      <cr-image class="avator-swiper_item" :src="leftImgPath" />
    </div>
  </div>
</template>
<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
export default {
  name: 'AvatorSwiper',
  components: {
    swiper,
    swiperSlide
  },
  props: {
    avatorData: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    const vm = this;
    return {
      activeIndex: 0,
      leftImgPath: '',
      rightImgPath: '',
      swiperOptions: {
        loop: true,
        initialSlide: 0,
        slidesPerView: 7,
        spaceBetween: 4,
        speed: 1100,
        centeredSlides: true,
        watchSlidesProgress: true,
        allowTouchMove: false,
        autoplay: false,
        // autoplay: {
        //   delay: 900,
        //   stopOnLastSlide: false,
        //   disableOnInteraction: false
        // }
        on: {
          init: function() {
            let slideLeft = this.slides.eq(this.activeIndex - 3);
            slideLeft.addClass('ani-left');
          },
          transitionStart: function() {
            let slideLeft = this.slides.eq(this.activeIndex - 3);
            let slideRight = this.slides.eq(this.activeIndex + 4);
            slideLeft.addClass('ani-left');
            slideRight.addClass('ani-right');
            vm.leftImgPath = slideRight.find('img').attr('src');
          },
          transitionEnd: function() {
            for (let i = 0; i < this.slides.length; i++) {
              let slide = this.slides.eq(i);
              slide.removeClass('ani-left');
              slide.removeClass('ani-right');
            }
          }
        }
      }
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
  watch: {
    avatorData() {
      this.onSlidePrevChange();
    }
  },
  methods: {
    onSlidePrevChange() {
      this.swiper.slidePrev();
    }
  }
};
</script>
<style lang="less">
@keyframes leftToRight {
  from {
    transform: scale(0.3, 0.3);
    opacity: 0;
  }
  to {
    transform: scale(1, 1);
    opacity: 1;
  }
}
.ani-left {
  transform-origin: right;
  animation: leftToRight 0.7s linear;
}
@keyframes scaleTo {
  to {
    opacity: 1;
  }
}
.ani-right {
  opacity: 0;
  // animation: scaleTo 3s ease-out;
}
.avator-swiper {
  width: 100%;
  margin: 0 auto !important;
  position: relative;
  &_right {
    height: 25px;
    width: 25px;
    position: absolute;
    right: 3px;
    top: 2px;
  }
  &_item {
    transition: transform 0.2s linear;
    height: 32px;
    padding-top: 2px;
    img {
      height: 25px;
      width: 25px;
      border-radius: 50%;
    }
    &.active {
      transform: scale(1.2);
    }
  }
}
</style>
