{% extends "base_status.html" %}
{% load adagiostags %}
{% block title %}{{ my_object.display_name }}{% endblock %}
{% block smallheader %}
    <a href="{% url status.views.status_detail %}?host_name={{ host.name }}">
        <small>{{ host.display_name }}</small>
    </a>
{% endblock %}
{% block largeheader %}
    {{ service.display_name }}
    <!--
    <span class="state_{{ my_object.state }}">{{ my_object.status }}</span>
    -->
{% endblock %}
{% block nav1 %}<a href="{% url status.views.services %}">status</a>{% endblock %}
{% block nav2 %}{{ host_name }} {% if primary_object.object_type == 'service' %}- {{ service_description }}{% endif %}{% endblock %}


{% block view_buttons %}{% endblock %}

{% block content %}
    <div class="span9" id="maincontent_left_side">

    <ul class="nav nav-tabs" id="objecttab">
        <li><a href="#general" data-toggle="tab">General</a></li>
        <li><a href="#information" data-toggle="tab">Information</a></li>
        <li><a href="#events" data-toggle="tab" id="history_link">History</a></li>
        <li><a href="#graphs" data-toggle="tab" id="graphs_link">Graphs</a></li>
        <li><a href="#custom_variables" data-toggle="tab">Custom Variables</a></li>
        <li><a href="#other" data-toggle="tab">Debug</a></li>
    </ul>
    <div class="tab-content">

    <div class="tab-pane active" id="general">
    {% if my_object %}
        <table class="table">
            <tr>
                <td>Status</td>
                <td>
                    <div class="circle state_{{ my_object.state }}"></div> {{ my_object.status }} for {{ my_object.last_state_change|timestamp|timesince }}
                    {% if my_object.state != 0 %}
                        {% if my_object.acknowledged == 1 %}(has been acknowledged)
                        {% else %}(not acknowledged)
                        {% endif %}
                    {% endif %}
                </td>
            </tr>
            <tr >
                <td>Host Name</td>
                <td><a href="{% url status.views.status_detail %}?host_name={{ host.name  }}" title="{{ host.plugin_output }}">
                    <div class="circle state_{{ host.state }}"></div>
                    {{ host.display_name }}
                </a>
                </td>
            </tr>
            <tr >
                <td>Host Address</td>
                <td>{{ host.address }}</td>
            </tr>
            <tr>
                <td>Host Alias</td>
                <td>{{ host.alias }}</td>
            </tr>
            {% if service %}
            <tr >
                <td>Service Name</td>
                <td>{{ service.display_name }}</td>
            </tr>
            {% endif %}
            <tr>
                <td>Check output</td>
                <td>
                    {% if my_object.long_plugin_output %}
                    <pre id="plugin_output">{{ my_object.plugin_output }}
{{ my_object.long_plugin_output }}</pre>
                    {% else %}
                    <pre id="plugin_output">{{ my_object.plugin_output }}</pre>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td>Performance  Metrics</td>
                <td >
                    {% if perfdata %}
                        <table border=1 class=table style="border-color: #ffffff;" >
                            <tr>
                                <th></th>
                                <th>label</th>
                                <th>value</th>
                                <th>warn</th>
                                <th>crit</th>
                                <th>unit</th>
                                <th>min</th>
                                <th>max</th>
                                <th>pnp</th>
                            </tr>
                            {% for i in perfdata %}
                                <tr>
                                    <td class="nowrap"><div class="circle state_{{ i.get_status }}"> </div></td>
                                    <td>{{ i.label }}</td>
                                    <td>{{ i.value }} </td>
                                    <td>{{ i.warn }}  </td>
                                    <td>{{ i.crit }}  </td>
                                    <td> {{ i.uom }}</td>
                                    <td>{{ i.min }}   </td>
                                    <td>{{ i.max }}   </td>
                                    <td>
                                        <a href="{% url pnp.views.pnp %}/image?host={{ host.name }}&source={{ i.i }}&srv={{ service_description }}" class="preview" title="{{ service_description }} {{ i.label }}"><i class="glyph glyph-charts"></i></a>
                                    </td>
                                </tr>
                            {% endfor %}
                        </table>
                    {% else %}
                        No Perfdata available.
                    {% endif %}

                </td>
            </tr>


        </table>
    {% endif %}
    </div>

    <div class="tab-pane" id="other">
        <h4>Information available via livestatus:</h4>
        <table class="table-condensed table">
            {% for k,v in my_object.items %}
            <tr>
                <td>                {{ k }}
                </td>
                <td>
                {{ v }}
                </td>
            </tr>
            {% endfor %}
        </table>

        <!-- servicestatus has to be here for the action buttons to work -->
        <div  id="servicestatus">
            <table>
                <tbody>
                <tr>
                    <td class="selectable">
                        <input class="chkbox {{ service.tags }}"  style="margin: -2px; padding: 0px;" type="checkbox" checked="checked" name="{{ my_object.object_type }}" value="{{ my_object.short_name }}">
                        <input type="hidden" class="input_hostname" name="host_name" value="{{ host_name }}">
                        <input type="hidden" class="input_service_description" name="service_description" value="{{ service_description }}">

                    </td>
                </tr>
                </tbody>
            </table>
        </div>

    </div>

    <div class="tab-pane" id="information">
        <p>
            This {{ my_object.object_type }} is checked every <a href="#">{{ my_object.check_interval }}</a> minutes.

            When a problem occurs it will be checked <a href="#">{{ my_object.max_check_attempts }}</a> times

            every <a href="#">{{ my_object.retry_interval }}</a> minutes before going to
            hard state and start sending out notifications.
        </p>

        {% if my_object.notes %}
        <p>
            <b>Notes:</b> {{ my_object.notes }}
        </p>
        {% endif %}

        <table class=table>
            <tr>
                <td>Last Check</td>
                <td>
                    {% if my_object.last_check %}
                        {{ my_object.last_check|timestamp|date:'Y-m-d H:i' }}
                         <span class="pull-right">({{ my_object.last_check|timestamp|timesince  }} ago)</span>
                    {% else %}
                        check has never been executed.
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td>Next Check</td>
                <td>
                    {% if my_object.next_check %}
                        {{ my_object.next_check|timestamp|date:'Y-m-d H:i' }}
                        <span class="pull-right">({{ my_object.next_check|timestamp|timeuntil }} from now)</span>
                    {% else %}
                        Check is not scheduled to be executed.
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td>Check Period</td>
                <td>
                    {% if my_object.check_period %}
                    <a href="{% url edit_by_shortname "timeperiod" my_object.check_period %}">{{ my_object.check_period }}</a>
                    {% else %}
                    Unknown
                    {% endif %}
                    <span class="pull-right">
                        (currently {% if my_object.in_check_period %}inside{% else %}outside{% endif %} check_period)
                    </span>
                </td>
            </tr>
            <tr>
                <td>Notification Period</td>
                <td>
                    {% if my_object.notification_period %}
                    <a href="{% url edit_by_shortname "timeperiod" my_object.notification_period %}">{{ my_object.notification_period }}</a>
                    {% else %}
                    Unknown
                    {% endif %}
                    <span class="pull-right">
                        (currently {% if my_object.in_notification_period %}inside{% else %}outside{% endif %} notification period)
                    </span>
                </td>
            </tr>
            <tr>
                <td>
                    Check command
                </td>
                <td id="effective_check_command"> Loading...</td>
            </tr>
            <tr></tr>
            <tr>
                <td>
                    Downtimes:
                </td>
                <td>
                    {% if my_object.downtimes_with_info %}
                        <div id="comments">
                            {% for i in my_object.downtimes_with_info %}
                                <div class="alert alert-info">
                                    <button class="close delete_downtime" data-dismiss="alert" title="Delete Downtime {{ i.0 }}"
                                            onclick="$.getJSON('{% url rest/status %}json/delete_downtime?downtime_id={{ i.0 }}{% if object_type == 'host' %}&is_service=False{% endif %}');">
                                        <i class="glyph-bin"></i></button>
                                    <i class="glyph-user"></i> <a href="{% url status.views.contact_detail i.1 %}">{{ i.1 }}</a>: {{ i.2 }}
                                </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        No Downtimes are scheduled.
                    {% endif %}

                </td>
            </tr>
            <tr>
                <td>
                    Comments:
                </td>
                <td>
                    <div id="downtimes">
                        {% if my_object.comments_with_info %}
                            {% for i in my_object.comments_with_info %}
                                <div class="alert alert-info">
                                    <button class="close delete_comment" data-dismiss="alert" title="Delete comment {{ i.0 }}" onclick="$.getJSON('{% url rest/status %}json/delete_comment?host_name={{ host_name }}&service_description={{ service_description }}&comment_id={{ i.0 }}');">
                                        <i class="glyph-bin"></i></button>
                                    <i class="glyph-user"></i> <a href="{% url status.views.contact_detail i.1 %}">{{ i.1 }}</a>: {{ i.2 }}
                                </div>
                            {% endfor %}
                        {% else %}
                            <p>This {{ my_object.object_type }} has no comments.</p>
                        {% endif %}
                        <div id="div_add_comment" class="alert alert-info">
                                    <input type="hidden" name="author" id="id_author" value="{{ remote_user }}"/>
                                    <input type="hidden" name="host_name" id="id_host_name" value="{{ host_name }}"/>
                                    {% if my_object.object_type == 'service' %}
                                        <input type="hidden" name="service_description" id="id_service_description" value='{{ service_description }}'/>
                                    {% endif %}
                                    <input type="hidden" name="persistent" value="0" id="id_persistent" />
                                    <i class="glyph-user"></i> <a href="">{{ remote_user }}</a>: <input name=comment id="write_a_comment" placeholder="Write a comment...">
                        </div>
                    </div>

                </td>
            </tr>
            <tr>
                <td>Network Parents</td>
                <td>
                    {% if network_parents %}
                        <table>
                            <tbody>
                            {% for parent_level in network_parents %}
                                <tr>
                                    {% for parent in parent_level %}
                                        <td>
                                            <div class="alert state_{{ parent.state }}">
                                                <a href="{% url status.views.status_detail %}?host_name={{ parent.name }}">{{ parent.name }}</a>
                                            </div>
                                        </td>
                                    {% endfor %}
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    {% else %}
                        This {{ my_object.object_type }} has no configured network parents.
                    {% endif %}
            </tr>
        </table>
    </div>

    <div class="tab-pane" id="custom_variables">
        Some checks have custom variables defined. These can be defined for example to define warning/critical thresholds
        for custom plugins.
        <p>
        </p>
        {% if my_object.custom_variables %}
        <table class="table table-condensed">
            <tr>
                <th>
                    Variable Name
                </th>
                <th>
                    Value
                </th>
            </tr>
            {% for k,v in my_object.custom_variables.items %}
                <tr>
                <td>{{ k }}</td>
                <td>
                    <div class="pull-left">
                        <input class="editable_field" name="_{{ k }}" value="{{ v }}"/>
                    </div>
                    <div id="_{{ k }}_status"></div>

                </td>
                </tr>
            {% endfor %}
        </table>
        {% else %}
        This check has no custom variables defined.
        {% endif %}
        <p></p>
        {% if my_object.host_custom_variables %}
            Host <a href="{% url status.views.status_detail %}?host_name={{ host_name }}#tab_custom_variables">{{ host_name }}</a> also has some custom variables.
            <table class="table table-condensed">
                <tr>
                    <th>
                        Variable Name
                    </th>
                    <th>
                        Value
                    </th>
                </tr>
                {% for k,v in my_object.host_custom_variables.items %}
                    <tr>
                        <td>{{ k }}</td>
                        <td>{{ v }}
                    </tr>
                {% endfor %}
            </table>
        {% else %}
        {% endif %}
        See also <a href="{% url adagios.misc.views.edit_check_command %}?host_name={{ host_name }}&service_description={{ service_description }}&check_command={{ my_object.check_command }}">Configure check command</a>


    </div>
    <div class="tab-pane" id="events">
        <div id="logdiv">This tab should update when the tab is clicked</div>
    </div>


    <div class="tab-pane" id="graphs">
            {% if graph_urls %}
                <div id="graphs_tab" class="tabbable"> <!-- Only required for left/right tabs -->
                    <ul  class="nav nav-pills" id="graphs_ul">
                        <li><a href="#hours" data-toggle="tab" id="test_id" name="test_name">4 Hours</a></li>
                        <li><a href="#day" data-toggle="tab" id="25hours">25 Hours</a></li>
                        <li><a href="#week" data-toggle="tab">One Week</a></li>
                        <li><a href="#month" data-toggle="tab">One Month</a></li>
                        <li><a href="#year" data-toggle="tab">One Year</a></li>
                    </ul>

                    <div class="tab-content" id="graphs_content">

                        <div class="tab-pane graph-pane" id="hours">
                            <!-- Javascript will put images here if applicable -->
                        </div>

                        <div class="tab-pane active" id="day">
                            <!-- Javascript will put images here if applicable -->
                        </div>

                        <div class="tab-pane" id="week">
                            <!-- Javascript will put images here if applicable -->
                        </div>

                        <div class="tab-pane" id="month">
                            <!-- Javascript will put images here if applicable -->
                        </div>

                        <div class="tab-pane" id="year">
                            <!-- Javascript will put images here if applicable -->
                        </div>

                    </div>
                </div>
            {% else %}
                {% if pnp4nagios_error %}
                    <div class="alert alert-warning">
                        We got an error while trying to talk with PNP4nagios: {{ pnp4nagios_error }}
                    </div>
                    Please check that:
                    <ul>
                    <li><a href="{{ pnp_url }}">pnp4nagios</a> is installed and working</li>
                    <li><i>PNP Filepath</i> is correctly configured under <a href="{% url misc.views.settings %}">settings</a></li>
                    <li>User running nagios is a member of the unix groups apache or www-data</li>
                    <li>Click <a href="{% url pnp.views.pnp "json" %}">here</a> to see pnp4nagios output</li>
                    </ul>
                {% endif %}
                No Performance data is available for this {{ my_object.object_type }}.
            {% endif %}
    </div>
    </div>
</div>
<div class="span3" id="maincontent_right_side">
    {% if host.services_with_info %}
        <h5>Services on this host</h5>
        <ul class="nav nav-list">
            {% for i in host.services_with_info %}
                <li >
                    <a title="{{ i.3 }}" href="{% url status.views.status_detail %}?host_name={{ host.name  }}&service_description={{ i.0 }}">
                        <div class="circle state_{{ i.1 }}"></div> {{ i.0 }}
                    </a>
                </li>

            {% endfor %}
        </ul>
    {% endif %}

    <h5>External Links</h5>
    <ul class="nav nav-list">
        {% if my_object.action_url %}
            <li><a href="{{ my_object.action_url_expanded }}">Extra Actions</a></li>
        {% endif %}

        {% if my_object.notes_url %}
            <li><a href="{{ my_object.notes_url_expanded }}">Extra Notes</a></li>
        {% endif %}

        {% if my_object.host_action_url %}
            <li><a href="{{ my_object.host_action_url_expanded }}">Host Actions</a></li>
        {% endif %}

        {% if my_object.host_notes_url %}
            <li><a href="{{ my_object.host_notes_url_expanded }}">Host Notes</a></li>
        {% endif %}
    </ul>
    {% if hostgroup_name %}
        {% if hostgroups %}
            <h5>Is a member of hostgroup:</h5>
            <ul class="nav nav-list">
                {% for group in hostgroups %}
                    <li><a href="{% url status.views.status_hostgroup group.name %}">{{ group.name }}</a></li>
                {% endfor %}
            </ul>
        {% endif %}
    {% endif %}
    {% if host.parents %}
        <h5>Network Parents</h5>
        <ul class="nav nav-list">
            {% for i in host.parents %}
                <li><a href="{% url status.views.status_detail %}?host_name={{ i }}">
                    {{ i }}
                </a></li>
            {% endfor %}
        </ul>
    {% endif %}

    {% if host.groups %}
        <h5>Is a member of hostgroups</h5>
        <ul class="nav nav-list">
            {% for group in host.groups %}
                <li><a href="{% url status.views.status_hostgroup group %}">{{ group }}</a></li>
            {% endfor %}
        </ul>
    {% endif %}

    {% if my_object.contact_groups %}
        <h5>Is a member of contactgroups</h5>
        <ul class="nav nav-list">
            {% for group in my_object.contact_groups %}
                <li><a href="">{{ group }}</a></li>
            {% endfor %}
        </ul>
    {% endif %}

    {% if service.groups %}
        <h5>Is a member of servicegroups</h5>
        <ul class="nav nav-list">
            {% for group in service.groups %}
                <li><a href="">{{ group }}</a></li>
            {% endfor %}
        </ul>
    {% endif %}

</div>


{% endblock %}

{% block header %}
    {{ block.super }}
    <style>
    form {
        margin: 0px;
    }
    #maincontent_left_side {
        margin: 0px;
    }
    </style>
    <style>
        .table td {
            line-height: 10px;
        }
        #preview{
            position:absolute;
            border:1px solid #ccc;
            background:#333;
            padding:5px;
            display:none;
            color:#fff;
        }
        /* avoid bootstrap highlight */
        .table tbody tr:hover td, .table tbody tr:hover th {
            background-color: transparent;
        }


    </style>

{% endblock %}



{% block footer %}
    {{ block.super }}
    <script type="text/javascript" xmlns="http://www.w3.org/1999/html">

        // Treat acknowledge button depending on if acknowledgements are needed
        $(document).ready(function() {
            {% if my_object.state == 0 %}
            $('#acknowledge_button').attr('disabled', 'disabled');
            {% endif %}
            {% if my_object.acknowledged == 1 %}
                $('#acknowledge_button').attr('disabled','disabled');
                $('#acknowledge_text').html('Is acknowledged');
            {% endif %}

            // Fetch the effective check command for this object
            var url = '{% url rest/status %}json/command_line?host_name={{ host_name }}&service_description={{ service_description }}';
            $.getJSON(url)
                    .done( function(data) {
                        $('#effective_check_command').text(data);
                    })
                    .fail( function(data) {
                        $('#effective_check_command').text("Failed to load check_command.");
                        console.log(data);
                    });

            // If there happens to be any html in the plugin output, lets render it
            $("#plugin_output").html($("#plugin_output").text());


        });
        // When graphs link is clicked, simulate a click on 25hours tab as well
        $('#graphs_link').click(function(event) {
            $('#25hours').click();


        });
        // When history link is clicked, load the log history for this object
        $('#history_link').click(function(event) {
            // Load log history in its own tab
            $('#logdiv').html("Loading ...");
            var host_name = encodeURIComponent("{{ host_name }}");
            var service_description = encodeURIComponent("{{ service_description }}");
            var snippet_url = '{% url adagios.status.views.snippets_log %}?host_name=' + host_name + '&service_description=' + service_description;
            $('#logdiv').load(snippet_url);

        });
        // Load graphs when you click a new timeperiod under graphs tab
        $('#graphs_ul').click(function(event){
                    var anchor = event.target.href.split('#')[1];
                    var title = event.target.text;
                    my_element = document.getElementById(anchor);


                    if (my_element.childElementCount == 0) {
                        spinner = document.createElement( 'img');
                        spinner.setAttribute("src", "{% url media path="select2/spinner.gif" %}");
                        my_element.appendChild( spinner );
                        var url = '{% url pnp.views.pnp 'json' %}?host={{ host_name }}&srv={{ service_description }}';
                        $.getJSON(url)
                                .done( function(data) {
                                    $.each(data, function(i, item) {
                                        if (item['title'] == title) {
                                            new_img = document.createElement( 'img');
                                            new_img.setAttribute("src", "{% url pnp.views.pnp 'image' %}?" + item['image_url']);
                                            my_element.appendChild( new_img );
                                            }
                                        }
                                    )

                                })
                                .fail( function() {
                                    console.log('failed to fetch info from pnp4nagios');
                                })
                                .always( function() {
                                    spinner.setAttribute('class', 'hide');
                                });
                    }

        });
        // When enter is pressed in comment form. Post a new comment.
        $('#write_a_comment').keyup(function (e) {
            if (e.keyCode === 13) {
                my_comment = this.value;
                if (my_comment == '')
                    return;
                comment(my_comment,"{{ host_name }}", "{{ service_description }}");
                location.reload();
            }
        });
        // When an editable field is change, lets update configuration
        $('.editable_field').focus(function(e) {
            $(e.target).data('orig-value', "" + $(e.target).val());
        })
        $('.editable_field').blur(function(e) {
            var old_value = "" + $(e.target).data('orig-value');
            var new_value = "" + $(e.target).val();
            var attribute_name = "" + $(e.target).attr('name');
            var object_type = "{{ my_object.object_type }}";
            var short_name = "{{ my_object.short_name }}";
            if (old_value == new_value)
                return false;

            // Get the div right next to the input box for editing
            var message_div = $('#' + $(e.target).attr('name') + "_status");
            message_div.html('<div class="pull-right"><div class="circle state_3"></div> Saving ...</div>');

            tmp = change_attribute(
                    object_type,
                    short_name,
                    attribute_name,
                    new_value,
                    function(e) {
                        message_div.html(
                        '<a class="pull-left" href="" onclick=undo_attribute(\''+attribute_name+'\')>undo </a>' +
                        '<div class="pull-right">' +
                        '<div class="circle state_0"></div>  Saved. ' +
                        '</div> '
                        );
                    }
            );

        });

        // Perfomance Graph Preview on hover
        $("a.preview").popover({
            placement: 'top',
            trigger: 'hover',
            template: '<div class="popover"><div class="arrow"></div><div class="popover-inner" style="width: 600px"><h3 class="popover-title"></h3><div class="popover-content" style="min-height: 200px"><p></p></div></div></div>',
            content: function() {
                return "<img src='"+ $(this)[0].href +"' alt='Image preview' />";
            }
        });
    </script>

{% endblock %}


{% block sidebar %}
{% endblock %}

{% block action_buttons %}
    {{ block.super }}
{% endblock %}