<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="!advertTop.close" @click="_close"></div>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapMutations } from 'vuex';
export default {
  name: 'advert-top',
  data() {
    return {
      spread: this.$route.query.spread,
      swiper: null,
      list: []
    };
  },
  computed: {
    ...mapGetters({
      advertTop: 'advertTop'
    })
  },
  mounted() {
    this.list = this.advertTop.list;
    this.$nextTick(() => {
      this._initSwiper();
    });
  },
  methods: {
    ...mapMutations({
      set_advertTop: 'set_advertTop'
    }),
    _initSwiper() {
      const self = this;
      const option = {
        direction: 'vertical',
        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.advertTop));
      info.status = false;
      this.set_advertTop(info);
    }
  },
  watch: {
    'advertTop.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>
