{% extends 'base.html' %}
{% load buttons %}
{% load custom_links %}
{% load helpers %}
{% load static %}
{% load plugins %}

{% block header %}
    <div class="row noprint">
        <div class="col-sm-8 col-md-9">
            <ol class="breadcrumb">
                <li><a href="{% url 'dcim:rack_list' %}">Racks</a></li>
                <li><a href="{% url 'dcim:rack_list' %}?site={{ rack.site.slug }}">{{ rack.site }}</a></li>
                <li>{{ rack }}</li>
            </ol>
        </div>
        <div class="col-sm-4 col-md-3">
            <form action="{% url 'dcim:rack_list' %}" method="get">
                <div class="input-group">
                    <input type="text" name="q" class="form-control" placeholder="Search racks" />
                    <span class="input-group-btn">
                        <button type="submit" class="btn btn-primary">
                            <span class="fa fa-search" aria-hidden="true"></span>
                        </button>
                    </span>
                </div>
            </form>
        </div>
    </div>
    <div class="pull-right noprint">
        {% plugin_buttons rack %}
        <a {% if prev_rack %}href="{% url 'dcim:rack' pk=prev_rack.pk %}"{% else %}disabled="disabled"{% endif %} class="btn btn-primary">
            <span class="fa fa-chevron-left" aria-hidden="true"></span> Previous Rack
        </a>
        <a {% if next_rack %}href="{% url 'dcim:rack' pk=next_rack.pk %}"{% else %}disabled="disabled"{% endif %} class="btn btn-primary">
            <span class="fa fa-chevron-right" aria-hidden="true"></span> Next Rack
        </a>
        {% if perms.dcim.add_rack %}
            {% clone_button rack %}
        {% endif %}
        {% if perms.dcim.change_rack %}
            {% edit_button rack %}
        {% endif %}
        {% if perms.dcim.delete_rack %}
            {% delete_button rack %}
        {% endif %}
    </div>
    <h1>{% block title %}Rack {{ rack }}{% endblock %}</h1>
    {% include 'inc/created_updated.html' with obj=rack %}
    <div class="pull-right noprint">
        <button class="btn btn-sm btn-default toggle-images" selected="selected">
            <span class="glyphicon glyphicon-check" aria-hidden="true"></span> Show Images
        </button>
        {% custom_links rack %}
    </div>
    <ul class="nav nav-tabs">
        <li role="presentation"{% if not active_tab %} class="active"{% endif %}>
            <a href="{{ rack.get_absolute_url }}">Rack</a>
        </li>
        {% if perms.extras.view_objectchange %}
            <li role="presentation"{% if active_tab == 'changelog' %} class="active"{% endif %}>
                <a href="{% url 'dcim:rack_changelog' pk=rack.pk %}">Change Log</a>
            </li>
        {% endif %}
    </ul>
{% endblock %}

{% block content %}
<div class="row">
	<div class="col-md-6">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>Rack</strong>
            </div>
            <table class="table table-hover panel-body attr-table">
                <tr>
                    <td>Site</td>
                    <td>
                        {% if rack.site.region %}
                            <a href="{{ rack.site.region.get_absolute_url }}">{{ rack.site.region }}</a>
                            <i class="fa fa-angle-right"></i>
                        {% endif %}
                        <a href="{% url 'dcim:site' slug=rack.site.slug %}">{{ rack.site }}</a>
                    </td>
                </tr>
                <tr>
                    <td>Group</td>
                    <td>
                        {% if rack.group %}
                            <a href="{% url 'dcim:rack_list' %}?site={{ rack.site.slug }}&group={{ rack.group.slug }}">{{ rack.group }}</a>
                        {% else %}
                            <span class="text-muted">None</span>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>Facility ID</td>
                    <td>{{ rack.facility_id|placeholder }}</td>
                </tr>
                <tr>
                    <td>Tenant</td>
                    <td>
                        {% if rack.tenant %}
                            {% if rack.tenant.group %}
                                <a href="{{ rack.tenant.group.get_absolute_url }}">{{ rack.tenant.group }}</a>
                                <i class="fa fa-angle-right"></i>
                            {% endif %}
                            <a href="{{ rack.tenant.get_absolute_url }}">{{ rack.tenant }}</a>
                        {% else %}
                            <span class="text-muted">None</span>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>Status</td>
                    <td>
                        {{ rack.get_status_display }}
                    </td>
                </tr>
                <tr>
                    <td>Role</td>
                    <td>
                        {% if rack.role %}
                            <a href="{{ rack.role.get_absolute_url }}">{{ rack.role }}</a>
                        {% else %}
                            <span class="text-muted">None</span>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>Serial Number</td>
                    <td>{{ rack.serial|placeholder }}</td>
                </tr>
                    <tr>
                        <td>Asset Tag</td>
                        <td>{{ rack.asset_tag|placeholder }}</td>
                    </tr>
                <tr>
                    <td>Devices</td>
                    <td>
                        <a href="{% url 'dcim:device_list' %}?rack_id={{ rack.id }}">{{ rack.devices.count }}</a>
                    </td>
                </tr>
                    <tr>
                        <td>Utilization</td>
                        <td>{% utilization_graph rack.get_utilization %}</td>
                    </tr>
            </table>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>Dimensions</strong>
            </div>
            <table class="table table-hover panel-body attr-table">
                <tr>
                    <td>Type</td>
                    <td>
                        {% if rack.type %}
                            {{ rack.get_type_display }}
                        {% else %}
                            <span class="text-muted">None</span>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>Width</td>
                    <td>{{ rack.get_width_display }}</td>
                </tr>
                <tr>
                    <td>Height</td>
                    <td>{{ rack.u_height }}U ({% if rack.desc_units %}descending{% else %}ascending{% endif %})</td>
                </tr>
                <tr>
                    <td>Outer Width</td>
                    <td>
                        {% if rack.outer_width %}
                            <span>{{ rack.outer_width }} {{ rack.get_outer_unit_display }}</span>
                        {% else %}
                            <span class="text-muted">&mdash;</span>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>Outer Depth</td>
                    <td>
                        {% if rack.outer_depth %}
                            <span>{{ rack.outer_depth }} {{ rack.get_outer_unit_display }}</span>
                        {% else %}
                            <span class="text-muted">&mdash;</span>
                        {% endif %}
                    </td>
                </tr>
            </table>
        </div>
        {% include 'inc/custom_fields_panel.html' with obj=rack %}
        {% include 'extras/inc/tags_panel.html' with tags=rack.tags.all url='dcim:rack_list' %}
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>Comments</strong>
            </div>
            <div class="panel-body rendered-markdown">
                {% if rack.comments %}
                    {{ rack.comments|render_markdown }}
                {% else %}
                    <span class="text-muted">None</span>
                {% endif %}
            </div>
        </div>
        {% if power_feeds %}
            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Power Feeds</strong>
                </div>
                <table class="table panel-body">
                    <tr>
                        <th>Panel</th>
                        <th>Feed</th>
                        <th>Status</th>
                        <th>Type</th>
                        <th>Utilization</th>
                    </tr>
                    {% for powerfeed in power_feeds %}
                        <tr>
                            <td>
                                <a href="{{ powerfeed.power_panel.get_absolute_url }}">{{ powerfeed.power_panel.name }}</a>

                            <td>
                                <a href="{{ powerfeed.get_absolute_url }}">{{ powerfeed.name }}</a>
                            </td>
                            <td>
                                <span class="label label-{{ powerfeed.get_status_class }}">{{ powerfeed.get_status_display }}</span>
                            </td>
                            <td>
                                <span class="label label-{{ powerfeed.get_type_class }}">{{ powerfeed.get_type_display }}</span>
                            </td>
                            {% with power_port=powerfeed.connected_endpoint %}
                                {% if power_port %}
                                    <td>{% utilization_graph power_port.get_power_draw.allocated|percentage:powerfeed.available_power %}</td>
                                {% else %}
                                    <td class="text-muted">N/A</td>
                                {% endif %}
                            {% endwith %}
                        </tr>
                    {% endfor %}
                </table>
            </div>
        {% endif %}
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>Images</strong>
            </div>
            {% include 'inc/image_attachments.html' with images=rack.images.all %}
            {% if perms.extras.add_imageattachment %}
                <div class="panel-footer text-right noprint">
                    <a href="{% url 'dcim:rack_add_image' object_id=rack.pk %}" class="btn btn-primary btn-xs">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                        Attach an image
                    </a>
                </div>
            {% endif %}
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>Reservations</strong>
            </div>
            {% if reservations %}
                <table class="table table-hover panel-body">
                    <tr>
                        <th>Units</th>
                        <th>Tenant</th>
                        <th>Description</th>
                        <th></th>
                    </tr>
                    {% for resv in reservations %}
                        <tr>
                            <td>
                                <a href="{{ resv.get_absolute_url }}">{{ resv.unit_list }}</a>
                            </td>
                            <td>
                                {% if resv.tenant %}
                                    <a href="{{ resv.tenant.get_absolute_url }}">{{ resv.tenant }}</a>
                                {% else %}
                                    <span class="text-muted">None</span>
                                {% endif %}
                            </td>
                            <td>
                                {{ resv.description }}<br />
                                <small>{{ resv.user }} &middot; {{ resv.created }}</small>
                            </td>
                            <td class="text-right noprint">
                                {% if perms.dcim.change_rackreservation %}
                                    <a href="{% url 'dcim:rackreservation_edit' pk=resv.pk %}?return_url={{ rack.get_absolute_url }}" class="btn btn-warning btn-xs" title="Edit reservation">
                                        <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>
                                    </a>
                                {% endif %}
                                {% if perms.dcim.delete_rackreservation %}
                                    <a href="{% url 'dcim:rackreservation_delete' pk=resv.pk %}?return_url={{ rack.get_absolute_url }}" class="btn btn-danger btn-xs" title="Delete reservation">
                                        <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                                    </a>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            {% else %}
                <div class="panel-body text-muted">None</div>
            {% endif %}
            {% if perms.dcim.add_rackreservation %}
                <div class="panel-footer text-right noprint">
                    <a href="{% url 'dcim:rackreservation_add' %}?rack={{ rack.pk }}&return_url={{ rack.get_absolute_url }}" class="btn btn-primary btn-xs">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                        Add a reservation
                    </a>
                </div>
            {% endif %}
        </div>
        {% plugin_left_page rack %}
	</div>
    <div class="col-md-6">
        <div class="row" style="margin-bottom: 20px">
            <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="rack_header">
                    <h4>Front</h4>
                </div>
                {% include 'dcim/inc/rack_elevation.html' with face='front' %}
            </div>
            <div class="col-md-6 col-sm-6 col-xs-12">
                <div class="rack_header">
                    <h4>Rear</h4>
                </div>
                {% include 'dcim/inc/rack_elevation.html' with face='rear' %}
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>Non-Racked Devices</strong>
            </div>
            {% if nonracked_devices %}
                <table class="table table-hover panel-body">
                    <tr>
                        <th>Name</th>
                        <th>Role</th>
                        <th>Type</th>
                        <th>Parent</th>
                    </tr>
                    {% for device in nonracked_devices %}
                        <tr{% if device.device_type.u_height %} class="warning"{% endif %}>
                            <td>
                                <a href="{% url 'dcim:device' pk=device.pk %}">{{ device }}</a>
                            </td>
                            <td>{{ device.device_role }}</td>
                            <td>{{ device.device_type.display_name }}</td>
                            <td>
                                {% if device.parent_bay %}
                                    <a href="{{ device.parent_bay.device.get_absolute_url }}">{{ device.parent_bay }}</a>
                                {% else %}
                                    <span class="text-muted">&mdash;</span>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            {% else %}
                <div class="panel-body text-muted">None</div>
            {% endif %}
            {% if perms.dcim.add_device %}
                <div class="panel-footer text-right noprint">
                    <a href="{% url 'dcim:device_add' %}?site={{ rack.site.pk }}&rack={{ rack.pk }}" class="btn btn-primary btn-xs">
                        <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                        Add a non-racked device
                    </a>
                </div>
            {% endif %}
        </div>
        {% plugin_right_page rack %}
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        {% plugin_full_width_page rack %}
    </div>
</div>
{% endblock %}

{% block javascript %}
<script src="{% static 'js/rack_elevations.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}
