<template>
  <div v-bind="$attrs" class="relative w-full">
    <swiper ref="swiper" class="relative w-full h-full" :options="swiperOption">
      <swiper-slide v-for="(item, index) in items" :key="index">
        <div class="w-full h-full absolute bg-[rgba(0,0,0,0)]"></div>
        <img class="w-full h-full object-cover" @click="handleGoTo(item)" :src="item.bannerUrl" />

        <div
          :class="`absolute bottom-23rem left-26rem font-normal z-50 flex text-white text-size-20rem ${
            titleClass ?? ''
          }`"
        >
          {{ item.content }}
        </div>
      </swiper-slide>
    </swiper>
    <div v-if="showPagination" :class="`absolute bottom-22rem right-12rem z-50 flex ${paginationclass}`">
      <div
        @click="clickPagination(index)"
        v-for="(item, index) in items"
        :key="index"
        :class="`dot ${index === activeIndex ? 'selected' : ''}`"
      ></div>
    </div>
  </div>
</template>

<script>
import { nanoid } from "nanoid";
import { Swiper, SwiperSlide } from "vue-awesome-swiper";
import "swiper/swiper-bundle.min.css";

import SwiperCore, { Autoplay } from "swiper/core";
SwiperCore.use([Autoplay]);
let vm
export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  props: {
    items: {
      default: [],
    },
    showPagination: {
      default: false,
    },
    paginationclass:{
      default: ''
    } ,
    titleClass: {},
  },
  computed: {
    paginationId: () => nanoid(6),
    swiperOption: () => ({
      slidesPerView: 1,
      click: false,
      // loop: true,
      autoplay: {
        delay: 2000,
        // pauseOnMouseEnter: true,
      },
      on: {
        click () {
           vm.handleGoTo(vm.items[this.activeIndex])
        }
      }
    }),
  },

  data() {
    return {
      activeIndex: 0,
    };
  },

  methods: {
    handleGoTo(item){

      if(item.link){

        if(item.link.startsWith("http")){
          window.open(item.link);
        }else{
          this.$router.push(item.link);
        }

      }
    },
    change(swiper) {
      if (!this.showPagination) return;
      this.activeIndex = swiper.activeIndex;
    },
    clickPagination(index) {
      this.$refs.swiper.$swiper.slideTo(index);
    },
  },
  mounted() {
    this.$refs.swiper.$swiper.on("activeIndexChange", this.change);
    vm = this;
  },
};
</script>
<style>
.swiper-pagination-bullet-active {
  @apply bg-white;
}
.swiper-pagination-bullet {
  @apply bg-rgba(255,255,255,1);
}
.dot {
  @apply w-12rem h-12rem rounded-full bg-[rgba(255,255,255,0.3)] mr-10rem cursor-pointer;
}
.dot.selected {
  @apply bg-white;
}
</style>
