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

    Macro for rendering a form widget in default style.

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

{%- macro render_form_item_default(form_item, with_errors = True, placeholder = None) %}
                                <div class="form-group{% if form_item.errors %}{{ ' has-error' }}{% endif %}">
                                    {{ render_form_label_help(form_item) }}
                                    {{ form_item(class_='form-control' + (' is-invalid' if with_errors and form_item.errors else ''), placeholder=placeholder, **kwargs) }}
    {%- if with_errors %}
        {%- for err in form_item.errors %}
                                    <span class="form-text form-error invalid-feedback">{{ get_icon('form-error') }} {{ err }}</span>{%- if not loop.last %}<br>{%- endif %}
        {%- endfor %}
    {%- endif %}
                                </div>
{%- endmacro %}


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

    Macro for rendering a form select widget with selectpicker class.

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

{%- macro render_form_item_select(form_item, with_errors = True) %}
                                <div class="form-group{% if form_item.errors %}{{ ' has-error' }}{% endif %}">
                                    {{ render_form_label_help(form_item) }}
                                    {{ form_item(class_='form-control selectpicker' + (' is-invalid' if with_errors and form_item.errors else ''),**{'data-none-selected-text':_('Nothing selected')}) }}
    {%- if with_errors %}
        {%- for err in form_item.errors %}
                                    <span class="form-text form-error invalid-feedback">{{ get_icon('form-error') }} {{ err }}</span>{%- if not loop.last %}<br>{%- endif %}
        {%- endfor %}
    {%- endif %}
                                </div>
{%- endmacro %}


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

    Macro for rendering a form widget with datetime selection class.

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

{%- macro render_form_item_datetime(form_item, ident, with_errors = True) %}
                                <div class="form-group{% if form_item.errors %}{{ ' has-error' }}{% endif %}">
                                    {{ render_form_label_help(form_item) }}
                                    <div id="{{ ident }}" class="input-group date">
                                        {{ form_item(class_='form-control' + (' is-invalid' if with_errors and form_item.errors else '')) }}
                                        <span class="input-group-text">
                                            {{ get_icon('calendar') }}
                                        </span>
                                    </div>
                                    <div class="text-muted">
                                        <small>
                                            {{ _('Note: Your selected timezone is &quot;<em>%(tz)s</em>&quot;', tz = session.timezone) | safe }}
                                        </small>
                                    </div>
    {%- if with_errors %}
        {%- for err in form_item.errors %}
                                    <span class="form-text form-error text-danger">{{ get_icon('form-error') }} {{ err }}</span>{%- if not loop.last %}<br>{%- endif %}
        {%- endfor %}
    {%- endif %}
                                </div>
{%- endmacro %}


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

    Macro for rendering a static form widget.

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

{%- macro render_form_item_static(form_item_label, form_item_value) %}
                                <div class="form-group">
                                    <label class="col-form-label">{{ form_item_label }}</label>
                                    <p class="form-control-plaintext">{{ form_item_value }}</p>
                                </div>
{%- endmacro %}


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

    Macro for rendering a radio button form widget.

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

{%- macro render_form_item_radiobutton(form_item, with_errors = True, equally_big = False) %}
                                <div class="form-group{% if form_item.errors %}{{ ' has-error' }}{% endif %}">
                                    <div>
                                        {{ render_form_label_help(form_item) }}
                                    </div>
                                    <div class="btn-group d-flex {{ 'flex-grow-1' if equally_big else '' }}" data-bs-toggle="buttons">
    {%- for subfield in form_item %}
                                        {{ subfield(class_='btn-check' + (' is-invalid' if with_errors and form_item.errors else '')) }} {{ subfield.label(class_='btn btn-outline-secondary' + (' w-100' if equally_big else '')) }}
    {%- endfor %}

                                    </div>
    {%- if with_errors %}
        {%- for err in form_item.errors %}
                                    <span class="form-text form-error invalid-feedback">{{ get_icon('form-error') }} {{ err }}</span>{%- if not loop.last %}<br>{%- endif %}
        {%- endfor %}
    {%- endif %}
                                </div>
{%- endmacro %}


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

    Macro for rendering a list of errors associated with given form widget.

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

{%- macro render_form_errors(errors, bound_to_input=false) %}
    {%- if errors %}
        {%- for err in errors %}
<span class="form-text form-error {% if bound_to_input %}invalid-feedback{% else %}text-danger{% endif %}">{{ get_icon('form-error') }} {{ err }}</span>{%- if not loop.last %}<br>{%- endif %}
        {%- endfor %}
    {%- endif %}
{%- endmacro %}

{%- macro render_form_errors_labeled(item, bound_to_input=false) %}
    {%- if errors %}
        {%- for err in item.errors %}
<div><span class="form-text form-error {% if bound_to_input %}invalid-feedback{% else %}text-danger{% endif %}">{{ get_icon('form-error') }} <strong>{{ item.label.text }}</strong> {{ err }}</span></div>
        {%- endfor %}
    {%- endif %}
{%- endmacro %}

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

    Macro for rendering a label with given form widget.

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

{%- macro render_form_label_help(item, icon='', placement = 'top') %}
                                    <div>
                                        {{ icon }}
    {%- if item.description %}
                                        <div class="popover-hover-container float-end form-help mb-1">
                                            <div class="popover-hover" role="button" data-popover-content="#fhpo-{{ item.label.field_id }}">
                                                <a class="btn btn-xs btn-sm btn-info text-light">
                                                    {{ get_icon('help') }}
                                                </a>
                                                <div id="fhpo-{{ item.label.field_id }}" 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">
                                                                <p>
                                                                    {{ item.description | safe }}
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
    {%- endif %}
                                        {{ item.label(class='form-label') }}
                                    </div>
{%- endmacro %}

{%- macro render_form_label_help_html(item, icon='', placement = 'top') %}
                                <div>
                                    {{ icon }}
                                    <div class="popover-hover-container float-end form-help mb-1">
                                        <div class="popover-hover" role="button" data-popover-content="#fhpo-{{ item.label.field_id }}">
                                            <a class="btn btn-xs btn-sm btn-info text-light">
                                                {{ get_icon('help') }}
                                            </a>
                                            <div id="fhpo-{{ item.label.field_id }}" 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">
        {%- if item.description %}
                                                            <p>
                                                                {{ item.description | safe }}
                                                            </p>
        {%- endif %}
                                                            {{ caller() }}
        {%- if hawat_current_view.has_help %}
                                                            <p>
                                                                {{ macros_page.render_help_link(icon = 'reference', text = _('more help'), tooltip_text = '') }}
                                                            </p>
        {%- endif %}
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    {{ item.label(class_='form-label') }}
                                </div>
{%- endmacro %}

{%- macro render_help_idea_reference() %}
                                    <p>
                                        {{ _('For more in-depth information about the <abbr title="Intrusion Detection Extensible Alert">IDEA</abbr> event format please study official <a href="%(url)s" target="_blank">documentation webpage</a>.', url = 'https://idea.cesnet.cz/en/definition') }}
                                    </p>
{%- endmacro %}

{%- macro render_help_datetime() %}
                                        <p>
                                            {{ _('User may change his/her timezone preferences in his/her <a href="%(url)s">account settings</a>. Alternatively you may use unambiguous explicit UTC timestamps in the format <code>YYYY-MM-DDThh:mm:ssZ</code>. UTC timestamps are particularly usefull for pre-generated URLs, that are independent of the user preferences.', url = url_for('users.me')) }}
                                        </p>
{%- endmacro %}

{%- macro render_help_currenttz() %}
                                    <div class="text-muted">
                                        <small>
                                            {{ _('Note: Your selected timezone is &quot;<em>%(tz)s</em>&quot;', tz = session.timezone) | safe }}
                                        </small>
                                    </div>
{%- endmacro %}
