<template>
  <div class="wrapper">
    <div class="box">
      <div class="swiper-container" ref="swiper">
        <div class="swiper-wrapper">
          <div class="swiper-slide" v-for="(item, index) in list" :key="index">
            <a v-if="item.advertLink" :href="item.advertLink" target="_blank">
              <img :src="item.advertImgUrl" alt="" />
            </a>
            <img v-else :src="item.advertImgUrl" alt="" />
          </div>
        </div>
      </div>
      <div class="close" v-if="!advertMiddle.close" @click="_close"></div>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapMutations } from 'vuex';
export default {
  name: 'advert-middle',
  data() {
    return {
      spread: this.$route.query.spread,
      swiper: null,
      list: []
    };
  },
  computed: {
    ...mapGetters({
      advertMiddle: 'advertMiddle'
    })
  },
  mounted() {
    this.list = this.advertMiddle.list;
    this.$nextTick(() => {
      this._initSwiper();
    });
  },
  methods: {
    ...mapMutations({
      set_advertMiddle: 'set_advertMiddle'
    }),
    _initSwiper() {
      const self = this;
      const option = {
        direction: 'horizontal',
        loop: true,
        autoplay: self.list.length > 1 ? { delay: 5000 } : false
      };
      this.swiper = new this.$swiper(this.$refs.swiper, option);
    },
    _close() {
      const info = JSON.parse(JSON.stringify(this.advertMiddle));
      info.status = false;
      this.set_advertMiddle(info);
    }
  },
  watch: {
    'advertMiddle.list'(nVal) {
      this.list = nVal;
      this.$nextTick(() => {
        this._initSwiper();
      });
    }
  }
};
</script>
<style lang="less" scoped>
.wrapper {
  position: relative;
  padding-top: 150/1080 * 100%;
  height: 0;
  .box {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    .swiper-container {
      height: 100%;
      .swiper-wrapper {
        height: 100%;
        .swiper-slide {
          position: relative;
          a,
          img {
            display: block;
            width: 100%;
            height: 100%;
          }
        }
      }
    }
    .close {
      position: absolute;
      right: 0;
      top: 0;
      z-index: 10;
      width: 0.3rem;
      height: 0.3rem;
      background: url('./img/icon-close.png') no-repeat center center;
      background-size: 0.2rem 0.2rem;
    }
  }
}
</style>
