<template>
  <div class="avator-swiper" @click="onSlidePrevChange">
    <swiper ref="mySwiper" :options="swiperOptions" class="avator-swiper_container">
      <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="rightImgPath" />
    </div>
  </div>
</template>
<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
export default {
  name: 'GroupSwiper',
  components: {
    swiper,
    swiperSlide
  },
  props: {
    avatorData: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    const vm = this;
    return {
      rightImgPath: '',
      swiperOptions: {
        loop: true,
        initialSlide: 0,
        slidesPerView: 7,
        spaceBetween: 4,
        speed: 1100,
        centeredSlides: true,
        watchSlidesProgress: true,
        allowTouchMove: false,
        autoplay: false,
        on: {
          init: function() {
            const slideLeft = this.slides.eq(this.activeIndex - 3);
            slideLeft.addClass('ani-left');
          },
          transitionStart: function() {
            const activeIndex = this.activeIndex;
            const slideLeft = this.slides.eq(activeIndex - 3);
            const slideRight = this.slides.eq(activeIndex + 4);
            slideLeft.addClass('ani-left');
            slideRight.addClass('ani-right');
            vm.rightImgPath = 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 ani-right');
            }
          }
        }
      }
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
  watch: {
    avatorData() {
      this.onSlidePrevChange();
    }
  },
  mounted() {
    console.log(this.swiper);
  },
  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: 100%;
    width: 25px;
    position: absolute;
    right: 3px;
    top: 2px;
  }
  &_item {
    transition: transform 0.2s linear;
    padding-top: 2px;
    img {
      height: 25px;
      width: 25px;
      border-radius: 50%;
    }
  }
}
</style>
