{{ 'collection-card.css' | asset_url | stylesheet_tag }}

{% liquid
  case section.settings.sort
    when 'products_high', 'products_low'
      assign collections = collections | sort: 'all_products_count'
    when 'date', 'date_reversed'
      assign collections = collections | sort: 'published_at'
  endcase

  if section.settings.sort == 'products_high' or section.settings.sort == 'date_reversed' or section.settings.sort == 'alphabetical_reversed'
    assign collections = collections | reverse
  endif
%}

<div {% render 'section-attr' %}>
  {% render 'section-bg-image' %}
  <xo-container {% render 'section-layout' %}>
    {%- case section.settings.list_type -%}
      {%- when 'custom' -%}
        <xo-grid style='--lg: {{ section.settings.columns_desktop }}; --md: {{ section.settings.columns_desktop | minus: 1 }}; --xs: {{ section.settings.columns_mobile }}'>
          {%- for block in section.blocks -%}
            {%- assign placeholder_image_index = forloop.index0 | modulo: 4 | plus: 1 -%}
            {%- assign placeholder_image = 'collection-apparel-' | append: placeholder_image_index -%}
            {% render 'collection-card',
              collection_item: block.settings.custom_collection,
              media_aspect_ratio: section.settings.collection_card_aspect_ratio,
              show_description: false,
              show_product_count: section.settings.collection_card_show_product_count,
              color_scheme: section.settings.card_color_scheme,
              placeholder_image: placeholder_image,
              custom_image: block.settings.custom_image
            %}
          {%- endfor -%}
        </xo-grid>
      {%- when 'all' -%}
        {%- if section.settings.collection_loadtype == 'paginate' -%}
          {%- paginate collections by section.settings.collections_per_page -%}
            <xo-grid style='--lg: {{ section.settings.columns_desktop }}; --md: {{ section.settings.columns_desktop | minus: 1 }}; --xs: {{ section.settings.columns_mobile }}'>
              {%- for collection in collections -%}
                {%- assign placeholder_image_index = forloop.index0 | modulo: 4 | plus: 1 -%}
                {%- assign placeholder_image = 'collection-apparel-' | append: placeholder_image_index -%}
                {% render 'collection-card',
                  collection_item: collection,
                  media_aspect_ratio: section.settings.collection_card_aspect_ratio,
                  show_description: false,
                  show_product_count: section.settings.collection_card_show_product_count,
                  color_scheme: section.settings.card_color_scheme,
                  placeholder_image: placeholder_image
                %}
              {%- endfor -%}
            </xo-grid>
            {%- render 'pagination', paginate: paginate -%}
          {%- endpaginate -%}
        {%- else -%}
          <xo-grid style='--lg: {{ section.settings.columns_desktop }}; --md: {{ section.settings.columns_desktop | minus: 1 }}; --xs: {{ section.settings.columns_mobile }}'>
            {%- for collection in collections -%}
              {%- assign placeholder_image_index = forloop.index0 | modulo: 4 | plus: 1 -%}
              {%- assign placeholder_image = 'collection-apparel-' | append: placeholder_image_index -%}
              {% render 'collection-card',
                collection_item: collection,
                media_aspect_ratio: section.settings.collection_card_aspect_ratio,
                show_description: false,
                show_product_count: section.settings.collection_card_show_product_count,
                color_scheme: section.settings.card_color_scheme,
                placeholder_image: placeholder_image
              %}
            {%- endfor -%}
          </xo-grid>
        {%- endif -%}
    {%- endcase -%}
  </xo-container>
</div>

{% schema %}
{
  "name": "t:sections.main_list_collections.name",
  "settings": [
    {
      "type": "select",
      "id": "width",
      "label": "t:sections.main_list_collections.settings.width.label",
      "options": [
        {
          "value": "box",
          "label": "t:sections.main_list_collections.settings.width.option_1.label"
        },
        {
          "value": "wide",
          "label": "t:sections.main_list_collections.settings.width.option_2.label"
        }
      ],
      "default": "box"
    },
    {
      "type": "select",
      "id": "list_type",
      "label": "t:sections.main_list_collections.settings.list_type.label",
      "options": [
        {
          "value": "all",
          "label": "t:sections.main_list_collections.settings.list_type.option_1.label"
        },
        {
          "value": "custom",
          "label": "t:sections.main_list_collections.settings.list_type.option_2.label"
        }
      ],
      "default": "custom"
    },
    {
      "type": "select",
      "id": "sort",
      "options": [
        {
          "value": "alphabetical",
          "label": "t:sections.main_list_collections.settings.sort.options__1.label"
        },
        {
          "value": "alphabetical_reversed",
          "label": "t:sections.main_list_collections.settings.sort.options__2.label"
        },
        {
          "value": "date_reversed",
          "label": "t:sections.main_list_collections.settings.sort.options__3.label"
        },
        {
          "value": "date",
          "label": "t:sections.main_list_collections.settings.sort.options__4.label"
        },
        {
          "value": "products_high",
          "label": "t:sections.main_list_collections.settings.sort.options__5.label"
        },
        {
          "value": "products_low",
          "label": "t:sections.main_list_collections.settings.sort.options__6.label"
        }
      ],
      "default": "alphabetical",
      "label": "t:sections.main_list_collections.settings.sort.label"
    },
    {
      "type": "select",
      "id": "collection_loadtype",
      "label": "t:sections.main_list_collections.settings.collection_loadtype.label",
      "info": "t:sections.main_list_collections.settings.collection_loadtype.info",
      "options": [
        {
          "value": "paginate",
          "label": "t:sections.main_list_collections.settings.collection_loadtype.option_1.label"
        },
        {
          "value": "infinity",
          "label": "t:sections.main_list_collections.settings.collection_loadtype.option_2.label"
        }
      ],
      "default": "infinity"
    },
    {
      "type": "range",
      "id": "collections_per_page",
      "min": 8,
      "max": 24,
      "step": 1,
      "default": 12,
      "label": "t:sections.main_list_collections.settings.collections_per_page.label",
      "info": "t:sections.main_list_collections.settings.collections_per_page.info"
    },
    {
      "type": "range",
      "id": "columns_desktop",
      "min": 1,
      "max": 4,
      "step": 1,
      "default": 4,
      "label": "t:sections.main_list_collections.settings.columns_desktop.label"
    },
    {
      "type": "select",
      "id": "columns_mobile",
      "options": [
        {
          "value": "1",
          "label": "t:sections.main_list_collections.settings.columns_mobile.options__1.label"
        },
        {
          "value": "2",
          "label": "t:sections.main_list_collections.settings.columns_mobile.options__2.label"
        }
      ],
      "default": "1",
      "label": "t:sections.main_list_collections.settings.columns_mobile.label"
    },
    {
      "type": "header",
      "content": "t:sections.all.collection_card_heading.content"
    },
    {
      "type": "checkbox",
      "id": "collection_card_show_product_count",
      "label": "t:sections.all.collection_card_show_product_count.label",
      "default": true
    },
    {
      "type": "text",
      "id": "collection_card_aspect_ratio",
      "label": "t:sections.all.collection_card_aspect_ratio.label",
      "default": "3/4"
    },
    {
      "type": "color_scheme",
      "id": "card_color_scheme",
      "label": "t:sections.all.card_color_scheme.label"
    },
    {
      "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": "custom_collection",
      "name": "t:sections.main_list_collections.blocks.name",
      "settings": [
        {
          "type": "collection",
          "id": "custom_collection",
          "label": "t:sections.main_list_collections.blocks.custom_collection.label"
        },
        {
          "type": "image_picker",
          "id": "custom_image",
          "label": "t:sections.main_list_collections.blocks.custom_image.label",
          "info": "t:sections.main_list_collections.blocks.custom_image.info"
        }
      ]
    }
  ]
}
{% endschema %}
