<template>
  <div class="avator-swiper" :class="{ 'swiper-disabled': !isLoop }">
    <div v-if="isLoop" class="avator_left avator-swiper_item" :class="{ 'ani-left': isAddClass }">
      <img :src="leftImgPath" />
    </div>
    <swiper ref="mySwiper" :options="swiperOptions" :class="{ 'avator-swiper_container': isLoop }">
      <swiper-slide
        v-for="(item, index) in avatorData"
        :key="index"
        :data-src="item.avatar || defaultImg"
        :data-name="item.name"
        class="avator-swiper_item"
      >
        <img :src="(item.avatar || defaultImg) | Img2Thumb" />
      </swiper-slide>
    </swiper>
    <div v-if="isLoop" class="avator_right avator-swiper_item" :class="{ 'ani-right': isAddClass }">
      <img :src="rightImgPath" />
    </div>
    <span class="avator-swiper-bg" />
  </div>
</template>
<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';

import Img2Thumb from '@/filters/img2Thumb.filter';
export default {
  name: 'AvatorSwiper',
  components: {
    swiper,
    swiperSlide
  },
  filters: {
    Img2Thumb
  },
  props: {
    avatorData: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    const vm = this;
    const isLoop = vm.avatorData.length < 9 ? false : true;
    const autoplay = isLoop ? { delay: 1500 } : false;
    return {
      timer: null,
      interTimer: null,
      isLoop,
      isAddClass: false,
      rightImgPath: '',
      leftImgPath: '',
      defaultImg: 'https://himg.bdimg.com/sys/portraitn/item/19e96973556e646566696e6564d8bcc080',
      swiperOptions: {
        loop: isLoop,
        initialSlide: 0,
        slidesPerView: 9,
        // spaceBetween: 4,
        speed: 800,
        centeredSlides: isLoop,
        centeredSlidesBounds: true,
        allowTouchMove: false,
        autoplay,
        on: {
          slideChangeTransitionStart: function() {
            vm.$store.dispatch('goods_avator_info', { showInfo: false });
          },
          transitionStart: function() {
            const activeIndex = this.activeIndex;
            const slideLeft = this.slides.eq(activeIndex - 5);
            const slideRight = this.slides.eq(activeIndex + 4);
            vm.leftImgPath = slideLeft.data('src');
            vm.rightImgPath = slideRight.data('src');
            slideLeft.addClass('ani-opt');
            slideRight.addClass('ani-opt');
            vm.isAddClass = true;
            // clearTimeout(vm.timer);
            // vm.timer = setTimeout(() => {
            //   vm.oneTransLate(vm.getAvatorItem(slideRight));
            //   clearTimeout(vm.timer);
            // }, 1000);
          },
          transitionEnd: function() {
            vm.oneTransLate(vm.getAvatorItem(this.slides.eq(this.activeIndex + 4)));
            for (let i = 0; i < this.slides.length; i++) {
              let slide = this.slides.eq(i);
              slide.removeClass('ani-opt');
            }
            vm.isAddClass = false;
          }
        }
      }
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
  mounted() {
    const avatorData = this.avatorData;
    if (!this.isLoop && avatorData.length) {
      this.startTimeoutAvatar(avatorData);
    }
  },
  beforeDestroy() {
    this.$store.dispatch('goods_avator_info', { showInfo: false });
    clearTimeout(this.timer);
    clearInterval(this.interTimer);
  },
  methods: {
    oneTransLate(avatorItem) {
      this.$store.dispatch('goods_avator_info', {
        ...{ src: avatorItem.avatar, name: avatorItem.name },
        showInfo: true
      });
    },
    getAvatorItem(ele) {
      return {
        avatar: ele.data('src') || '',
        name: ele.data('name') || ''
      };
    },
    startTimeoutAvatar(avatorData) {
      const avatorLength = avatorData.length - 1;
      let count = avatorLength;
      this.oneTransLate(avatorData[count]);
      this.interTimer = setInterval(() => {
        count--;
        this.swiperOptions.on.slideChangeTransitionStart();
        if (count < 0) count = avatorLength;
        clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.oneTransLate(avatorData[count]);
          clearTimeout(this.timer);
        }, 800);
      }, 3000);
    }
  }
};
</script>
<style lang="less" scoped>
@keyframes bigToSamll {
  from {
    transform: scale(1);
    opacity: 1;
  }
  to {
    transform: scale(0.3);
    opacity: 0;
  }
}
@keyframes samllToBig {
  from {
    transform: scale(0.3);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}
.ani-left {
  transform-origin: right;
  animation: bigToSamll 0.8s linear forwards;
}
.ani-right {
  transform-origin: left;
  animation: samllToBig 0.8s linear;
}
.avator-swiper-bg {
  width: 4px;
  height: 24px;
  position: absolute;
  top: 0;
  right: -2px;
  z-index: 2;
  background-color: #f7f8f9;
}
.ani-opt {
  opacity: 0;
  transition: opacity 0.1s linear;
}
.avator_left,
.avator_right {
  position: absolute;
  top: 0;
}
.avator_right {
  right: 4px;
}
.avator_left {
  left: 0px;
}

.avator-swiper {
  position: relative;
  width: 261px;
  margin: 0 auto;
  &_item {
    height: 25px;
    width: 25px;
    // transition: transform 0 0.2s linear;
    box-sizing: border-box;
    // display: flex;
    // align-items: center;
    // justify-content: center;
    // transform: scale(1);
    // transition: transform 0 0.2s linear;
    img {
      display: block;
      height: 25px;
      width: 25px;
      border-radius: 50%;
      box-sizing: border-box;
    }
  }
}

// .swiper-slide {
//   text-align: center;

//   /* Center slide text vertically */
//   display: -webkit-box;
//   display: -ms-flexbox;
//   display: -webkit-flex;
//   display: flex;
//   -webkit-box-pack: center;
//   -ms-flex-pack: center;
//   -webkit-justify-content: center;
//   justify-content: center;
//   -webkit-box-align: center;
//   -ms-flex-align: center;
//   -webkit-align-items: center;
//   align-items: center;
// }
// .avator-swiper_container {
//   .swiper-slide-active {
//     transform: scale(1.2);
//     transition: transform 0.6s linear;
//   }
// }
// .avator-swiper_container {
//   .swiper-slide-active {
//     transform: scale(1);
//     transition: transform 0.6s linear;
//   }
//   .swiper-slide:not(.swiper-slide-active) {
//     transform: scale(0.8);
//   }
// }

.swiper-disabled {
  @{deep} .swiper-wrapper {
    display: flex;
    justify-content: center;
  }
}
</style>
