{# LIBRARIES These must include the libraries for nre-ad.twig if loading via Ajax. #}
{{ attach_library('ui_patterns/libraries.bs_collapse') }}
{{ attach_library('ui_patterns/libraries.bs_spinners') }}
{{ attach_library('ui_patterns/nre_ad_boards.nre_ad_lib') }}
{{ attach_library('ui_patterns/reveal_all.reveal_all_lib') }}
{{ attach_library('ui_patterns/libraries.bs_tab') }}
{{ attach_library('ui_patterns/bs-tabs.tabs_lib') }}
{{ attach_library('ui_patterns/alert.alert_lib') }}

{# VARIABLES #}
{% set hide_auto_update = hide_auto_update|default(0) %}
{% set style = style|default('1') %}
{% set last_updated_text = last_updated_text|default('Last updated:') %}
{% set initial_state = initial_state|default('off') %}
{% set auto_disabled = (initial_state == 'on') ? '0' : '1' %}
{% set seconds = seconds|default(0) %}
{% set tabbed = tabbed|default(false) %}
{% set tabs_style = tabs_style|default('full') %}
{% set row_limit = row_limit|default(10) %}
{% set preview_limit = preview_limit|default(false) %}
{% set next_limit = next_limit|default(false) %}
{% set hide_departs = hide_departs|default(false) %}
{% set hide_arrivals = hide_arrivals|default(false) %}
{% set tid = 'nre-ldb-stations-' ~ station %}
{% set on_string = "<i class='fas fa-power-off'></i> Turn on auto update" %}
{% set off_string = "<i class='fas fa-sync' id='#{ tid }-spin'></i> Turn off auto update" %}
{% set wrapper_class = "style-#{ style }" %}
{% set loader_class = '' %}

{% if hide_auto_update %}
  {% set wrapper_class = wrapper_class ~ ' no-auto-update' %}
{% endif %}

{% if style != 1 %}
  {% set loader_class = 'h2 aa-update-invert' %}
{% endif %}

{% set ajax_attrs = altcom_ajax_attrs_string(
  'nre_ldb_stations',
  tid,
  { 'station': station,
    'calling_at': calling_at,
    'tabbed': tabbed,
    'tabs_style': tabs_style,
    'row_limit': row_limit,
    'preview_limit': preview_limit,
    'next_limit': next_limit,
    'hide_departs': hide_departs,
    'hide_arrivals': hide_arrivals,
  }, 1, 1, 0, seconds|number_format)
%}

{# TEMPLATE #}
<div id="nre-ad-block" class="{{ wrapper_class }}">
  <span class="visually-hidden" {{ ajax_attrs|raw }}>Load stations</span>

  {% if hide_auto_update %}

    <div class="updated">
      <span class="{{ loader_class }}">{{ altcom_ajax_update(tid, last_updated_text) }}</span>
    </div>

  {% else %}

    <div class="table-meta">
      {{ altcom_ajax_toggle(tid, 'sgl', initial_state, on_string, off_string, 'light') }}
      <span class="{{ loader_class }}">{{ altcom_ajax_update(tid, last_updated_text) }}</span>
    </div>

  {% endif %}

  <div id="{{ tid }}" data-altcom-ajax-disabled="{{ auto_disabled }}">
    {% include "@molecules/tm-bs/spinner/spinner.twig" with {
      'size': 'sm',
      'show_text': true,
      'status': 'light',
    } only %}
    {% if aadc %}<div class="aadc">{{ aadc }}</div>{% endif %}
  </div>

</div>
