{#- ----------------------------------------------------------------------------

    Macro for rendering the breadcrumb menu for currently active view.

----------------------------------------------------------------------------- #}

{%- macro render_breadcrumbs(context_item = None) %}
    {%- set breadcrumbs_menu = hawat_current_view.get_breadcrumbs_menu() %}
    {%- if breadcrumbs_menu %}
                    <!-- Breadcrumbs menu widget - BEGIN ---------------------->
                    <ol class="breadcrumb">
        {%- for menu_item in breadcrumbs_menu.get_entries(item = context_item) %}
                        <li class="breadcrumb-item{% if menu_item.is_active(request, item = context_item) %} active{% endif %}">
            {%- if menu_item.is_active(request, item = context_item) %}
                            {{ menu_item.get_title(item = context_item) }}
            {%- else %}
                            <a href="{{ menu_item.get_url(item = context_item) }}">
                                {{ menu_item.get_title(item = context_item) }}
                            </a>
            {%- endif %}
                        </li>
        {%- endfor %}
                    </ol>
                    <!-- Breadcrumbs menu widget - END ------------------------>
    {%- else %}
                    <!-- Breadcrumbs menu widget is empty on this page -------->
    {%- endif %}
{%- endmacro %}


{#- ----------------------------------------------------------------------------

    Macro for rendering the action menu for currently active view.

----------------------------------------------------------------------------- #}

{%- macro render_menu_actions(context_item = None, action_menu = None) %}
    {%- if not action_menu %}
    {%- set action_menu = hawat_current_view.get_action_menu() %}
    {%- endif %}
    {%- if action_menu %}
        {%- set menu_item_list = action_menu.get_entries(item = context_item) %}
        {%- if menu_item_list %}
                        <!-- Action menu widget - BEGIN ----------------------->
                        <div class="btn-toolbar" role="toolbar" aria-label="{{ _('Action toolbar') }}">
                            <div class="btn-group btn-group-sm" role="group" aria-label="{{ _('Action buttons') }}">
            {%- for menu_item in menu_item_list %}
                {%- if menu_item.type == 'submenu' %}
                                <a data-bs-toggle="dropdown" role="button" aria-haspopup="true" href="#" class="btn btn-secondary btn-sm dropdown-toggle{% if menu_item.cssclass %} {{ menu_item.cssclass }}{% endif %}" aria-expanded="false">
                                    <span{% if menu_item.get_legend(item = context_item) %} data-bs-toggle="tooltip" title="{{ menu_item.get_legend(item = context_item) }}"{% endif %}>
                                        {% if menu_item.get_icon(item = context_item) %}{{ get_icon(menu_item.get_icon(item = context_item)) }}{% endif %}{% if menu_item.get_title(item = context_item) %}{% if menu_item.resptitle %}<span class="d-none d-lg-inline"> {{ menu_item.get_title(item = context_item) }}</span>{% else %} {{ menu_item.get_title(item = context_item) }}{% endif %}{% endif %} <i class="fas fa-fw fa-bars"></i>
                                    </span>
                                </a>
                                <ul class="dropdown-menu{% if menu_item.align_right %} dropdown-menu-right{% endif %}">
                    {%- for submenu_item in menu_item.get_entries(item = context_item) %}
                                    <li>
                                        <a href="{{ submenu_item.get_url(item = context_item) }}"{% if submenu_item.get_legend(item = context_item) %} data-bs-toggle="tooltip" title="{{ submenu_item.get_legend(item = context_item) }}"{% endif %} class="dropdown-item{% if menu_item.cssclass %} {{ menu_item.cssclass }}{% endif %}">
                                            {% if submenu_item.get_icon(item = context_item) %}{{ get_icon(submenu_item.get_icon(item = context_item)) }}{% endif %}{% if submenu_item.get_title(item = context_item) %}{% if submenu_item.resptitle %}<span class="d-none d-lg-inline"> {{ submenu_item.get_title(item = context_item) }}</span>{% else %} {{ submenu_item.get_title(item = context_item) }}{% endif %}{% endif %}
                                        </a>
                                    </li>
                    {%- endfor %}
                                </ul>
               {%- else %}
                                <a role="button" class="btn btn-secondary btn-sm{% if menu_item.cssclass %} {{ menu_item.cssclass }}{% endif %}" href="{{ menu_item.get_url(item = context_item) }}"{% if menu_item.get_legend(item = context_item) %} data-bs-toggle="tooltip" title="{{ menu_item.get_legend(item = context_item) }}"{% endif %}>
                                    {% if menu_item.get_icon(item = context_item) %}{{ get_icon(menu_item.get_icon(item = context_item)) }}{% endif %}{% if menu_item.get_title(item = context_item) %}{% if menu_item.resptitle %}<span class="d-none d-lg-inline"> {{ menu_item.get_title(item = context_item) }}</span>{% else %} {{ menu_item.get_title(item = context_item) }}{% endif %}{% endif %}
                                </a>
                {%- endif %}
            {%- endfor %}
                            </div>
                        </div>
                        <!-- Action menu widget - END ------------------------->
        {%- endif %}
    {%- endif %}
{%- endmacro %}


{#- ----------------------------------------------------------------------------

    Macro for rendering the context item action menus.

----------------------------------------------------------------------------- #}

{%- macro render_menu_context_actions(context_item, action_menu = None, cssclass = '', kwargs = {}) %}
    {%- if not action_menu %}
    {%- set action_menu = hawat_current_view.get_context_action_menu() %}
    {%- endif %}
    {%- if action_menu %}
        {%- set menu_item_list = action_menu.get_entries(item = context_item, **kwargs) %}
        {%- if menu_item_list %}
                                    <!-- Item context action menu widget - BEGIN ------------------>
                                    <div class="btn-toolbar{% if cssclass %} {{ cssclass }}{% endif %}" role="toolbar" aria-label="{{ _('Item context action toolbar') }}">
                                        <div class="btn-group btn-group-sm m-1" role="group" aria-label="{{ _('Item context action buttons') }}">
                {%- for menu_item in menu_item_list %}
                    {%- if menu_item.type == 'submenu' %}
                                            <a data-bs-toggle="dropdown" role="button" aria-haspopup="true" href="#" class="btn btn-outline-dark btn-sm dropdown-toggle{% if menu_item.cssclass %} {{ menu_item.cssclass }}{% endif %}" aria-expanded="false">
                                                <span{% if menu_item.get_legend(item = context_item, **kwargs) %} data-bs-toggle="tooltip" title="{{ menu_item.get_legend(item = context_item, **kwargs) }}"{% endif %}>
                                                    {% if menu_item.get_icon(item = context_item, **kwargs) %}{{ get_icon(menu_item.get_icon(item = context_item, **kwargs)) }}{% endif %}{% if menu_item.get_title(item = context_item, **kwargs) %}{% if menu_item.resptitle %}<span class="d-none d-lg-inline"> {{ menu_item.get_title(item = context_item, **kwargs) }}</span>{% else %} {{ menu_item.get_title(item = context_item, **kwargs) }}{% endif %}{% endif %} <i class="fas fa-fw fa-bars"></i>
                                                </span>
                                            </a>
                                            <ul class="dropdown-menu{% if menu_item.align_right %} dropdown-menu-right{% endif %}">
                        {%- for submenu_item in menu_item.get_entries(item = context_item, **kwargs) %}
                                                <li>
                                                    <a href="{{ submenu_item.get_url(item = context_item, **kwargs) }}"{% if submenu_item.get_legend(item = context_item, **kwargs) %} data-bs-toggle="tooltip" title="{{ submenu_item.get_legend(item = context_item, **kwargs) }}"{% endif %}class="dropdown-item{% if menu_item.cssclass %} {{ menu_item.cssclass }}{% endif %}">
                                                        {% if submenu_item.get_icon(item = context_item, **kwargs) %}{{ get_icon(submenu_item.get_icon(item = context_item, **kwargs)) }}{% endif %}{% if submenu_item.get_title(item = context_item, **kwargs) %}{% if submenu_item.resptitle %}<span class="d-none d-lg-inline"> {{ submenu_item.get_title(item = context_item, **kwargs) }}</span>{% else %} {{ submenu_item.get_title(item = context_item, **kwargs) }}{% endif %}{% endif %}
                                                    </a>
                                                </li>
                        {%- endfor %}
                                            </ul>
                    {%- else %}
                                            <a role="button" class="btn btn-outline-dark btn-sm{% if menu_item.cssclass %} {{ menu_item.cssclass }}{% endif %}" href="{{ menu_item.get_url(item = context_item, **kwargs) }}"{% if menu_item.get_legend(item = context_item, **kwargs) %} data-bs-toggle="tooltip" title="{{ menu_item.get_legend(item = context_item, **kwargs) }}"{% endif %}>
                                                {% if menu_item.get_icon(item = context_item, **kwargs) %}{{ get_icon(menu_item.get_icon(item = context_item, **kwargs)) }}{% endif %}{% if menu_item.get_title(item = context_item, **kwargs) %}{% if menu_item.resptitle %}<span class="d-none d-lg-inline"> {{ menu_item.get_title(item = context_item, **kwargs) }}</span>{% else %} {{ menu_item.get_title(item = context_item, **kwargs) }}{% endif %}{% endif %}
                                            </a>
                    {%- endif %}
                {%- endfor %}
                                        </div>
                                    </div>
                                    <!-- Item context action menu widget - END -------------------->
        {%- endif %}
    {%- endif %}
{%- endmacro %}


{#- ----------------------------------------------------------------------------

    Macro for rendering the changelog record widget.

----------------------------------------------------------------------------- #}

{%- macro render_label_endpoint(endpoint) -%}
<span class="badge text-bg-{% if 'create' in endpoint %}success{% elif 'update' in endpoint %}primary{% elif 'delete' in endpoint %}danger{% elif 'enable' in endpoint %}warning{% elif 'disable' in endpoint %}info{% else %}secondary{% endif %}">{{ get_endpoint_icon(endpoint) }} {{ endpoint }}</span>
{%- endmacro -%}


{%- macro render_label_action(endpoint) -%}
<span class="badge text-bg-{% if 'create' in endpoint %}success{% elif 'update' in endpoint %}primary{% elif 'delete' in endpoint %}danger{% elif 'enable' in endpoint %}warning{% elif 'disable' in endpoint %}info{% else %}secondary{% endif %}" data-bs-toggle="tooltip" title="{{ endpoint.split('.')[-1] }}">{{ get_endpoint_icon(endpoint) }}</span>
{%- endmacro -%}


{%- macro render_label_module(endpoint) -%}
<span class="badge text-bg-secondary" data-bs-toggle="tooltip" title="{{ endpoint.split('.')[0] }}">{{ get_module_icon(endpoint) }}</span>
{%- endmacro -%}


{%- macro render_item_in_changelog(item) -%}
    {% set endpoint = '{}.show'.format(item.module) %}
    {% if check_endpoint_exists(endpoint) and item.operation in ('create', 'createfor', 'update', 'enable', 'disable') %}
        <a data-bs-toggle="tooltip"
           href="{{ url_for(endpoint, item_id = item.model_id ) }}"
           title="{{ _('View details of item &quot;%(item)s&quot;', item = '{}#{}'.format(item.model, item.model_id)) }}"
        >
            {{ item.model }}#{{ item.model_id }}
        </a>
    {% else %}
        {{ item.model }}#{{ item.model_id }}
    {% endif %}
{%- endmacro -%}


{%- macro render_changelog_records(items, context_action_menu, idprefix = 'changelog', hide_author = False, hide_item = False) -%}
                    <ul class="list-group list-group-hover">
                        {%- for item in items %}
                        <li class="list-group-item">
                            <p class="list-group-item-text">
                                <div class="float-end">
                                    {{ render_menu_context_actions(item, context_action_menu) }}
                                </div>
                                {{ render_label_module(item.endpoint) }}
                                {{ render_label_action(item.endpoint) }}
                                <strong>{{ babel_format_datetime(item.createtime) }}</strong> ({{ _('%(delta)s ago', delta = babel_format_timedelta(current_datetime_utc - item.createtime)) }})
                                {%- if not hide_author %}
                                &nbsp;|&nbsp;
                                    {%- if item.author %}
                                <strong>{{ _('Author') }}:</strong> <a data-bs-toggle="tooltip" href="{{ url_for('users.show', item_id = item.author.id ) }}" title="{{ _('View details of user account &quot;%(item)s&quot;', item = item.author.login) }}">
                                    {{ item.author.login }}
                                </a>
                                    {%- else %}
                                {{ _('<< system change >>') }}
                                    {%- endif %}
                                {%- endif %}
                                {%- if not hide_item %}
                                &nbsp;|&nbsp;
                                <strong>{{ _('Item') }}:</strong>
                                    {{ render_item_in_changelog(item) }}
                                {%- endif %}
                                &nbsp;|&nbsp;
                                <a class="btn btn-outline-dark btn-xs btn-sm"
                                   role="button"
                                   data-bs-toggle="collapse"
                                   href="#{{ idprefix }}-difference-{{ loop.index }}"
                                   aria-expanded="false" aria-controls="collapseExample"
                                >
                                    {{ _('Difference') }} <i class="fas fa-fw fa-bars"></i>
                                </a>
                                <div class="collapse" id="{{ idprefix }}-difference-{{ loop.index }}">
                                    <hr>
                                    <div class="border rounded" style="height: 20em; overflow: scroll;">
                                        {% highlight 'diff' %}{{ item.diff }}{% endhighlight %}
                                    </div>
                                </div>
                            </p>
                        </li>
                        {%- endfor %}
                    </ul>
{%- endmacro -%}

{%- macro render_popover_help_html(placement = 'top') %}
{%- set tmp_uuid4 = get_uuid4() %}
                                    <div class="popover-hover-container float-end form-help mb-1">
                                        <div class="popover-hover" role="button" data-popover-content="#hhpo-{{ tmp_uuid4 }}">
                                            <a class="btn btn-xs btn-sm btn-info text-light">
                                                {{ get_icon('help') }}
                                            </a>
                                            <div id="hhpo-{{ tmp_uuid4 }}" class="d-none">
                                                <div class="popover top">
                                                    <div class="popover-arrow" data-popper-arrow></div>
                                                    <h3 class="popover-title">
                                                        {{ get_icon('help') }} {{ item.label.text }}
                                                    </h3>
                                                    <div class="popover-content-outer">
                                                        <div class="popover-content">
                                                            {{ caller() }}
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
{%- endmacro %}


{%- macro render_help_link(icon = 'reference', text = _('Help'), tooltip_text = _('View full online documentation'), tooltip_placement = 'bottom', url_suffix = '', in_navbar = False) %}
<a href="https://713.gitlab-pages.cesnet.cz/mentat/mentat/master/html/_doclib/hawat_plugin_{{ hawat_current_view.module_name }}.html#section-hawat-plugin-{{ hawat_current_view.module_name }}-features-{{ hawat_current_view.get_view_name() }}{{ url_suffix }}" target="_blank"{% if tooltip_text %} data-bs-toggle="tooltip" data-bs-placement="{{ tooltip_placement }}" title="{{ tooltip_text }}"{% endif %}{% if in_navbar %}class="nav-link"{% endif %}>{% if icon %}{{ get_icon(icon) }}{% endif %}{% if text %} {{ text }}{% endif %}</a>
{%- endmacro %}
