{# LIBRARIES #}
{{ attach_library('altcom_ajax/altcom_ajax') }}
{{ attach_library('ui_patterns/libraries.bs_collapse') }}
{{ attach_library('ui_patterns/libraries.bs_buttons') }}
{{ attach_library('ui_patterns/jc_dropdown.jc_base') }}
{{ attach_library('ui_patterns/jc_dropdown.jc_dropdown_lib') }}

{# VARIABLES #}
{% set parent = parent|default('') %}
{% set uid = uid|default('') %}
{% set machine_name = item.machineName|replace({'&': ''}) %}
{% if uid %}
  {% set uid = "#{uid}-#{machine_name}-#{item.id}" %}
{% else %}
  {% set uid = "#{machine_name}" %}
{% endif %}

{% set pid = pid|default('') %}
{% if pid %}
  {% set uid = "#{pid}-#{uid}" %}
{% endif %}

{% set attributes = create_attribute()
  .setAttribute('id', "jcml-#{uid}")
  .setAttribute('data-bs-toggle', 'collapse')
  .setAttribute('data-bs-target', "#jcmi-#{uid}")
  .setAttribute('data-alt-jct', machine_name)
  .setAttribute('data-alt-jcc', is_child)
  .setAttribute('data-alt-jcp', parent)
  .setAttribute('role', 'button')
  .setAttribute('aria-expanded', 'false')
  .setAttribute('aria-controls', "jcmi-#{uid}")
  .setAttribute('tabindex', '0')
%}

{# If the item has children, loading these #}
{% if not item.children %}
  {% set ajax_attrs = altcom_ajax_attrs_array(
    "journey_check_route_details",
    "jc-mpi-#{uid}",
    {"route_id": item.id, 'uid': uid, 'pid': pid}, 1, 0, 1)
  %}
  {% for k, v in ajax_attrs %}
    {% set attributes = attributes.setAttribute(k, v) %}
  {% endfor %}
{% endif %}

{# TEMPLATE #}
<div class="jc-modal-item status-{{ item.status }} {{ cols|default('col-md-12') }}">
  <!-- Collapse trigger -->
  <h2 class="item">
    {% set show_link = (item.status == 'good') ? false : true %}
    {% set show_link = (item.showDetails) ? true : show_link %}
    {% set show_link = (item.hasIncidents) ? true : show_link %}
    {% set show_link = (item.children) ? true : show_link %}

    {% set text %}
      {% set statusMessage %}
        <span class='status'>{{ item.statusText }}</span>
      {% endset %}
      {% if item.statusDescription %}
        {% set statusMessage %}
          <span class="jcs-affected"> - {{ item.statusDescription|raw }}</span>
        {% endset %}
      {% endif %}
      {{ item.name }} {{ statusMessage|raw }}
    {% endset %}

    {% include "@atoms/tm-links/icon-bookended/icon-bookended.twig" with {
      "text": text|raw,
      "status": item.status,
      "class": item.status ~ ' service-update-anchor beliag belirf aat-cancel',
      "attr":show_link ? attributes : [],
      "url": show_link ? "#jcmi-#{uid}" : null,
      "toggle": show_link ? 'sign' : null,
    } %}
  </h2>
  <!-- / Collapse trigger -->

  <!-- Collapse area -->
  <div id="jcmi-{{ uid }}"
       class="service-group route-details collapse"
       data-alt-toggle="state"
       data-alt-target="collapse"
       aria-expanded="false">
    <!-- Details -->
    <div id="jc-mpi-{{ uid }}">
      {% if item.children %}
        {% for childItem in item.children %}
          {% include '@organisms/tm-jc/jc-modal/jc-modal-items/journey-check-item.html.twig' with {
            'item': childItem,
            'uid': machine_name,
            'pid': pid,
            'parent': "jcml-#{uid}",
          } %}
        {% endfor %}
      {% else %}
        <div class="service-loader incident">
          {% include "@molecules/tm-bs/spinner/spinner.twig" with {
            'size': 'sm', 'show_text': true
          } only %}
        </div>
      {% endif %}
    </div>
    <!-- / Details -->
  </div>
  <!-- / Collapse area -->
</div>
