{# LIBRARIES #}
{{ attach_library('ui_patterns/sa_calendar.sa_calendar_lib') }}
{% import '@organisms/tm-sa/sa-calendar/calendar.macros.twig' as calendar_macros %}

{# VARIABLES #}
{% set btn_next_icon = global_icon_right|default('fas fa-chevron-right') %}
{% set btn_prev_icon = global_icon_left|default('fas fa-chevron-left') %}
{% if next_icon %}{% set btn_next_icon = next_icon %}{% endif %}
{% if prev_icon %}{% set btn_prev_icon = prev_icon %}{% endif %}
{% set btn_next_class = btn_next_class|default('btn btn-btn-lg btn-outline-default') %}
{% set btn_next_class = btn_next_class ~ ' next-month' %}
{% set btn_next_text = "#{next_text} <i class='" ~ btn_next_icon ~ " belir'></i>" %}
{% set btn_next_attributes = create_attribute().setAttribute('rel', 'nofollow') %}
{% set btn_prev_class = btn_prev_class|default('btn btn-btn-lg btn-outline-default') %}
{% set btn_prev_class = btn_prev_class ~ ' prev-month' %}
{% set btn_prev_text = "<i class='" ~ btn_prev_icon ~ " belil'></i> #{prev_text}" %}
{% set btn_prev_attributes = create_attribute().setAttribute('rel', 'nofollow') %}
{% set use_ajax = use_ajax|default(false) %}

{% set btn_prev_url = "?date=#{prev_from}--#{prev_to}" %}
{% set btn_next_url = "?date=#{next_from}--#{next_to}" %}

{% if use_ajax %}
  {# Next button ajax call attributes #}
  {% set btn_next_url = "#" %}
  {% set ajax_attrs = altcom_ajax_attrs_array(
    'service_alterations_calendar_ajax_wrapper',
    'calendar-wrapper',
    {'ajax_date': "#{next_from}--#{next_to}"},
    0)
  %}
  {% for k, v in ajax_attrs %}
    {% set btn_next_attributes = btn_next_attributes.setAttribute(k, v) %}
  {% endfor %}
  {# Prev button ajax call attributes #}
  {% set btn_prev_url = "#" %}
  {% set ajax_attrs = altcom_ajax_attrs_array(
    'service_alterations_calendar_ajax_wrapper',
    'calendar-wrapper',
    {'ajax_date': "#{prev_from}--#{prev_to}"},
    0)
  %}
  {% for k, v in ajax_attrs %}
    {% set btn_prev_attributes = btn_prev_attributes.setAttribute(k, v) %}
  {% endfor %}
{% endif %}

{# TEMPLATE #}
<div id="sac-pager" class="pager">
  {% if prev_show %}
    {% include "@atoms/tm-links/standard/standard.twig" with {
      "text": btn_prev_text ~ ' <i class="fas fa-circle-notch d-none spin"></i>',
      "url": btn_prev_url,
      "class": btn_prev_class,
      'attributes': btn_prev_attributes
    } only %}
  {% else %}
    {% set btn_prev_attributes = btn_prev_attributes.setAttribute('disabled', 'disabled') %}
    {% include "@atoms/tm-links/standard/standard.twig" with {
      "text": btn_prev_text,
      "url": '#',
      "class": btn_prev_class ~ ' disabled',
    } only %}
  {% endif %}
  <h2 class="month-name">{{ calendar_start|date('F Y') }}</h2>
  {% if next_show %}
    {% include "@atoms/tm-links/standard/standard.twig" with {
      "text": '<i class="fas fa-circle-notch d-none spin"></i> ' ~ btn_next_text,
      "url": btn_next_url,
      "class": btn_next_class,
      'attributes': btn_next_attributes
    } only %}
  {% else %}
    {% set btn_next_attributes = btn_next_attributes.setAttribute('disabled', 'disabled') %}
    {% include "@atoms/tm-links/standard/standard.twig" with {
      "text": btn_next_text,
      "url": '#',
      "class": btn_next_class ~ ' disabled',
    } only %}
  {% endif %}
</div>

<div id="sac-calendar" class="dtod" role="grid">
  <ul id="days-row" class="ulc">
    <li class="lic title-cell">Sun</li>
    <li class="lic title-cell">Mon</li>
    <li class="lic title-cell">Tue</li>
    <li class="lic title-cell">Wed</li>
    <li class="lic title-cell">Thur</li>
    <li class="lic title-cell">Fri</li>
    <li class="lic title-cell">Sat</li>
  </ul>
  {{ calendar_macros.listDays(calendar_start|date('N'), calendar_start, calendar_end, count, alterations) }}
</div>

{% include '@organisms/tm-sa/sa-calendar/calendar-table.twig' with {
  'alterations': alterations,
  'title': 'Alterations for the month',
  'hide': true,
  'tid': 'calendar',
} only %}
