<template>
  <BaseBlock
    :title="block.title"
    :description="block.description"
    :moreLink="hasCategory ? {name: 'product.categories.show', params: { category_id: block.data.category_id }} : null"
    :moreText="block.data.all_text"
  >
    <div v-if="!hasCategory" role="alert" class="alert alert-warning alert-soft">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 shrink-0 stroke-current" fill="none" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
      </svg>
      <span>Для блока не выбрана категория.</span>
    </div>
    <div v-else-if="!hasProducts" role="alert" class="alert alert-soft">
      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 shrink-0 stroke-current" fill="none" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
      </svg>
      <span>В этой категории пока нет товаров.</span>
    </div>
    <Swiper
        v-else
        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"
          class="pb-1"
      >
        <div class="radius-box bg-base-100 shadow-sm p-2">
          <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 {computed} from "vue";
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 hasCategory = computed(() => Boolean(props.block.data?.category_id));
const hasProducts = computed(() => (props.block.data?.products?.data?.length ?? 0) > 0);

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>