{% extends 'layout/admin_page.html' %}

{% from 'message_box.html' import message_box %}

{% macro render_network_group_row(network_group, show_flag_col) %}
    <tr>
        <td>{{ network_group.name }}</td>
        <td>{{ network_group.description }}</td>
        <td>
            {% if network_group.networks|length < 3 %}
                {{ network_group.networks|join(', ') }}
            {% else %}
                <span data-qtip-html=".qtip-hidden-content">
                    <em>{% trans number=network_group.networks|length %}{{ number }} subnets{% endtrans %}</em>
                </span>
                <div class="qtip-hidden-content">
                    <ul>
                        {% for network in network_group.networks | sort %}
                            <li>{{ network }}</li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
        </td>
        {% if show_flag_col %}
            <td>
                {% if network_group.hidden %}
                    <i class="icon-eye-blocked"
                       title="{% trans %}Network cannot be added to ACLs{% endtrans %}"></i>
                {% else %}
                    <i class="icon-eye"></i>
                {% endif %}

                {% if network_group.attachment_access_override %}
                    <i class="icon-unlocked semantic-text success"
                       title="{% trans %}Network has unrestricted access to all attachments{% endtrans %}"></i>
                {% else %}
                    <i class="icon-lock semantic-text disabled"></i>
                {% endif %}
            </td>
        {% endif %}
        <td class="actions">
            <a href="#"
               class="icon-edit"
               data-ajax-dialog
               data-title
               title="{% trans %}Edit network{% endtrans %}"
               data-href="{{ url_for('.edit_group', network_group) }}"
               data-reload-after>
            </a>
            <a href="#"
               class="icon-remove"
               data-ajax-dialog
               data-title
               title="{% trans %}Delete network{% endtrans %}"
               data-href="{{ url_for('.delete_group', network_group) }}"
               data-reload-after>
            </a>
        </td>
    </tr>
{% endmacro %}

{%- block content %}
    {% set show_flag_col = network_groups|selectattr('hidden')|any or
                           network_groups|selectattr('attachment_access_override')|any %}

    {% call message_box('highlight', fixed_width=true) %}
        {% trans -%}
            IP networks can be used inside the ACLs of categories and events to
            limit their access to certain ranges of IP addresses.
        {%- endtrans %}
    {% endcall %}
    <div class="i-box">
        <div class="i-box-header">
            <div class="i-box-title">
                {% trans %}IP Networks{% endtrans %}
            </div>
            <div class="i-box-buttons toolbar right">
                <button class="i-button icon-plus"
                   data-ajax-dialog
                   data-title="{% trans %}Create an IP network{% endtrans %}"
                   data-href="{{ url_for('.create_group') }}"
                   data-reload-after>
                    {% trans %}New IP network{% endtrans %}
                </button>
            </div>
        </div>
        <div class="i-box-table-widget">
            <table class="i-table-widget ip-network-group">
                <thead>
                    <tr>
                        <th>{% trans %}Name{% endtrans %}</th>
                        <th>{% trans %}Description{% endtrans %}</th>
                        <th>{% trans %}IP subnets{% endtrans %}</th>
                        {% if show_flag_col %}
                            <th></th>
                        {% endif %}
                        <th class="actions"></th>
                    </tr>
                </thead>
                <tbody>
                    {% for network_group in network_groups %}
                        {{ render_network_group_row(network_group, show_flag_col) }}
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{%- endblock %}
