<template>
  <div class="avator-swiper" :class="{ 'swiper-disabled': !isLoop }">
    <div v-if="isLoop" class="avator_left avator-swiper_item" :class="{ anileft: isAnimate }">
      <img :src="leftImgPath" />
    </div>
    <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 || defaultImg"
        class="avator-swiper_item"
      >
        <img :src="item.avatar || defaultImg" />
      </swiper-slide>
    </swiper>
    <div v-if="isLoop" class="avator_right avator-swiper_item">
      <img :src="rightImgPath" />
    </div>
    <span class="avator-swiper-bg" :style="{ backgroundColor: bgColor }" />
  </div>
</template>
<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
export default {
  name: 'GroupSwiper',
  components: {
    swiper,
    swiperSlide
  },
  props: {
    bgColor: {
      required: true,
      type: String,
      default: ''
    },
    avatorData: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    const vm = this;
    const isLoop = vm.avatorData.length < 7 ? false : true;
    const autoplay = isLoop ? { delay: 1000, reverseDirection: true } : false;
    return {
      timer: null,
      interTimer: null,
      isLoop,
      isAnimate: false,
      leftImgPath: '',
      rightImgPath: '',
      defaultImg: 'https://himg.bdimg.com/sys/portraitn/item/19e96973556e646566696e6564d8bcc080',
      swiperOptions: {
        loop: isLoop,
        loopAdditionalSlides: 2,
        initialSlide: 0,
        slidesPerView: 7,
        speed: 600,
        centeredSlides: isLoop,
        centeredSlidesBounds: true,
        allowTouchMove: false,
        autoplay,
        on: {
          slideChangeTransitionStart: function() {
            vm.$emit('animation-event-start');
          },
          transitionStart: function() {
            const activeIndex = this.activeIndex,
              slideLeft = this.slides.eq(activeIndex - 3),
              slideRight = this.slides.eq(activeIndex + 4);
            vm.isAnimate = true;
            vm.rightImgPath = slideRight.data('src');
            vm.leftImgPath = slideLeft.data('src');
            slideLeft.addClass('ani-opt');
            slideRight.addClass('ani-opt');
          },
          transitionEnd: function() {
            vm.oneTransLate(vm.getAvatorItem(this.slides.eq(this.activeIndex - 3)));
            for (let i = 0; i < this.slides.length; i++) {
              let slide = this.slides.eq(i);
              slide.removeClass('ani-opt');
            }
            vm.isAnimate = false;
          }
        }
      }
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
  mounted() {
    const avatorData = this.avatorData;
    if (!this.isLoop && avatorData.length) {
      this.startTimeoutAvatar(avatorData);
    }
  },
  destroyed() {
    clearTimeout(this.timer);
    clearInterval(this.interTimer);
  },
  methods: {
    oneTransLate(fristItem) {
      this.$emit('animation-event-end', { src: fristItem.avatar, name: fristItem.name });
    },
    getAvatorItem(ele) {
      return {
        avatar: ele.data('src'),
        name: ele.data('name')
      };
    },
    startTimeoutAvatar(avatorData) {
      const avatorLength = avatorData.length;
      let count = 0;
      this.oneTransLate(avatorData[count]);
      this.interTimer = setInterval(() => {
        count++;
        this.swiperOptions.on.slideChangeTransitionStart();
        if (count >= avatorLength) count = 0;
        clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.oneTransLate(avatorData[count]);
          clearTimeout(this.timer);
        }, 800);
      }, 2000);
    }
  }
};
</script>
<style lang="less" scoped>
@keyframes leftToRight {
  from {
    transform: scale(0.3) translate3d(0, 0, 0);
    opacity: 0;
  }
  to {
    transform: scale(1) translate3d(0, 0, 0);
    opacity: 1;
  }
}
.anileft {
  transform-origin: right;
  animation: leftToRight 0.6s linear;
}

.ani-opt {
  opacity: 0;
}
.avator_left,
.avator_right {
  position: absolute;
  top: 0px;
}
.avator_right {
  right: 8px;
}
.avator_left {
  left: 0px;
}

.avator-swiper-bg {
  width: 4px;
  height: 24px;
  position: absolute;
  top: 0;
  right: -2px;
  z-index: 2;
}
.avator-swiper {
  position: relative;
  width: 100%;
  height: 24px;
  margin-right: -15px;
  &_container {
    transition-timing-function: linear;
  }
  &_item {
    height: 24px;
    width: 24px;
    transition: transform 0 0.2s linear;
    box-sizing: border-box;
    img {
      display: block;
      height: 24px;
      width: 24px;
      border-radius: 50%;
      border: solid @white 1px;
      box-sizing: border-box;
    }
  }
}
.swiper-disabled {
  @{deep} .swiper-wrapper {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
