<template>
  <div class="loyalty-suite-achievements-block" :class="{
    [`loyalty-suite-achievements-block_${style}`]: true
  }">
    <div class="loyalty-suite-achievements-block__header">
      <div class="loyalty-suite-achievements-block__title">
        {{ $t('myAchievements') }}
      </div>
      <router-link v-if="achievements.length" to="/my-achievements" class="loyalty-suite-achievements-block__link">
        {{ $t('viewAll') }}
      </router-link>
    </div>
    <div class="loyalty-suite-achievements-block__wrapper" :class="{ 'loyalty-suite-swiper-ready': isSwiperReady }">
      <swiper v-if="achievements.length" :slidesPerView="1" :spaceBetween="20" :modules="modules" @init="onSwiperInit"
        :navigation="navigationOptions" class="loyalty-suite-achievements-block__slider">
        <swiper-slide class="loyalty-suite-achievements-block__slide" v-for="(achievement, index) in achievements"
          :key="index">
          <Achievement
            :key="achievement.id || index"
            :achievement="achievement"
            :myAchievements="true"
            :inSlider="true"
            :style="style"
            @share="onShare"
          />
        </swiper-slide>
      </swiper>
      <div v-else class="loyalty-suite-achievements-block__empty">
        <span class="loyalty-suite-material-icons">emoji_events</span>
        {{ $t('noMyAchievements') }}
      </div>

      <div v-if="achievements.length" class="loyalty-suite-achievements-block__prev">
        <div class="loyalty-suite-achievements-block__prev-arrow">
          <span class="loyalty-suite-material-icons">chevron_left</span>
        </div>
      </div>
      <div v-if="achievements.length" class="loyalty-suite-achievements-block__next">
        <div class="loyalty-suite-achievements-block__next-arrow">
          <span class="loyalty-suite-material-icons">chevron_right</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { ref, defineComponent } 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 Achievement from '../components/Achievement.vue';

export default defineComponent({
  props: {
    achievements: {
      type: Array,
      required: true,
    },
    style: {
      type: String,
      default: 'classic',
    },
  },
  components: {
    Achievement,
    Swiper,
    SwiperSlide,
  },
  emits: ['share'],
  setup(props, { emit }) {
    const navigationOptions = {
      nextEl: '.loyalty-suite-achievements-block__next',
      prevEl: '.loyalty-suite-achievements-block__prev',
    };

    const isSwiperReady = ref(false);

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

    const onShare = (link: string) => {
      emit('share', link);
    };

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

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

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

  &__header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 10px 10px 0 10px;
    justify-content: space-between;
  }

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

  &__empty {
    display: flex;
    width: 200px;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    align-self: center;
    padding: 5px 10px;
    span {
      font-size: 72px;
      margin-bottom: 10px;
      color: var(--loyalty-suite-primary);
    }
  }

  a.loyalty-suite-achievements-block__link {
    text-decoration: none;
    transition: 0.3s;
    color: var(--loyalty-suite-dark);
    padding: 5px 10px;
    border: 1px solid var(--loyalty-suite-borders-60);
    background: var(--loyalty-suite-light);
    border-radius: 5px;
    margin-bottom: 10px;

    &:hover {
      color: var(--loyalty-suite-light);
      background: var(--loyalty-suite-primary);
      border-color: var(--loyalty-suite-primary);
    }
  }

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

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

  &__slide {
    width: 100%;
    height: 100%;
  }

  &__prev {
    display: none;
    flex-shrink: 0;
    width: 35px;
    height: 35px;
    position: absolute;
    z-index: 2;
    top: 50%;
    left: 0;
    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: 0;
    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-achievements-block__prev,
.loyalty-suite-swiper-ready .loyalty-suite-achievements-block__next {
  display: flex;
}
</style>
