<template>
  <div class="carousel" :style="{backgroundColor: activeColor}">
    <div class="container">
      <slot></slot>
      <div class="carousel-container">
        <div v-swiper:mySwiper="swiperOption">
          <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="banner in banners.data">
              <a :href="banner.hrefUrl" target="_blank">
                <img :src="banner.pictureUrl"/>
              </a>
            </div>
          </div>
          <div class="swiper-pagination swiper-pagination-bullets"></div>
          <div class="swiper-button-prev"><i class="iconfont icon-arrow-left"></i></div>
          <div class="swiper-button-next"><i class="iconfont icon-arrow-right"></i></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'carousel',
    data () {
      return {
        activeSlide: 0,
        swiperOption: {
          autoplay: 6000,
          pagination: '.swiper-pagination',
          paginationClickable: true,
          mousewheelControl: true,
          effect: 'fade',
          lazyLoading: true,
          prevButton: '.swiper-button-prev',
          nextButton: '.swiper-button-next',
          onTransitionStart: (swiper) => {
            // 不要通过vue刷新dom，会导致pagination无法刷新
            // this.activeSlide = swiper.activeIndex
            document.querySelector('.carousel').style.backgroundColor =
              this.banners.data[swiper.activeIndex].metadata['background-color']
          }
        }
      }
    },
    computed: {
      banners () {
        return this.$store.state.carousel.banners
      },
      activeColor () {
        return this.banners.data.length ? this.banners.data[this.activeSlide].metadata['background-color'] : null
      }
    }
  }
</script>
<style lang="scss" scoped>
  @import '~assets/scss/variables';

  $carousel_width: 990px;
  $carousel_height: 477px;

  .carousel {
    transition: background-color .3s;
    position: relative;
    margin-bottom: $lg-pad;

    .carousel-container {
      width: $carousel_width;
      height: $carousel_height;
      margin-left: 200px;
      overflow: hidden;

      .swiper-wrapper {
        .swiper-slide {
          img {
            display: block;
            height: $carousel_height;
          }
        }
      }
    }
  }
</style>
