{% extends "edit_object.html" %}
{% block title %}{{ block.super }}{% endblock %}

{% block head %}{{ block.super }}{% endblock %}


{% block sidebar %}{{ block.super }}{% endblock %}

{% block adagios_hausinn %}
    {{ block.super }}
    {% if status.text and status.plugin_output %}
        <div class="row-fluid" id="top_status">
            <div class="span1"><div class="servicestate label {{ status.css_label }}" id="state">{{ status.text }}</div></div>
            <div class="span11"><div id="top_status_output" class="label label-inverse">{{ status.plugin_output}}</div></div>
        </div>
    {% endif %}
{% endblock %}

{% block content %}

    {% if my_object %}
        <!-- wrapper for scrollable items -->
        <ul class="nav nav-tabs" id="objecttab">
            <li><a href="#general" data-toggle="tab">General</a></li>
            <li><a href="#monitoring" data-toggle="tab">Monitoring</a></li>
            <li><a href="#notifications_tab" data-toggle="tab">Notifications</a></li>
            <li id="macros_li"><a href="#macros_tab" data-toggle="tab">Macros</a></li>
            <li><a href="#advanced" data-toggle="tab">Advanced</a></li>
        </ul>
        <form class="form-horizontal" action="#" method="POST">{% csrf_token %}
            <div class="tab-content">
                <div class="tab-pane" id="general">
                    <fieldset>
                        {% include "bootstrap_field.html" with field=form.host_name %}
                        {% include "bootstrap_field.html" with field=form.service_description %}
                        {% include "bootstrap_field.html" with field=form.servicegroups %}
                        {% include "bootstrap_field.html" with field=form.use %}
                    </fieldset>

                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">Save changes</button>
                    </div>
                </div>

                <div class="tab-pane" id="monitoring">
                    <div class="control-group">
                        <label class="control-label" for="check_command">
                            Check Command
                        </label>
                        <div class="controls" id="check_command">
                            <input id="check_command_actual_entry" type="hidden" name="check_command" value="{{ form.check_command.value }}"/>
                            <select style="width: 500px;" data-placeholder="Please select a check command"  id="check_command_select">
                                <option value=""></option>
                                {% for i in command_names %}
                                    <option value="{{ i }}" {% if i == check_command %}selected="selected"{% endif %}>{{ i }}</option>
                                {% endfor %}
                            </select>
                            <table style="margin-left: 10px; margin-top: 5px; min-width: 495px;">
                                <tbody id="check_command_argument_table"></tbody>
                                <tbody id="service_macros_table"></tbody>
                                <tbody id="other_attributes_table"></tbody>
                            </table>

                            <label class="help-block">Check command is a previously defined command, that runs the actual plugin. The input boxes below are automatically generated based on what check command you select. In the command_line previews below you can see the actual command that nagios will run.</label>
                        </div>

                    </div>



                    <div id="check_command_editor_tables">
                        <div class="control-group">
                            <label class="control-label" for="original_command_line_div">
                                Command Line
                            </label>
                            <div class="controls" id="original_command_line_div">
                                <pre  id="original_command_line"></pre>
                            </div>
                        </div>


                        <div class="control-group">
                            <label class="control-label" for="effective_command_line">
                                Expanded Command Line
                            </label>
                            <div class="controls" id="effective_command_line">
                                <pre id="command_line_preview">Loading...</pre>
                            </div>
                        </div>

                    </div>
                        {% include "bootstrap_field.html" with field=form.max_check_attempts %}
                        {% include "bootstrap_field.html" with field=form.check_interval %}
                        {% include "bootstrap_field.html" with field=form.active_checks_enabled %}
                        {% include "bootstrap_field.html" with field=form.passive_checks_enabled %}
                        {% include "bootstrap_field.html" with field=form.check_period %}
                    </fieldset>

                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">Save changes</button>
                        <button class="btn">Cancel</button>
                    </div>
                </div>

                <div class="tab-pane" id="notifications_tab">
                    <fieldset>
                        {% include "bootstrap_field.html" with field=form.notification_interval %}
                        {% include "bootstrap_field.html" with field=form.first_notification_delay %}
                        {% include "bootstrap_field.html" with field=form.notification_period %}
                        {% include "bootstrap_field.html" with field=form.notification_options %}
                        {% include "bootstrap_field.html" with field=form.contact_groups %}
                        {% include "bootstrap_field.html" with field=form.contacts %}
                    </fieldset>
                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">Save changes</button>
                        <button class="btn">Cancel</button>
                    </div>

                </div>
                <div class="tab-pane" id="macros_tab">
                    <div class="well">
                        This tab contains custom macros (also called "custom variables") for this {{ my_object.object_type }}.
                    </div>
                    <fieldset>
                        {% for field in form %}
                            {%  if field.label|slice:":1" == "_" %}
                                {% include "bootstrap_field.html" with field=field %}
                            {% endif %}
                        {% endfor %}
                    </fieldset>
                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">Save changes</button>
                    </div>

                </div>
        </form>
            <div class="tab-pane" id="advanced">
                <form action="{%  url objectbrowser.views.advanced_edit my_object.id %}" method="post" class="form-horizontal">{% csrf_token %}

                    {% include "bootstrap_fields.html" with fields=advanced_form %}
                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">Submit</button>
                    </div>
                </form>
            </div>
        </div>

    {% else %}
        <p>Object not found </p>
    {% endif %}
{% endblock %}



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

                    if ( $('div.tab-pane#macros_tab fieldset').children().length ) {
                        $('li#macros_li').show();
                    }


                    // Activate a clever check_command_editor

                    editor = adagios.objectbrowser.CheckCommandEditor( {
                        host_name: "{{ my_object.host_name }}",
                        check_command: "{{ check_command }}",
                        name: "{{ my_object.name }}",
                        service_description : "{{ my_object.service_description }}"
                    });
                    editor.generate_input_fields();
                    // When check_command is changed, lets recreate the input fields:
                    $("#check_command_select").change( function() {
                        editor.check_command = $('#check_command_select').find(":selected").text();
                        editor.generate_input_fields();
                    });

                    // When the special save button is called, we call save
                    $("#check_command_save_button").click( function() {
                        editor.save_check_command();
                    });


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