<template>
  <div class="loyalty-suite-rankset" :class="{
    [`loyalty-suite-rankset_${style}`]: true
  }">
    <div v-if="rankSet.show" class="loyalty-suite-rankset__header">
      <div class="loyalty-suite-rankset__title">
        {{ rankSet.name }}
      </div>
    </div>
    <div class="loyalty-suite-rankset__wrapper" :class="{ 'loyalty-suite-swiper-ready': isSwiperReady }">
      <swiper v-if="rankSet.ranks.length" slidesPerView="auto" :spaceBetween="20" :modules="modules"
        @init="onSwiperInit" :navigation="{
          nextEl: '.loyalty-suite-rankset__next.rankset-next-' + index,
          prevEl: '.loyalty-suite-rankset__prev.rankset-prev-' + index,
        }" class="loyalty-suite-rankset__slider">
        <swiper-slide class="loyalty-suite-rankset__slide" v-for="(rank, rankIndex) in rankSet.ranks" :key="rankIndex">
          <Rank :rank="rank" :rankSet="rankSet" :style="style" :inSlider="true" />
        </swiper-slide>
      </swiper>

      <div v-if="rankSet.ranks.length" class="loyalty-suite-rankset__prev" :class="'rankset-prev-' + index">
        <div class="loyalty-suite-rankset__prev-arrow">
          <span class="loyalty-suite-material-icons">chevron_left</span>
        </div>
      </div>
      <div v-if="rankSet.ranks.length" class="loyalty-suite-rankset__next" :class="'rankset-next-' + index">
        <div class="loyalty-suite-rankset__next-arrow">
          <span class="loyalty-suite-material-icons">chevron_right</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/navigation';
import { Navigation } from 'swiper/modules';
import type { Swiper as SwiperInstance } from 'swiper/types';
import Rank from './Rank.vue';

export default defineComponent({
  props: {
    rankSet: {
      type: Array,
      required: true,
    },
    index: {
      type: Number,
      required: true,
    },
    style: {
      type: String,
      default: 'classic',
    },
  },
  components: {
    Swiper,
    SwiperSlide,
    Rank
  },
  setup(props) {
    const isSwiperReady = ref(false);

    const onSwiperInit = (swiper: SwiperInstance) => {
      isSwiperReady.value = true;
    };

    return {
      modules: [Navigation],
      isSwiperReady,
      onSwiperInit
    };
  }
});
</script>

<style lang="scss" scoped>
.loyalty-suite-rankset {
  display: flex;
  flex-direction: column;
  width: 100%;

  &__wrapper {
    display: flex;
    flex-direction: column;
    width: 100%;
    justify-content: center;
    position: relative;
  }

  &__header {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
    margin-bottom: 10px;
  }

  &__title {
    display: flex;
    font-size: 14px;
    font-weight: 700;
    margin-right: 5px;
  }

  &__empty {
    display: flex;
  }

  &__slider {
    width: 100%;
    padding: 10px;
    margin: 0;

    .swiper-slide:last-child {
      margin-right: 10px;
    }
  }

  &__slide {
    width: 270px;
  }

  &__prev {
    display: none;
    flex-shrink: 0;
    width: 35px;
    height: 35px;
    position: absolute;
    z-index: 2;
    top: 50%;
    left: 10px;
    transform: translateY(-50%);
    align-items: center;
    justify-content: center;

    &-arrow {
      display: flex;
      flex-shrink: 0;
      width: 35px;
      height: 35px;
      border-radius: 50%;
      justify-content: center;
      align-items: center;
      transition: 0.3s;
      background: var(--loyalty-suite-primary-80);

      &:hover {
        cursor: pointer;
        background: var(--loyalty-suite-primary-90);
      }
    }

    .loyalty-suite-material-icons {
      font-size: 26px;
      color: var(--loyalty-suite-light);
      margin-right: 2px;
    }

    &.swiper-button-disabled {
      display: none !important;
    }
  }

  &__next {
    display: none;
    flex-shrink: 0;
    width: 35px;
    height: 35px;
    position: absolute;
    z-index: 2;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    align-items: center;
    justify-content: center;

    &-arrow {
      display: flex;
      flex-shrink: 0;
      width: 35px;
      height: 35px;
      border-radius: 50%;
      justify-content: center;
      align-items: center;
      transition: 0.3s;
      background: var(--loyalty-suite-primary-80);

      &:hover {
        cursor: pointer;
        background: var(--loyalty-suite-primary-90);
      }
    }

    .loyalty-suite-material-icons {
      font-size: 26px;
      color: var(--loyalty-suite-light);
    }

    &.swiper-button-disabled {
      display: none !important;
    }
  }
}

.loyalty-suite-swiper-ready .loyalty-suite-rankset__prev,
.loyalty-suite-swiper-ready .loyalty-suite-rankset__next {
  display: flex;
}
</style>
