{% extends "adagios_standard.html" %}
{% block title %}Add new Object{% endblock %}
{% block smallheader %}Add{% endblock %}
{% block largeheader %}{{ object_type }}{% endblock %}
{% block nav1 %}<a href="{% url objectbrowser %}">Object Browser</a>{% endblock %}



{% block content %}
    <p>
    Alright, before adding a new {{  object_type }} we need some basic info:
    </p>
    <form action="#" method="post" class="form-horizontal">{% csrf_token %}
        {% include "bootstrap_fields.html" with fields=form %}
        <div class="form-actions">
            <button class="btn btn-primary" type="submit">Save {{ object_type }} and let me edit it further</button>
        </div>
    </form>
    <div id="template_services">
    </div>

{% endblock %}

{% block footer %}

    {{ block.super }}
    <!-- DNS lookup for the address field -->
    <script>
        // When host_name has been entered, automatically look up the ip address
        $('#id_host_name').focusout(function() {
            if ($('#id_address').val() == "") {
                $.getJSON('/rest/pynag/json/dnslookup',
                        {
                            host_name: $(this).val()
                        },
                        function(data) {
                            $('#id_address').val(data['addresslist'][0]);
                        }
                );
            }
        });

        // When use field is updated, automatically fill out service description
        $('#id_use').change(function() {
            selected_parents = '';
            $('#id_use :selected').each( function(i) {
                var query = {
                    'name': this.text,
                    'object_type': 'service',
                    'with_fields': 'service_description,check_command'
                };
                adagios.rest.pynag.get_objects(query)
                        .done( function(data) {
                            data = data[0];
                            if (data.service_description  == null) {
                                return;
                            }
                            $('#id_service_description').val(data.service_description);

                        })
                        .fail( function() { alert('fail');});
            });
        });

    </script>
{% endblock %}