{{ 'section-featured-blog.aio.min.css' | asset_url | stylesheet_tag }}

{%- liquid
  assign section_id = '#section-' | append: section.id
  assign slider_id = '#slider-' | append: section.id
  assign enable_slider = section.settings.enable_slider
  assign controls_id = '#controls-' | append: section.id
  assign items_resp = section.settings.items_resp

  assign blog = blogs[section.settings.blog]
  if section.settings.blog != blank and section.settings.limit > blog.articles_count
    assign limit = blog.articles_count
  else
    assign limit = section.settings.limit
  endif
  
  assign items_spacing = section.settings.items_spacing
  if items_spacing
    assign items_spacing_slider = '30px'
  else
    assign items_spacing_slider = '0'
    assign items_spacing_row = 'no-padding'
  endif

  assign heading_style_block = section.blocks | where: 'type' , 'heading-style' | first
-%}

{%- capture section_heading -%}
  {%- render 'snippet-heading', type:'render-html', config: section.settings -%}
{%- endcapture -%}

{%- capture grid_class -%}{%- render 'set-item-per-row', value: items_resp -%}{%- endcapture -%}

{%- capture content -%}
  {%- if section.settings.blog == blank or blog.articles_count < 1 -%}
    {%- for index in (1..limit) -%}
      {%- assign placeholder_image_index = index | minus: 1 | modulo: 3 | plus: 1 -%}
      <article class="featured_blog__item{% unless enable_slider %} {{ grid_class }}{% endunless %}">
        <div class="featured_blog__item-inner">
          <div class="featured_blog__item-image">
            {{ 'blog-apparel-' | append: placeholder_image_index | placeholder_svg_tag: 'placeholder-svg' }}
          </div>
          <div class="featured_blog__item-info">
            <h3 class="featured_blog__item-title">
              {{ 'general.onboarding.blog_post' | t }}
            </h3>
          </div>
        </div>
      </article>
    {%- endfor -%}

  {%- else -%}
    {%- for article in blog.articles limit: limit -%}
      <article class="featured_blog__item{% unless enable_slider %} {{ grid_class }}{% endunless %}">
        <div class="featured_blog__item-inner">
          <div class="featured_blog__item-image">
            <a href="{{ article.url }}">
              {%- render 'image-style', image: article.image -%}
            </a>
          </div>
          <div class="featured_blog__item-info">
            {%- if section.settings.visibility_date -%}
              <span class="featured_blog__item-publish">{{ article.published_at | date: "%b %d, %Y" }}</span>
            {%- endif -%}
            
            <h3 class="featured_blog__item-title" style="font-size: {{ section.settings.blog_post_heading_fs }}px; font-weight: {{ section.settings.blog_post_heading_fw }};">
              <a href="{{ article.url }}">{{ article.title }}</a>
            </h3>

            {%- if section.settings.show_excerpt and article.excerpt_or_content != blank -%}
              <div class="featured_blog__item-excerpt">{{ article.excerpt_or_content }}</div>
            {%- endif -%}
            
            {%- if section.settings.read_more_btn -%}
              <a class="featured_blog__item-btn" href="{{ article.url }}">
                <span>{{ 'general.button.read_more' | t }}</span>
                {%- render 'svg', type: 'arrow-right' -%}
              </a>
            {%- endif -%}
          </div>
        </div>
      </article>
    {%- endfor -%}
  {%- endif -%}
{%- endcapture -%}

<div class="section-featured-blog" id="section-{{ section.id }}" data-section-id="{{ section.id }}"{% if enable_slider %} data-section-type="featured-collection"{% endif %}>
  <div class="{%- if template == 'index' or template contains 'page' -%}{{ section.settings.section_layout }}{%- endif -%}">
    <div class="section-wrapper {{ items_spacing_row }}">

      {%- if section_heading != blank -%}
        {%- capture heading_css -%}
          {%- render 'snippet-heading', type: 'render-css', config: heading_style_block.settings, classID: section_id -%}
        {%- endcapture -%}
        <div class="section-header">{{ section_heading }}</div>
      {%- endif -%}

      <div class="section-body">
        {%- if enable_slider -%}<slider-component>{%- endif -%}
          <div class="slider-container js-slider-container">
            <div class="slider-wrapper">

              <div class="slider-list{% unless enable_slider %} row{% endunless %}" id="slider-{{ section.id }}">
                {{ content }}
              </div>

              {%- if enable_slider -%}
                {%- render 'set-tns-config'
                  slider_id: slider_id,
                  classID: section_id,
                  loop: section.settings.loop,
                  dots: section.settings.dots,
                  controls: section.settings.controls,
                  controls_id: controls_id,
                  customControls: true,
                  autoplay: section.settings.autoplay,
                  autoplay_timeout: section.settings.autoplay_timeout,
                  padding: items_spacing_slider,
                  items_resp: items_resp,
                  total_item: limit
                -%}
              {%- endif -%}
            </div>
          </div>
        {%- if enable_slider -%}</slider-component>{%- endif -%}
      </div>
    </div>
  </div>
</div>

{%- if enable_slider -%}
  <script>
    window.theme = window.theme || {};
    if(!!window.theme.sectionRegister){
      !window.theme.sectionRegister.includes("featured-collection") && (window.theme.sectionRegister = [...window.theme.sectionRegister, "featured-collection"]);
    }else{
      window.theme.sectionRegister = ["featured-collection"];
    }
  </script>

  <script>
    window.theme = window.theme || {};
    if(!!window.theme.customElementsList){
      !window.theme.customElementsList.includes("slider-component") && (window.theme.customElementsList = [...window.theme.customElementsList, "slider-component"]);
    }else{
      window.theme.customElementsList = ["slider-component"];
    }
  </script>
{%- endif -%}

{%- capture section_css -%}
  {{- heading_css -}}
  {%- render 'snippet-theme', type: 'section-style', preClassID: section_id, config: section.settings -%}
{%- endcapture -%}

{%- style -%}{{ section_css | strip | strip_newlines | remove: '  ' }}{%- endstyle -%}

{%- schema -%}
{
  "name": "t:sections.featured-blog.name",
  "tag": "section",
  "disabled_on": {
    "groups": ["header", "footer"]
  },
  "settings": [
    {
      "type": "select",
      "id": "section_layout",
      "label": "t:settings_schema.global.settings.layout_mode.label",
      "default": "container",
      "options": [
        {
          "value": "container",
          "label": "t:settings_schema.global.settings.layout_mode.options__1.label"
        },
        {
          "value": "container-fluid",
          "label": "t:settings_schema.global.settings.layout_mode.options__2.label"
        }
      ]
    },
    {
      "type": "text",
      "id": "margin",
      "label": "t:sections.common.section_margin",
      "info": "t:sections.common.order_info"
    },
    {
      "type": "text",
      "id": "padding",
      "label": "t:sections.common.section_padding",
      "info": "t:sections.common.order_info"
    },
    {
      "type": "color",
      "id": "bg_color",
      "label": "t:sections.common.background_color"
    },
    {
      "type": "header",
      "content": "t:sections.common.blog_post"
    },
    {
      "type": "text",
      "id": "heading",
      "label": "t:sections.common.heading",
      "default": "Featured blog"
    },
    {
      "type": "blog",
      "id": "blog",
      "label": "t:sections.common.blog_post"
    },
    {
      "type": "text",
      "id": "items_resp",
      "label": "t:sections.common.items_per_row",
      "default": "3,2,1"
    },
    {
      "type": "checkbox",
      "id": "visibility_date",
      "label": "t:sections.featured-blog.settings.visibility_date",
      "default": true
    },
    {
      "type": "checkbox",
      "id": "show_excerpt",
      "label": "t:sections.featured-blog.settings.show_excerpt"
    },
    {
      "type": "checkbox",
      "id": "read_more_btn",
      "label": "t:sections.featured-blog.settings.read_more_button"
    },
    {
      "type": "range",
      "id": "blog_post_heading_fs",
      "label": "t:sections.featured-blog.settings.article_heading_fs",
      "step": 1,
      "min": 12,
      "max": 30,
      "default": 16,
      "unit": "px"
    },
    {
      "type": "range",
      "id": "blog_post_heading_fw",
      "label": "t:sections.featured-blog.settings.article_heading_fw",
      "step": 100,
      "min": 100,
      "max": 700,
      "default": 600
    },
    {
      "type": "checkbox",
      "id": "items_spacing",
      "label": "t:sections.common.items_spacing",
      "default": false
    },
    {
      "type": "range",
      "id": "limit",
      "label": "t:sections.common.limit",
      "step": 1,
      "min": 1,
      "max": 6,
      "default": 3
    },
    {
      "type": "header",
      "content": "t:sections.common.slider_settings"
    },
    {
      "type": "checkbox",
      "id": "enable_slider",
      "label": "t:sections.common.enable_slider",
      "default": true
    },
    {
      "type": "checkbox",
      "id": "loop",
      "label": "t:sections.common.loop",
      "default": false
    },
    {
      "type": "checkbox",
      "id": "controls",
      "label": "t:sections.common.controls",
      "default": true
    },
    {
      "type": "checkbox",
      "id": "dots",
      "label": "t:sections.common.dots",
      "default": true
    },
    {
      "type": "checkbox",
      "id": "autoplay",
      "label": "t:sections.common.autoplay",
      "default": false
    },
    {
      "type": "range",
      "id": "autoplay_timeout",
      "label": "t:sections.common.autoplay_timeout",
      "step": 0.1,
      "min": 2,
      "max": 10,
      "unit": "s",
      "default": 4
    }
  ],
  "blocks": [
    {
      "type": "heading-style",
      "limit": 1,
      "name": "t:sections.common.heading_style",
      "settings": [
        {
          "type": "select",
          "id": "section_heading_container_align",
          "label": "t:sections.common.alignment",
          "options": [
            {
              "value": "start",
              "label": "t:sections.common.left"
            },
            {
              "value": "center",
              "label": "t:sections.common.center"
            },
            {
              "value": "end",
              "label": "t:sections.common.right"
            }
          ]
        },
        {
          "type": "text",
          "id": "section_heading_margin",
          "label": "t:sections.common.margin",
          "info": "t:sections.common.order_info"
        },
        {
          "type": "color",
          "id": "section_heading_color",
          "label": "t:sections.common.text_color"
        },
        {
          "type": "text",
          "id": "section_heading_font_size",
          "label": "t:sections.common.font_size",
          "info": "t:sections.common.unit_px"
        },
        {
          "type": "range",
          "id": "section_heading_font_weight",
          "label": "t:sections.common.font_weight",
          "step": 100,
          "min": 100,
          "max": 900,
          "default": 600
        }
      ]
    }
  ],
  "presets": [
    {
      "name": "t:sections.featured-blog.name"
    }
  ]
}
{%- endschema -%}