<template>
  <BaseBlock
    :title="block.title"
    :description="block.description"
    :moreLink="{name: 'product.categories.show', params: { category_id: block.data.category_id }}"
    :moreText="block.data.all_text"
  >
    <Swiper
        class="select-none block-products-carousel"
        :slides-per-view="block.data?.carousel?.slides_per_view || 2.5"
        :space-between="block.data?.carousel?.space_between || 20"
        :autoplay="block.data?.carousel?.autoplay || false"
        :freeMode="freeModeSettings"
        :lazy="true"
        @sliderMove="hapticScroll"
    >
      <SwiperSlide
          v-for="product in block.data.products.data"
          :key="product.id"

      >
        <div class="will-change-transform active:scale-97 transition-transform">
          <RouterLink
              :to="{name: 'product.show', params: {id: product.id}}"
              @click="slideClick(product)"

          >
            <div class="text-center">
              <img :src="product.images[0].url" :alt="product.name" loading="lazy" class="product-image"/>
              <PriceTitle :product="product"/>
            </div>
          </RouterLink>
        </div>

      </SwiperSlide>
    </Swiper>
  </BaseBlock>
</template>

<script setup>
import {useYaMetrikaStore} from "@/stores/yaMetrikaStore.js";
import {Swiper, SwiperSlide} from "swiper/vue";
import {useHapticScroll} from "@/composables/useHapticScroll.js";
import BaseBlock from "@/components/MainPage/Blocks/BaseBlock.vue";
import PriceTitle from "@/components/ProductItem/PriceTitle.vue";

const hapticScroll = useHapticScroll();
const yaMetrika = useYaMetrikaStore();

const props = defineProps({
  block: {
    type: Object,
    required: true,
  }
});

const freeModeSettings = {
  enabled: props.block.data?.carousel?.freemode?.enabled || false,
};

function slideClick(product) {
  if (props.block.goal_name) {
    yaMetrika.reachGoal(props.block.goal_name, {
      product_id: product.id,
      product_name: product.name,
    });
  }
}
</script>

<style scoped>
.product-image {
  border-radius: var(--radius-box);
}
</style>