{% extends "base_status.html" %}

{% load adagiostags %}
{% load static %}
{% load i18n %}

{% block title %}{{ my_object.display_name }}{% endblock %}
{% block smallheader %}
{% endblock %}
{% block largeheader %}
    {% if object_type == 'service' %}
    {% trans "Service" %}
            <div class="circle state_{{ my_object.state }}"></div>
            <small>{{ service.display_name }}</small>{% trans " on" %}
    {% endif %}

    {% trans "host" %}

    <div class="circle host state_{{ host.state }}"></div>
    <a href="{% url 'status.views.detail' %}?host_name={{ host.name }}">
        <small>{{ host.display_name }}</small>
    </a>
{% endblock %}
{% block nav1 %}<a href="{% url 'status.views.services' %}">{% trans "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="row-fluid">
      <div class="span9" id="maincontent_left_side">
        <ul class="nav nav-tabs" id="objecttab">
            <li><a href="#general" data-toggle="tab">{% trans "General" %}</a></li>
            <li><a href="#information" data-toggle="tab">{% trans "Information" %}</a></li>
            <li><a href="#events" data-toggle="tab" id="history_link">{% trans "History" %}</a></li>
            {% if settings.enable_pnp4nagios %}
            <li><a href="#graphs" data-toggle="tab" id="graphs_link">{% trans "Graphs" %}</a></li>
            {% endif %}
            {% if settings.enable_graphite %}
              <li><a href="#graphite" data-toggle="tab" id="graphite_link">{% trans "Graphite" %}</a></li>
            {% endif %}
            <li><a href="#custom_variables" data-toggle="tab">{% trans "Custom Variables" %}</a></li>
            <li><a href="#other" data-toggle="tab">{% trans "Debug" %}</a></li>
        </ul>
        <div class="tab-content">

        <div class="tab-pane active" id="general">
        {% if my_object %}
            <table class="table">
                <tr>
                    <td class="hidden-phone">{% trans "Status" %}</td>
                    <td>
                        {% if my_object.last_check != 0 %}
                        <div class="circle {{ object_type }} 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 %}{% trans "(has been acknowledged)" %}
                            {% else %}{% trans "(not acknowledged)" %}
                            {% endif %}
                          {% endif %}
                        {% else %}
                        <div class="circle state_pending"></div>{% trans " Pending" %}
                        {% endif %}
                    </td>
                </tr>
                <tr >
                    <td class="hidden-phone" >{% trans "Host Name" %}</td>
                    <td><a href="{% url 'status.views.detail' %}?host_name={{ host.name  }}" title="{{ host.plugin_output }}">
                        {% if my_object.last_check != 0 %}
                        <div class="circle host state_{{ host.state }}"></div>
                        {% else %}
                        <div class="circle host state_pending"></div>
                        {% endif %}
                        {{ host.display_name }}
                    </a>
                    </td>
                </tr>
                <tr >
                    <td class="hidden-phone">{% trans "Host Address" %}</td>
                    <td>{{ host.address }}</td>
                </tr>
                <tr>
                    <td class="hidden-phone">{% trans "Host Alias" %}</td>
                    <td>{{ host.alias }}</td>
                </tr>
                {% if service %}
                <tr >
                    <td class="hidden-phone">{% trans "Service Name" %}</td>
                    <td>{{ service.display_name }}</td>
                </tr>
                {% endif %}
                <tr>
                    <td class="hidden-phone">{% trans "Check output" %}</td>
                    <td>
                        {% if my_object.last_check == 0 %}
                        <div class="alert alert-info">{% trans "Has not run yet." %}</div>
                        {% else %}
                            {% 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 %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td class="hidden-phone">{% trans "Performance  Metrics" %}</td>
                    <td >
                        {% if perfdata %}
                            <table border=1 class="small" style="font-size: 90%; border-color: #ffffff;" >
                                <tr>
                                    <th></th>
                                    <th>{% trans "label" %}</th>
                                    <th>{% trans "value" %}</th>
                                    <th>{% trans "warn" %}</th>
                                    <th>{% trans "crit" %}</th>
                                    <th>{% trans "unit" %}</th>
                                    <th>{% trans "min" %}</th>
                                    <th>{% trans "max" %}</th>
                                    {% if settings.enable_pnp4nagios %}
                                    <th>{% trans "pnp" %}</th>
                                    {% endif %}
                                    {% if settings.enable_graphite %}
                                      <th>{% trans "graphite" %}</th>
                                    {% endif %}
                                </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>
                                        {% if settings.enable_pnp4nagios %}
                                        <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>
                                        {% endif %}
                                        {% if settings.enable_graphite %}
                                          <td><a href="{{ graphite_default|hash:i.label }}" class="preview" title="{{ service_description }} {{ i.label }}"><i class="glyph glyph-charts"></i></a></td>
                                        {% endif %}
                                    </tr>
                                {% endfor %}
                            </table>
                        {% else %}
                            {% trans "No Perfdata available." %}
                        {% endif %}

                    </td>
                </tr>


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

        <div class="tab-pane" id="other">
            <h4>{% trans "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" class="hide">
                <table>
                    <tbody>
                    <tr>
                        <td class="selectable">
                            <input class="chkbox chkbox-primary {{ service.tags }}"  style="margin: -2px; padding: 0px;" type="checkbox" checked="checked" name="{{ my_object.object_type }}" value="{{ my_object.short_name }}" data-host_name="{{ host_name }}" data-service_description="{{ service_description }}" data-object_type={{ my_object.object_type }}>
                            <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>
              {% blocktrans with ot=my_object.object_type ci=my_object.check_interval %}
              This {{ ot }} is checked every <a href="#">{{ ci }}</a> minutes.
              {% endblocktrans %}

              {% blocktrans with mca=my_object.max_check_attempts ri=my_object.retry_interval %}
              When a problem occurs it will be checked <a href="#">{{ mca }}</a> times
              every <a href="#">{{ ri }}</a> minutes before going to
              hard state and start sending out notifications.
              {% endblocktrans %}
            </p>

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

            <table class=table>
                <tr>
                    <td>{% trans "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  }}{% trans " ago)" %}</span>
                        {% else %}
                            {% trans "check has never been executed." %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>{% trans "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 }}{% trans " from now)" %}</span>
                        {% else %}
                            {% trans "Check is not scheduled to be executed." %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>{% trans "Check Period" %}</td>
                    <td>
                        {% if my_object.check_period %}
                        <a href="{% url 'adagios.objectbrowser.views.edit_object' %}?object_type=timeperiod&timeperiod_name={{ my_object.check_period }}">{{ my_object.check_period }}</a>
                        {% else %}
                        {% trans "Unknown" %}
                        {% endif %}
                        <span class="pull-right">
                            (currently {% if my_object.in_check_period %}inside{% else %}outside{% endif %} check_period)
                        </span>
                    </td>
                </tr>
                <tr>
                    <td>{% trans "Notification Period" %}</td>
                    <td>
                        {% if my_object.notification_period %}
                        <a href="{% url 'adagios.objectbrowser.views.edit_object' %}?object_type=timeperiod&timeperiod_name={{ my_object.notification_period }}">{{ my_object.notification_period }}</a>
                        {% else %}
                        {% trans "Unknown" %}
                        {% endif %}
                        <span class="pull-right">
                            (currently {% if my_object.in_notification_period %}inside{% else %}outside{% endif %} notification period)
                        </span>
                    </td>
                </tr>
                <tr>
                    <td>
                        {% trans "Check command" %}
                    </td>
                    <td id="effective_check_command">{% trans " Loading..." %}</td>
                </tr>
                <tr>
                    <td colspan="2">

                    </td>
                </tr>
                <tr>
                    <td>{% trans "Active Checks" %}</td>
                    <td>
                        {% if my_object.active_checks_enabled == 1 %}
                            {% trans "Enabled" %}
                        {% else %}
                            {% trans "Disabled" %}
                        {% endif %}

                    </td>
                </tr>
                <tr>
                    <td>{% trans "Passive checks" %}</td>
                    <td>
                        {% if my_object.accept_passive_checks == 1 %}
                            {% trans "Enabled" %}
                        {% else %}
                            {% trans "Disabled" %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>{% trans "Obsessing" %}</td>
                    <td>
                        {% if my_object.host_obsess_over_host == 1 %}
                            {% trans "Enabled" %}
                        {% else %}
                            {% trans "Disabled" %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>{% trans "Notifications" %}</td>
                    <td>
                        {% if my_object.notifications_enabled == 1 %}
                            {% trans "Enabled" %}
                        {% else %}
                            {% trans "Disabled" %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>{% trans "Event Handler" %}</td>
                    <td>
                        {% if my_object.event_handler_enabled == 1 %}
                            {% trans "Enabled" %}
                        {% else %}
                            {% trans "Disabled" %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>{% trans "Flap Detection" %}</td>
                    <td>
                        {% if my_object.flap_detection_enabled == 1 %}
                            {% trans "Enabled" %}
                        {% else %}
                            {% trans "Disabled" %}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td>
                        {% trans "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="{% trans "Delete Downtime" %} {{ i.0 }}"
                                                onclick="$.getJSON('{% url 'home' %}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.detail' %}?contact_name={{ i.1 }}">{{ i.1 }}</a>: {{ i.2 }}
                                    </div>
                                {% endfor %}
                            </div>
                        {% else %}
                            {% trans "No Downtimes are scheduled." %}
                        {% endif %}

                    </td>
                </tr>
                <tr>
                    <td>
                        {% trans "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 'home' %}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.detail' %}?contact_name={{ i.1 }}">{{ i.1 }}</a>: {{ i.2 }}
                                    </div>
                                {% endfor %}
                            {% else %}
                                <p>{% blocktrans with ot=my_object.object_type %}This {{ ot }} has no comments.{% endblocktrans %}</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="{% trans "Write a comment..." %}">
                            </div>
                        </div>

                    </td>
                </tr>
                <tr>
                    <td>{% trans "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 host state_{{ parent.state }}">
                                                    <a href="{% url 'status.views.detail' %}?host_name={{ parent.name }}">{{ parent.name }}</a>
                                                </div>
                                            </td>
                                        {% endfor %}
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        {% else %}
                            {% blocktrans with ot=my_object.object_type %}This {{ ot }} has no configured network parents.{% endblocktrans %}
                        {% endif %}
                </tr>
            </table>
        </div>

        <div class="tab-pane" id="custom_variables">
            {% trans "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>
                        {% trans "Variable Name" %}
                    </th>
                    <th>
                        {% trans "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 %}
            {% trans "This check has no custom variables defined." %}
            {% endif %}
            <p></p>
            {% if my_object.host_custom_variables %}
                Host <a href="{% url 'status.views.detail' %}?host_name={{ host_name }}#tab_custom_variables">{{ host_name }}</a> also has some custom variables.
                <table class="table table-condensed">
                    <tr>
                        <th>
                            {% trans "Variable Name" %}
                        </th>
                        <th>
                            {% trans "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.objectbrowser.views.edit_object' %}?object_type={{ object_type }}&host_name={{ host_name }}{% if object_type == "service" %}&service_description={{ service_description }}{% endif %}#monitoring_tab">{% trans "Configure check command" %}</a>


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


        {% if settings.enable_pnp4nagios %}
        <div class="tab-pane" id="graphs">
                {% if graph_urls %}
                    <div id="graphs2" 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">{% trans "4 Hours" %}</a></li>
                            <li><a href="#day" data-toggle="tab" id="25hours">{% trans "25 Hours" %}</a></li>
                            <li><a href="#week" data-toggle="tab">{% trans "One Week" %}</a></li>
                            <li><a href="#month" data-toggle="tab">{% trans "One Month" %}</a></li>
                            <li><a href="#year" data-toggle="tab">{% trans "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">
                            {% blocktrans %}We got an error while trying to talk with PNP4nagios: {{ pnp4nagios_error }}{% endblocktrans %}
                        </div>
                        {% trans "Please check that:" %}
                        <ul>
                        <li><a href="{{ pnp_url }}">pnp4nagios</a>{% trans " is installed and working" %}</li>
                        <li><i>{% trans "PNP Filepath" %}</i>{% trans " is correctly configured under " %}<a href="{% url 'misc.views.settings' %}">{% trans "settings" %}</a></li>
                        <li>{% trans "User running nagios is a member of the unix groups apache or www-data" %}</li>
                        <li>{% url 'pnp.views.pnp' "json" as the_url %} {% blocktrans %}Click <a href="{{ the_url }}">here</a> to see pnp4nagios output{% endblocktrans %}</li>
                        </ul>
                    {% endif %}
                    {% blocktrans with ot=my_object.object_type %}No Performance data is available for this {{ ot }}.{% endblocktrans %}
                {% endif %}
        </div>
        {% endif %}
        {% if settings.enable_graphite %}
        <div class="tab-pane" id="graphite">
          <div id="graphite" class="tabbable"> <!-- Only required for left/right tabs -->
            <ul class="nav nav-pills" id="graphite_ul">
              {% for graph in graphite %}
                <li{% if graph.css_id == settings.GRAPHITE_DEFAULT_TAB %} class="active"{% endif %}><a href="#graphite_{{ graph.css_id }}" id="graphite_{{ graph.css_id }}_link" data-toggle="tab">{% trans graph.name %}</a></li>
              {% endfor %}
            </ul>

            <div class="tab-content" id="graphs_content">
              {% for graph in graphite %}
                <div class="tab-pane{% if graph.css_id == settings.GRAPHITE_DEFAULT_TAB %} active{% endif %}" id="graphite_{{ graph.css_id }}">
                  {% for k,v in graph.metrics.items %}
                    <img src="{{ v }}" />
                    <br /><br />
                  {% endfor %}
                </div>
              {% endfor %}
            </div>
          </div>
        </div>
        {% endif %}

        </div>
      </div>

    <div class="span3 visible-desktop" id="maincontent_right_side">
    {% if host.services_with_info %}
     <div id="host_services">
        <h5>{% trans "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.detail' %}?host_name={{ host.name  }}&service_description={{ i.0 }}">
                        <div class="circle state_pending"></div> {{ i.0 }}
                    </a>
                </li>

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

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

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

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

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

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

    {% if my_object.contact_groups %}
        <h5>{% trans "Is a member of contactgroups" %}</h5>
        <ul class="nav nav-list">
            {% for group in my_object.contact_groups %}
                <li><a href="{% url 'adagios.status.views.detail' %}?contactgroup_name={{ group }}">{{ group }}</a></li>
            {% endfor %}
        </ul>
    {% endif %}

    {% if service.groups %}
        <h5>{% trans "Is a member of servicegroups" %}</h5>
        <ul class="nav nav-list">
            {% for group in service.groups %}
                <li><a href="{% url 'adagios.status.views.detail' %}?servicegroup_name={{ group }}">{{ group }}</a></li>
            {% endfor %}
        </ul>
    {% endif %}

</div>

    </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() {
            adagios.status.service_detail_update_servicestate_icon("{{ host_name }}");
            {% 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 %}

             // Disable recheck button if active checks are not enabled
             {% if my_object.active_checks_enabled == 0 %}
                 $('#reschedule_button').attr('disabled','disabled');
                 $('#rescheduled_text').html('Active checks disabled');
             {% endif %}

            // Fetch the effective check command for this object
            var parameters = {};
            parameters['host_name'] = "{{ host_name }}";
            parameters['service_description'] = "{{ service_description }}";
            adagios.rest.status.command_line(parameters)
                    .done( function(data) {
                        $('#effective_check_command').text(data);
                    })
                    .fail( function(data) {
                        $('#effective_check_command').text(gettext("Failed to load check_command."));
                        console.log(data);
                    });

            {% if not settings.escape_html_tags %}
            // If there happens to be any html in the plugin output, lets render it
            $("#plugin_output").html($("#plugin_output").text());
            {% endif %}

        });
        // 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;
                    var my_element = document.getElementById(anchor);


                    if (my_element.childElementCount == 0) {
                        var spinner = document.createElement( 'img');
                        spinner.setAttribute("src", "{% url "media" path="external/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) {
                                    var new_img;
                                    $.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) {
                var my_comment = this.value;
                if (my_comment == '')
                    return;
                adagios.status.comment(my_comment,"{{ host_name }}", "{{ service_description }}")
                        .done(function(data) {
                            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>');

            adagios.status.change_attribute(object_type, short_name, attribute_name, new_value)
                    .done(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> '
                        );

                    });

            return true;
        });

        // 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='{% trans "Image preview" %}' />";
            }
        });
    </script>

{% endblock %}


{% block sidebar %}
{% endblock %}

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