{% load helpers %}
<tr class="interface{% if not iface.enabled %} danger{% elif iface.cable %} {{ iface.cable.get_status_class }}{% elif iface.is_virtual %} warning{% endif %}" id="interface_{{ iface.name }}">

    {# Checkbox #}
    {% if perms.dcim.change_interface or perms.dcim.delete_interface %}
        <td class="pk">
            <input name="pk" type="checkbox" value="{{ iface.pk }}" />
        </td>
    {% endif %}

    {# Icon and name #}
    <td class="text-nowrap">
        <span title="{{ iface.get_type_display }}">
            <i class="fa fa-fw fa-{% if iface.mgmt_only %}wrench{% elif iface.is_lag %}align-justify{% elif iface.is_virtual %}circle{% elif iface.is_wireless %}wifi{% else %}exchange{% endif %}"></i>
            <a href="{{ iface.get_absolute_url }}">{{ iface }}</a>
        </span>
        {% if iface.mac_address %}
            <br/><small class="text-muted">{{ iface.mac_address }}</small>
        {% endif %}
    </td>

    {# LAG #}
    <td>
        {% if iface.lag %}
            <a href="#interface_{{ iface.lag }}" class="label label-primary" title="{{ iface.lag.description }}">{{ iface.lag }}</a>
        {% endif %}
    </td>

    {# Description/tags #}
    <td>
        {% if iface.description %}
            {{ iface.description }}<br/>
        {% endif %}
        {% for tag in iface.tags.all %}
            {% tag tag %}
        {% empty %}
            {% if not iface.description %}&mdash;{% endif %}
        {% endfor %}
    </td>

    {# MTU #}
    <td>{{ iface.mtu|default:"&mdash;" }}</td>

    {# 802.1Q mode #}
    <td>{{ iface.get_mode_display|default:"&mdash;" }}</td>

    {# Cable #}
    <td class="text-nowrap">
        {% if iface.cable %}
            <a href="{{ iface.cable.get_absolute_url }}">{{ iface.cable }}</a>
            {% if iface.cable.color %}
            <span class="inline-color-block" style="background-color: #{{ iface.cable.color }}">&nbsp;</span>
            {% endif %}
            <a href="{% url 'dcim:interface_trace' pk=iface.pk %}" class="btn btn-primary btn-xs" title="Trace">
                <i class="fa fa-share-alt" aria-hidden="true"></i>
            </a>
        {% else %}
            <span class="text-muted">&mdash;</span>
        {% endif %}
    </td>

    {# Connection or type #}
    {% if iface.is_lag %}
        <td colspan="2" class="text-muted">
            LAG interface<br />
            <small class="text-muted">
                {% for member in iface.member_interfaces.all %}
                    <a href="#interface_{{ member.name }}">{{ member }}</a>{% if not forloop.last %}, {% endif %}
                {% empty %}
                    No members
                {% endfor %}
            </small>
        </td>
    {% elif iface.is_virtual %}
        <td colspan="2" class="text-muted">Virtual interface</td>
    {% elif iface.is_wireless %}
        <td colspan="2" class="text-muted">Wireless interface</td>
    {% elif iface.connected_endpoint.name %}
        {# Connected to an Interface #}
        <td>
            <a href="{% url 'dcim:device' pk=iface.connected_endpoint.device.pk %}">
                {{ iface.connected_endpoint.device }}
            </a>
        </td>
        <td>
            <a href="{% url 'dcim:interface' pk=iface.connected_endpoint.pk %}">
                <span title="{{ iface.connected_endpoint.get_type_display }}">
                    {{ iface.connected_endpoint }}
                </span>
            </a>
        </td>
    {% elif iface.connected_endpoint.term_side %}
        {# Connected to a CircuitTermination #}
        {% with iface.connected_endpoint.get_peer_termination as peer_termination %}
            {% if peer_termination %}
                {% if peer_termination.connected_endpoint %}
                    <td>
                        <a href="{% url 'dcim:device' pk=peer_termination.connected_endpoint.device.pk %}">
                            {{ peer_termination.connected_endpoint.device }}
                        </a><br/>
                        <small>via <i class="fa fa-fw fa-globe" title="Circuit"></i>
                            <a href="{{ iface.connected_endpoint.circuit.get_absolute_url }}">
                                {{ iface.connected_endpoint.circuit.provider }}
                                {{ iface.connected_endpoint.circuit }}
                            </a>
                        </small>
                    </td>
                    <td>
                        {{ peer_termination.connected_endpoint }}
                    </td>
                {% else %}
                    <td colspan="2">
                        <a href="{% url 'dcim:site' slug=peer_termination.site.slug %}">
                            {{ peer_termination.site }}
                        </a>
                        via <i class="fa fa-fw fa-globe" title="Circuit"></i>
                        <a href="{{ iface.connected_endpoint.circuit.get_absolute_url }}">
                            {{ iface.connected_endpoint.circuit.provider }}
                            {{ iface.connected_endpoint.circuit }}
                        </a>
                    </td>
                {% endif %}
            {% else %}
                <td colspan="2">
                    <i class="fa fa-fw fa-globe" title="Circuit"></i>
                    <a href="{{ iface.connected_endpoint.circuit.get_absolute_url }}">
                        {{ iface.connected_endpoint.circuit.provider }}
                        {{ iface.connected_endpoint.circuit }}
                    </a>
                </td>
            {% endif %}
        {% endwith %}
    {% else %}
        <td colspan="2">
            <span class="text-muted">Not connected</span>
        </td>
    {% endif %}

    {# Buttons #}
    <td class="text-right text-nowrap noprint">
        {% if show_interface_graphs %}
            {% if iface.connected_endpoint %}
                <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#graphs_modal" data-obj="{{ device.name }} - {{ iface.name }}" data-url="{% url 'dcim-api:interface-graphs' pk=iface.pk %}" title="Show graphs">
                    <i class="glyphicon glyphicon-signal" aria-hidden="true"></i>
                </button>
            {% endif %}
        {% endif %}
        {% if perms.ipam.add_ipaddress %}
            <a href="{% url 'ipam:ipaddress_add' %}?interface={{ iface.pk }}&return_url={{ device.get_absolute_url }}" class="btn btn-xs btn-success" title="Add IP address">
                <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
            </a>
        {% endif %}
        {% if perms.dcim.change_interface %}
            {% if iface.cable %}
                {% include 'dcim/inc/cable_toggle_buttons.html' with cable=iface.cable %}
            {% elif iface.is_connectable and perms.dcim.add_cable %}
                <span class="dropdown">
                    <button type="button" class="btn btn-success btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <span class="glyphicon glyphicon-resize-small" aria-hidden="true"></span>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-right">
                        <li><a href="{% url 'dcim:interface_connect' termination_a_id=iface.pk termination_b_type='interface' %}?return_url={{ device.get_absolute_url }}">Interface</a></li>
                        <li><a href="{% url 'dcim:interface_connect' termination_a_id=iface.pk termination_b_type='front-port' %}?return_url={{ device.get_absolute_url }}">Front Port</a></li>
                        <li><a href="{% url 'dcim:interface_connect' termination_a_id=iface.pk termination_b_type='rear-port' %}?return_url={{ device.get_absolute_url }}">Rear Port</a></li>
                        <li><a href="{% url 'dcim:interface_connect' termination_a_id=iface.pk termination_b_type='circuit-termination' %}?return_url={{ device.get_absolute_url }}">Circuit Termination</a></li>
                    </ul>
                </span>
            {% endif %}
            <a href="{% url 'dcim:interface_edit' pk=iface.pk %}?return_url={{ device.get_absolute_url }}" class="btn btn-info btn-xs" title="Edit interface">
                <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>
            </a>
        {% endif %}
        {% if perms.dcim.delete_interface %}
            {% if iface.connection or iface.circuit_termination %}
                <button class="btn btn-danger btn-xs" disabled="disabled">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                </button>
            {% else %}
                <a href="{% url 'dcim:interface_delete' pk=iface.pk %}?return_url={{ device.get_absolute_url }}" class="btn btn-danger btn-xs" title="Delete interface">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                </a>
            {% endif %}
        {% endif %}
    </td>
</tr>

{% with ipaddresses=iface.ip_addresses.all %}
    {% if ipaddresses %}
        <tr class="ipaddresses">
            {# Placeholder #}
            {% if perms.dcim.change_interface or perms.dcim.delete_interface %}
                <td></td>
            {% endif %}

            {# IP addresses table #}
            <td colspan="9" style="padding: 0">
                <table class="table table-condensed interface-ips">
                    <thead>
                        <tr class="text-muted">
                            <th class="col-md-3">IP Address</th>
                            <th class="col-md-2">Status/Role</th>
                            <th class="col-md-3">VRF</th>
                            <th class="col-md-3">Description</th>
                            <th class="col-md-1"></th>
                        </tr>
                    </thead>
                    {% for ip in iface.ip_addresses.all %}
                        <tr>

                            {# IP address #}
                            <td>
                                <a href="{% url 'ipam:ipaddress' pk=ip.pk %}">{{ ip }}</a>
                            </td>

                            {# Primary/status/role #}
                            <td>
                                {% if device.primary_ip4 == ip or device.primary_ip6 == ip %}
                                    <span class="label label-success">Primary</span>
                                {% endif %}
                                <span class="label label-{{ ip.get_status_class }}">{{ ip.get_status_display }}</span>
                                {% if ip.role %}
                                    <span class="label label-{{ ip.get_role_class }}">{{ ip.get_role_display }}</span>
                                {% endif %}
                            </td>

                            {# VRF #}
                            <td>
                                {% if ip.vrf %}
                                    <a href="{% url 'ipam:vrf' pk=ip.vrf.pk %}" title="{{ ip.vrf.rd }}">{{ ip.vrf.name }}</a>
                                {% else %}
                                    <span class="text-muted">Global</span>
                                {% endif %}
                            </td>

                            {# Description #}
                            <td>
                                {% if ip.description %}
                                    {{ ip.description }}
                                {% else %}
                                    <span class="text-muted">&mdash;</span>
                                {% endif %}
                            </td>

                            {# Buttons #}
                            <td class="text-right text-nowrap noprint">
                                {% if perms.ipam.change_ipaddress %}
                                    <a href="{% url 'ipam:ipaddress_edit' pk=ip.pk %}?return_url={{ device.get_absolute_url }}" class="btn btn-info btn-xs">
                                        <i class="glyphicon glyphicon-pencil" aria-hidden="true" title="Edit IP address"></i>
                                    </a>
                                {% endif %}
                                {% if perms.ipam.delete_ipaddress %}
                                    <a href="{% url 'ipam:ipaddress_delete' pk=ip.pk %}?return_url={{ device.get_absolute_url }}" class="btn btn-danger btn-xs">
                                        <i class="glyphicon glyphicon-trash" aria-hidden="true" title="Delete IP address"></i>
                                    </a>
                                {% endif %}
                            </td>

                        </tr>
                    {% endfor %}
                </table>
            </td>
        </tr>
    {% endif %}
{% endwith %}
