{% set title = post.post_title %}
{% set content = post.post_content %}
{% set date = fn.get_the_date() %}

{% set terms = fn.get_the_Terms(post.ID, 'category') %}
{% set tags = fn.get_the_tags() %}
{% set linkPage = fn.get_permalink() %}

{% set author = fn.get_the_author_meta('user_nicename', post.post_author) %}

{% set thumb = fn.get_post_thumbnail_id(post.ID) %}
{# og #}
{% set og_title = title %}
{% set og_desc = post.post_excerpt %}
{% set og_thumb = fn.get_the_post_thumbnail_url(post.ID, 'thumbnail') %}
{% set og_url = fn.get_the_permalink(post.ID) %}
{% if not og_desc %}{% set og_desc = post.post_excerpt
    ? post.post_excerpt
    : post.post_content | slice(0, 180) %}
{% endif %}


{% extends 'm_layout/base.twig' %}

{% block main %}

<article class="article {% if related %}article--hasRelated{% endif %}" m-pack-single>
  <section class="article__header">

    {% if title %}
      <h1 class="h2">{{title}}</h1>
    {% endif %}

    {% if date or author or terms %}
    <div class="article__bundle">

      <div class="article__bundle-info">

        {% if date %}
          <time datetime="{{dateTime}}">{{ date }}</time>
        {% endif %}

        {% if author %}
          <small class="author"> | {{author}}</small>
        {% endif %}

        {% if terms %}
        <strong>
            {% if not author and date or author %}<span class="separate">|</span>{% endif %}
            {% for term in terms %}
              <a href="{{fn.get_term_link(term.term_id) }}">{{term.name}}</a>{% if loop.index < terms|length %}, {% endif %}
            {% endfor %}
        </strong>
        {% endif %}

        {% if tags %}
            <small>,
              {% for tag in tags %}
              {% set tagLink = fn.get_tag_link(tag.term_id) %}
              <a href="{{tagLink}}">{{tag.name}}</a>
              {% endfor %}
            </small>
        {% endif %}

      </div>


      {% include 'm_pages/components/sharing.twig' with {url: linkPage} only %}

    </div>



    {% endif %}

  </section>


  {# HERO #}
  {% if thumb %}
    <section class="article__hero">
      {% include 'm_components/widgets/picture.twig' with {image: thumb, title: title} only %}
    </section>
  {% endif %}


  {# CONTENT #}
  {% if content %}
    <section class="article__content m_content ">
      {{ fn.apply_filters('the_content', content)|raw }}
    </section>
  {% endif %}


  {# SHARING #}
  <section class="article__share">
    <h3 class="h4">Share</h3>
    {% include 'm_pages/components/sharing.twig' with {url: linkPage} only %}
  </section>

</article>



{# RELATED #}
{% if related %}
  <aside class="m_panel article-related">
    <div class="m_headline">
      <h2 class="m_headline__title">{{post.post_type}} linked with the same category</h2>
    </div>
    <div class="grid">
      {% for item in related %}

        {% set related_title = item.post_title %}
        {% set related_content = item.post_excerpt
          ? item.post_excerpt
          : fn.wp_trim_words(item.post_content, 18, '...') %}
        {% set related_date = item.post_date | date('F d, Y') %}
        {% set related_time = item.post_date|date('Y-m-d') %}

        {% set image = fn.get_post_thumbnail_id(item.ID) %}

        <a href="{{fn.get_the_permalink(item.ID)}}" class="m_panel__block">
          <div class="m_panel__image">
            {% include 'm_components/widgets/picture.twig' with {image: image, title: title} only %}
          </div>
          <div class="m_panel__content">
            <time datetime="{{related_time}}">{{ related_date }}</time>
            <h2 class="h4">{{related_title}}</h2>
            {% if related_content %}
              <p>{{related_content|striptags}}</p>
            {% endif %}
            <span class="btn__arrow btn__arrow--right">Read more</span>
          </div>
        </a>

      {% endfor %}
    </div>
  </aside>
{% endif %}

{% endblock %}
