{{ 'section-collection-v2.css' | asset_url | stylesheet_tag }}

<section {% render 'section-attr', color_scheme: section.settings.color_scheme, background_color: section.settings.background_color %}>
  {% render 'section-bg-image' %}

  <xo-container {% render 'section-layout' %}>
    <xo-animate
      xo-type='fade-up'
      xo-delay='100'
      xo-duration='1000'
    >
      <div class='section-collection-v50'>
        <div class='collection-v50__header'>
          {%- if section.settings.collection_title -%}
            <h2 class='collection-v50__header-title'>{{ section.settings.collection_title }}</h2>
          {%- endif -%}
          <div>
            <a href='{{section.settings.btn_link}}' class='collection-v50__header-link'>{{ section.settings.btn_text }}</a>
          </div>
        </div>

        <xo-carousel
          xo-per-view='{{ section.settings.columns_carousel }}'
          xo-gap='{{section.settings.gap}}'
          xo-overflow='visible'
          xo-speed='400'
          xo-per-move='1'
          xo-breakpoints='
            {
              600: {
                perView: 1,
                perMove: 1,
              },
              1000: {
                perView: 2,
                perMove: 1,
              },

            }
          '
          class='xo-carousel'
          xo-render-bullet="<span class='collection-v50__carousel-bullet'></span>"
        >
          <xo-carousel-inner>
            <xo-carousel-list>
              {% for block in section.blocks %}
                {%- if block.settings.collection != blank -%}
                  <xo-carousel-slide class='collection-v50-slide'>
                    <div class='collection-v50 {%render 'hover-style'%}'>
                      <a href='{{block.settings.collection.url}}' class='collection-v50__img'>
                        {%- if block.settings.collection_image -%}
                          {% render 'image', image: block.settings.collection_image %}
                        {%- else -%}
                          {% render 'image', image: block.settings.collection.featured_image %}
                        {%- endif -%}
                      </a>
                      <div class='collection-v50__content'>
                        <h3 class='collection-v50__item'>
                          <a class='collection-v50__link hover-style__link' href='{{block.settings.collection.url }}'>
                            {{ block.settings.collection.title }}
                            <span class='collection-v50__link-icon'>
                              {%- render 'icon', name: 'icon-arrow', icon_size: '16', color: 'rgb(var(--color-foreground))' -%}
                            </span>
                          </a>
                        </h3>
                        <div class='collection-v50__subtitle'>{{ block.settings.collection.products_count }} products</div>
                      </div>
                    </div>
                  </xo-carousel-slide>
                {%- endif -%}
              {% endfor %}
            </xo-carousel-list>
          </xo-carousel-inner>
          <xo-carousel-prev class='collection-v50__btn-prev'>
            <button>
              {%- render 'icon', name: 'chevron-left', icon_size: '20', color: ' rgb(var(--color-foreground))' -%}
            </button>
          </xo-carousel-prev>
          <xo-carousel-next class='collection-v50__btn-next'>
            <button>
              {%- render 'icon', name: 'chevron-right', icon_size: '20', color: ' rgb(var(--color-foreground))' -%}
            </button>
          </xo-carousel-next>
          <xo-carousel-pagination
            class='collection-v50__pagination'
          ></xo-carousel-pagination>
        </xo-carousel>
      </div>
    </xo-animate>
  </xo-container>
</section>

<script>
  // Select elements
  const carousel = document.querySelector('.xo-carousel');
  const pagination = document.querySelector('.collection-v50__pagination');
  const prevBtn = document.querySelector('.collection-v50__btn-prev');
  const nextBtn = document.querySelector('.collection-v50__btn-next');
  const carouselLimit = document.querySelector('.xo-carousel').getAttribute('xo-per-view');

  // Calculate width
  const slides = carousel.querySelectorAll('.collection-v50-slide').length - carouselLimit;
  const paginationWidth = pagination.offsetWidth;
  const itemWidth = Math.min(100 / slides, paginationWidth / slides);

  // Position
  let currentPosition = 0;

  // Update function
  function updatePosition(direction) {
    // Update position
    currentPosition = Math.max(0, Math.min(currentPosition + direction * itemWidth, 100));
    if (currentPosition > 100) {
      currentPosition = 100;
    }
    // Update pagination
    pagination.style.setProperty('--position', `${currentPosition}%`);

    // Update carousel
    const index = currentPosition / itemWidth;
    carousel.goTo(index);
  }

  // Prev/Next handlers
  prevBtn.addEventListener('click', () => updatePosition(-1));
  nextBtn.addEventListener('click', () => updatePosition(1));

  // Reset position on drag end
  pagination.addEventListener('dragend', () => {
    currentPosition = 0;
    pagination.style.setProperty('--position', '0%');
  });
</script>

{% schema %}
{
  "name": "t:sections.collection_v2.name",
  "tag": "section",
  "class": "section",
  "settings": [
    {
      "type": "select",
      "id": "width",
      "label": "t:sections.all.width.label",
      "default": "box",
      "options": [
        {
          "value": "box",
          "label": "t:sections.all.width.options__1.label"
        },
        {
          "value": "wide",
          "label": "t:sections.all.width.options__2.label"
        },
        {
          "value": "fullwidth",
          "label": "t:sections.all.width.options__3.label"
        }
      ]
    },
    {
      "type": "text",
      "id": "collection_title",
      "label": "t:sections.collection_v2.settings.collection_title.label",
      "default": "Collection Title"
    },
    {
      "type": "text",
      "id": "btn_text",
      "label": "t:sections.collection_v2.settings.btn_text.label",
      "default": "Shop All"
    },
    {
      "type": "url",
      "id": "btn_link",
      "label": "t:sections.collection_v2.settings.btn_link.label",
      "default": "/collections/all"
    },
    {
      "type": "range",
      "id": "columns_carousel",
      "min": 3,
      "max": 8,
      "step": 1,
      "label": "t:sections.collection_v2.settings.columns_carousel.label",
      "default": 3
    },
    {
      "type": "range",
      "id": "gap",
      "min": 0,
      "max": 30,
      "step": 1,
      "unit": "px",
      "label": "t:sections.collection_v2.settings.gap.label",
      "default": 15
    },
    {
      "type": "header",
      "content": "Setting additional"
    },
    {
      "type": "select",
      "id": "hover",
      "label": "t:sections.all.hover.label",
      "default": "hover1",
      "options": [
        {
          "value": "hover1",
          "label": "t:sections.all.hover.option__1.label"
        },
        {
          "value": "hover2",
          "label": "Style 02"
        },
        {
          "value": "hover3",
          "label": "Style 03"
        },
        {
          "value": "hover4",
          "label": "Style 04"
        },
        {
          "value": "hover5",
          "label": "Style 05"
        },
        {
          "value": "hover6",
          "label": "Style 06"
        }
      ]
    },
    {
      "type": "header",
      "content": "t:sections.all.heading.content"
    },
    {
      "type": "color_scheme",
      "id": "color_scheme",
      "label": "t:sections.all.color_scheme.label"
    },
    {
      "type": "color",
      "id": "background_color",
      "label": "t:sections.all.bg_color.label"
    },
    {
      "type": "color_background",
      "id": "background_gradient",
      "label": "t:sections.all.bg_gradient.label"
    },
    {
      "type": "image_picker",
      "id": "background_image_desktop",
      "label": "t:sections.all.background_image_desktop.label"
    },
    {
      "type": "image_picker",
      "id": "background_image_mobile",
      "label": "t:sections.all.background_image_mobile.label"
    },
    {
      "type": "checkbox",
      "id": "use_parallax",
      "label": "t:sections.all.use_parallax.label",
      "default": true
    },
    {
      "type": "number",
      "id": "padding_top",
      "label": "t:sections.all.padding_top.label",
      "default": 50
    },
    {
      "type": "number",
      "id": "padding_bottom",
      "label": "t:sections.all.padding_bottom.label",
      "default": 50
    }
  ],
  "blocks": [
    {
      "type": "review",
      "name": " Collection",
      "settings": [
        {
          "type": "collection",
          "id": "collection",
          "label": "t:sections.collection_v2.blocks.settings.collection.label"
        },
        {
          "type": "image_picker",
          "id": "collection_image",
          "label": "t:sections.collection_v2.blocks.settings.collection_image.label",
          "info": "Support jpg, jpeg, png, svg"
        }
      ]
    }
  ],
  "presets": [
    {
      "name": "t:sections.collection_v2.presets.name"
    }
  ]
}
{% endschema %}
