

<section {% render 'section-attr' %}>
  {% render 'section-bg-image' %}
  <div class="collection-v47-section">
    <xo-container {% render 'section-layout' %}>
      <div class='collection-v47' style='--gap: {{ section.settings.gap }}px'>
      <h2 class='collection-v47__title'>{{ section.settings.title_heading }}</h2>
        {%- for block in section.blocks -%}
          {%- if block.settings.collection != blank -%}
            <xo-animate xo-cascade>
              <a href='{{ block.settings.collection.url }}'>
                <xo-parallax>
                  <div class='collection-v47__item'>
                    <h3 class='collection-v47__item-title'>{{ block.settings.title | escape }}</h3>
                  </div>
                  <xo-parallax-hover xo-strength='160' class='collection-v47__img'>
                    {% if block.settings.image != blank %}
                      {% assign image = block.settings.image %}
                    {% else %}
                      {% assign image = block.settings.collection.featured_image %}
                    {% endif %}
                    {% render 'image', image: image, media_aspect_ratio: section.settings.aspect_ratio %}
                  </xo-parallax-hover>
                </xo-parallax>
              </a>
            </xo-animate>
          {%- endif -%}
        {%- endfor -%}
      </div>
    </xo-container>
  </div>
</section>
<script>
document.addEventListener("DOMContentLoaded", () => {
  const section = document.querySelector(".collection-v47-section");
  if (!section) return;

  const items = document.querySelectorAll(".collection-v47__item");
  if (!items.length) return;

  let lastSrc = null;

  const preloadImage = (src) => {
    if (!src) return;
    const img = new Image();
    img.src = src;
  };

  const getLargeSrc = (item) => {
    const img = item.closest("a")?.querySelector("img");
    if (!img) return null;

    // ưu tiên lấy src gốc thay vì currentSrc
    let src = img.getAttribute("src") || img.src;
    if (!src) return null;

    // thử nâng width nếu url shopify có tham số width
    try {
      const url = new URL(src, window.location.origin);
      if (url.searchParams.has("width")) {
        url.searchParams.set("width", "2400");
        src = url.toString();
      }
    } catch (e) {}

    return src;
  };

  const setActive = (item) => {
    if (!item) return;

    items.forEach((i) => i.classList.remove("is-active"));
    item.classList.add("is-active");

    const src = getLargeSrc(item);
    if (!src || src === lastSrc) return;

    lastSrc = src;
    preloadImage(src);
    section.style.setProperty("--active-bg", `url("${src}")`);
  };

  const firstItem = items[0];
  if (firstItem) {
    const src = getLargeSrc(firstItem);
    if (src) preloadImage(src);
    setActive(firstItem);
  }

  items.forEach((item) => {
    const src = getLargeSrc(item);
    if (src) preloadImage(src);

    item.addEventListener("mouseenter", () => setActive(item));
    item.addEventListener("click", () => setActive(item));
  });
});
</script>

{% schema %}
{
  "name": "t:sections.collection_v1.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": "number",
      "id": "fullwidth_margin",
      "label": "t:sections.all.fullwidth_margin.label",
      "default": 0,
      "visible_if": "{{ section.settings.width == 'fullwidth' }}"
    },
    {
      "type": "range",
      "id": "gap",
      "min": 10,
      "max": 100,
      "step": 5,
      "unit": "px",
      "label": "t:sections.collection_v1.settings.gap.label",
      "default": 50
    },
    {
      "type": "text",
      "id": "title_heading",
      "label": "t:sections.collection_v1.settings.titleheading.label",
      "default": "collection"
    },
    {
      "type": "text",
      "id": "aspect_ratio",
      "label": "t:sections.collection_v1.settings.aspect_ratio.label",
      "default": "1/1"
    },
    {
      "type": "header",
      "content": "t:sections.all.heading.content"
    },
    {
      "type": "color_scheme",
      "id": "color_scheme",
      "label": "t:sections.all.color_scheme.label"
    },
    {
      "type": "select",
      "id": "background_type",
      "label": "t:sections.all.background_type.label",
      "options": [
        {
          "value": "color",
          "label": "t:sections.all.background_type.option__1.label"
        },
        {
          "value": "gradient",
          "label": "t:sections.all.background_type.option__2.label"
        },
        {
          "value": "image",
          "label": "t:sections.all.background_type.option__3.label"
        },
        {
          "value": "video",
          "label": "t:sections.all.background_type.option__4.label"
        }
      ],
      "default": "color"
    },
    {
      "type": "color",
      "id": "background_color",
      "label": "t:sections.all.background_color.label",
      "alpha": true,
      "visible_if": "{{ section.settings.background_type == 'color' }}"
    },
    {
      "type": "color_background",
      "id": "background_gradient",
      "label": "t:sections.all.background_gradient.label",
      "visible_if": "{{ section.settings.background_type == 'gradient' }}"
    },
    {
      "type": "image_picker",
      "id": "background_image_desktop",
      "label": "t:sections.all.background_image_desktop.label",
      "visible_if": "{{ section.settings.background_type == 'image' }}"
    },
    {
      "type": "image_picker",
      "id": "background_image_mobile",
      "label": "t:sections.all.background_image_mobile.label",
      "visible_if": "{{ section.settings.background_type == 'image' }}"
    },
    {
      "type": "checkbox",
      "id": "use_parallax",
      "label": "t:sections.all.use_parallax.label",
      "default": true,
      "visible_if": "{{ section.settings.background_type == 'image' }}"
    },
    {
      "type": "select",
      "id": "background_image_animation",
      "label": "t:sections.all.animation.label",
      "options": [
        {
          "value": "none",
          "label": "t:sections.all.animation.option__0.label"
        },
        {
          "value": "fade",
          "label": "t:sections.all.animation.option__1.label"
        },
        {
          "value": "fade-up",
          "label": "t:sections.all.animation.option__2.label"
        },
        {
          "value": "fade-down",
          "label": "t:sections.all.animation.option__3.label"
        },
        {
          "value": "fade-left",
          "label": "t:sections.all.animation.option__4.label"
        },
        {
          "value": "fade-right",
          "label": "t:sections.all.animation.option__5.label"
        },
        {
          "value": "zoom-in",
          "label": "t:sections.all.animation.option__6.label"
        },
        {
          "value": "zoom-out",
          "label": "t:sections.all.animation.option__7.label"
        },
        {
          "value": "rotate-left-up",
          "label": "t:sections.all.animation.option__8.label"
        },
        {
          "value": "rotate-right-up",
          "label": "t:sections.all.animation.option__9.label"
        },
        {
          "value": "snake-up",
          "label": "t:sections.all.animation.option__10.label"
        },
        {
          "value": "snake-down",
          "label": "t:sections.all.animation.option__11.label"
        },
        {
          "value": "snake-left",
          "label": "t:sections.all.animation.option__12.label"
        },
        {
          "value": "snake-right",
          "label": "t:sections.all.animation.option__13.label"
        },
        {
          "value": "3d-up",
          "label": "t:sections.all.animation.option__14.label"
        }
      ],
      "default": "none",
      "visible_if": "{{ section.settings.background_type == 'image' }}"
    },
    {
      "type": "textarea",
      "id": "background_video_url",
      "label": "t:sections.all.background_video_url.label",
      "visible_if": "{{ section.settings.background_type == 'video' }}"
    },
    {
      "type": "select",
      "id": "background_video_animation",
      "label": "t:sections.all.animation.label",
      "options": [
        {
          "value": "none",
          "label": "t:sections.all.animation.option__0.label"
        },
        {
          "value": "fade",
          "label": "t:sections.all.animation.option__1.label"
        },
        {
          "value": "fade-up",
          "label": "t:sections.all.animation.option__2.label"
        },
        {
          "value": "fade-down",
          "label": "t:sections.all.animation.option__3.label"
        },
        {
          "value": "fade-left",
          "label": "t:sections.all.animation.option__4.label"
        },
        {
          "value": "fade-right",
          "label": "t:sections.all.animation.option__5.label"
        },
        {
          "value": "zoom-in",
          "label": "t:sections.all.animation.option__6.label"
        },
        {
          "value": "zoom-out",
          "label": "t:sections.all.animation.option__7.label"
        },
        {
          "value": "rotate-left-up",
          "label": "t:sections.all.animation.option__8.label"
        },
        {
          "value": "rotate-right-up",
          "label": "t:sections.all.animation.option__9.label"
        },
        {
          "value": "snake-up",
          "label": "t:sections.all.animation.option__10.label"
        },
        {
          "value": "snake-down",
          "label": "t:sections.all.animation.option__11.label"
        },
        {
          "value": "snake-left",
          "label": "t:sections.all.animation.option__12.label"
        },
        {
          "value": "snake-right",
          "label": "t:sections.all.animation.option__13.label"
        },
        {
          "value": "3d-up",
          "label": "t:sections.all.animation.option__14.label"
        }
      ],
      "default": "none",
      "visible_if": "{{ section.settings.background_type == 'video' }}"
    },
    {
      "type": "color",
      "id": "background_overlay",
      "label": "t:sections.all.background_overlay.label",
      "alpha": true,
      "visible_if": "{{ section.settings.background_type == 'image' or section.settings.background_type == 'video' }}"
    },
    {
      "type": "number",
      "id": "background_margin",
      "label": "t:sections.all.background_margin.label",
      "default": 0
    },
    {
      "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": "collection",
      "name": "t:sections.collection_v1.blocks.name",
      "settings": [
        {
          "type": "image_picker",
          "id": "image",
          "label": "t:sections.collection_v1.blocks.settings.image.label"
        },
        {
          "type": "text",
          "id": "title",
          "label": "t:sections.collection_v1.blocks.settings.title.label",
          "default": "SALE 30% ALL ITEMS"
        },
        {
          "type": "collection",
          "id": "collection",
          "label": "t:sections.collection_v1.blocks.settings.collection.label"
        }
      ]
    }
  ],
  "presets": [
    {
      "name": "t:sections.collection_v1.presets.name",
      "category": "Text"
    }
  ]
}
{% endschema %}
