{% extends '_base.html' %}
{% load helpers %}

{% block content %}
<div class="pull-right">
    {% if perms.dcim.add_rack %}
        <a href="{% url 'dcim:rack_add' %}" class="btn btn-primary">
            <span class="fa fa-plus" aria-hidden="true"></span>
            Add a rack
        </a>
        <a href="{% url 'dcim:rack_import' %}" class="btn btn-info">
            <span class="fa fa-download" aria-hidden="true"></span>
            Import racks
        </a>
    {% endif %}
    {% include 'inc/export_button.html' with obj_type='racks' %}
</div>
<h1>{% block title %}Racks{% endblock %}</h1>
<div class="row">
	<div class="col-md-9">
        {% include 'utilities/obj_table.html' with bulk_edit_url='dcim:rack_bulk_edit' bulk_delete_url='dcim:rack_bulk_delete' %}
    </div>
    <div class="col-md-3">
		{% include 'inc/search_panel.html' %}
    </div>
</div>
{% endblock %}

{% block javascript %}
<script type="text/javascript">
$(document).ready(function() {

    var site_list = $('#id_site');
    var rack_group_list = $('#id_group_id');

    // Update rack group and rack options based on selected site
    site_list.change(function() {
        var selected_sites = $(this).val();
        if (selected_sites) {

            // Update rack group options
            rack_group_list.empty();
            $.ajax({
                url: netbox_api_path + 'dcim/rack-groups/?limit=500&site=' + selected_sites.join('&site='),
                dataType: 'json',
                success: function (response, status) {
                    $.each(response["results"], function (index, group) {
                        var option = $("<option></option>").attr("value", group.id).text(group.name);
                        rack_group_list.append(option);
                    });
                }
            });

        }
    });

});
</script>
{% endblock %}

