<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"
        :data-name="item.name"
        :data-src="item.avatar"
        class="avator-swiper_item"
      >
        <img :src="item.avatar" />
      </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 {
      timer: null,
      rightImgPath: '',
      avatorItem: null,
      swiperOptions: {
        loop: true,
        initialSlide: 0,
        slidesPerView: 7,
        spaceBetween: 4,
        speed: 700,
        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);
            vm.avatorItem = vm.getAvatorItem(slideLeft);
            vm.rightImgPath = slideRight.data('src');
            slideLeft.addClass('ani-left');
            slideRight.addClass('ani-right');
          },
          transitionEnd: function() {
            vm.$emit('animation-event-end', vm.avatorItem);
            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() {
      clearInterval(this.timer);
      this.onSlidePrevChange();
    }
  },
  mounted() {
    this.onSlidePrevChange();
  },
  beforeDestroy() {
    this.swiper.destroy(true);
    clearInterval(this.timer);
  },
  methods: {
    onSlidePrevChange() {
      this.timer = setInterval(() => {
        this.$emit('animation-event-start');
        this.swiper.slidePrev();
      }, 10000);
    },
    getAvatorItem(ele) {
      return {
        src: ele.data('src'),
        name: ele.data('name')
      };
    }
  }
};
</script>
<style lang="less">
@keyframes leftToRight {
  from {
    transform: scale(0.3, 0.3);
    transform-origin: right right;
    opacity: 0;
  }
  to {
    transform: scale(1, 1);
    transform-origin: left left;
    opacity: 1;
  }
}
.ani-left {
  transform-origin: right;
  animation: leftToRight 1s 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: 0px;
  }
  &_item {
    transition: transform 0.2s linear;
    padding-top: 2px;
    img {
      height: 25px;
      width: 25px;
      border-radius: 50%;
      border: solid @white 1px;
    }
  }
}
</style>
