{% extends "base.html" %}

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

{% block title %}{{ block.super }}{% endblock title %}
{% block smallheader %}{{ block.super }}{% endblock smallheader %}
{% block largeheader %}{{ block.super }}{% endblock largeheader %}
{% block content %}{{ block.super }}{% endblock content %}
{% block top_navigation %}{{ block.super }}{% endblock top_navigation %}
{% block page_header %}{{ block.super }}{% endblock page_header %}
{% block nav2 %}{{ block.super }}{% endblock nav2 %}
{% block header %}{{ block.super }}{% endblock header %}
{% block nav1 %}
    <a href="{% url "adagios.status.views.status_index" %}">{% trans "Status" %} </a>
{% endblock nav1 %}






{% block toolbar %}
    <div id="toolbar">

                <!-- block view_buttons starts -->
                {% block view_buttons %}
                <div class="btn-group pull-left" id="view_buttons">
                    <a class="btn btn-small dropdown-toggle " data-toggle="dropdown" href="#">
                        <i class="glyph-eye-open" title="View"> </i> <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="?q={{ request.GET.q }}&unhandled"> {% trans "Open Problems" %}</a></li>
                        <li><a href="?q={{ request.GET.q }}&state__isnot=0"> {% trans "All Problems" %}</a></li>
                        <li><a href="?q={{ request.GET.q }}&acknowledged=1"> {% trans "All Acknowledged" %}</a></li>
                        <li><a href="?q={{ request.GET.q }}&scheduled_downtime_depth__isnot=0"> {% trans "All in Downtime" %}</a></li>
                        <li><a href="?q={{ request.GET.q }}&state=0"> {% trans "All OK" %}</a></li>
                        <li><a href="?q={{ request.GET.q }}"> {% trans "Everything" %}</a></li>
                    </ul>
                </div>
                {% endblock %}
                <!-- block view_buttons ends -->

                <!-- block action_buttons starts -->
                {% block action_buttons %}
                    <div class="pull-left" id="action_buttons" >

                        <button class="btn btn-small adagios_service_multi" title="Recheck - Execute selected checks immediately" data-loading-text='<img src="{% url "media" path="external/select2/spinner.gif" %}"> <span id="reschedule_text"> {% trans "Running..." %}</span>' id="reschedule_button" onclick="adagios.status.reschedule();">
                            <i class="glyph-refresh" > </i> <span class="hide visible-desktop" id="reschedule_text">{% trans "Recheck" %}</span>
                        </button>
                        <button class="btn btn-small adagios_service_multi" title="Acknowledge - selected items will be hidden from unhandled and notifications will no longer be sent out" data-toggle="modal" data-target="#acknowledge_modal" id="acknowledge_button" data-loading-text='<img src="{% url "media" path="external/select2/spinner.gif" %}"> <span id="acknowledge_text"> {% trans "Running..." %}</span>'>
                            <i class="glyph-check" > </i><span  class="hide visible-desktop" id="acknowledge_text"> {% trans "Acknowledge" %}</span>
                        </button>
                        <button class="btn btn-small adagios_service_multi" title="Downtime - Selected items will be hidden from unhandled and notifications will not be sent until downtime is over" data-toggle="modal" data-target="#downtime_modal" id="downtime_button" data-loading-text='<img src="{% url "media" path="external/select2/spinner.gif" %}"> <span id="downtime_text"> {% trans "Running..." %}</span>'>
                            <i class="glyph-clock" > </i> <span  class="hide visible-desktop">{% trans "Downtime" %}</span>
                        </button>
                        <button class="btn btn-small adagios_service_multi" title="Send out mail for selected items" id="mail_button" data-toggle="modal" data-target="#mail_modal">
                            <i class="glyph-envelope" > </i>
                        </button>

                        <button class="btn btn-small adagios_service_multi" title="Copy {{ my_object.object_type }}" onclick="copy_selected_objects();">
                            <i class="icon-copy"> </i>
                        </button>
                        <button class="btn btn-small adagios_service_multi" title="Edit {{ my_object.object_type }}" id="configure_button" onclick="edit_selected_objects();">
                            <i class="glyph-edit" > </i>
                        </button>
                        <button class="btn btn-small adagios_service_multi" title="Delete {{ my_object.object_type }}" id="delete_button" onclick="delete_selected_objects();" >
                            <i class="glyph-bin" > </i>
                        </button>

                    </div>

                    <div class="btn-group pull-left hidden-phone" id="action_buttons_more">
                        <a class="btn btn-small dropdown-toggle" data-toggle="dropdown" id="status_toolbar_more_button">
                            More <span class="caret"></span>
                        </a>

                        <ul class="dropdown-menu">
                            <li>
                                <a class="enable_on_select" title="Add to businesgroup" id="add_to_businessprocess_button" onclick="adagios.bi.add_to_business_process();">
                                    {% trans "Add to business process" %}
                                </a>
                            </li>
                            <li>
                                <a class="enable_on_select" title="Add to contactgroup" onclick="add_to_group('contactgroup');">
                                    {% trans "Add to contactgroup" %}
                                </a>
                            </li>
                            <li>
                                <a class="enable_on_select" title="Add to servicegroup" onclick="add_to_group('servicegroup');">
                                    {% trans "Add to servicegroup" %}
                                </a>
                            </li>
                            <li>
                                <a class="enable_on_select" title="Remove acknowledgement" onclick="adagios.status.remove_acknowledgements();">
                                    {% trans "Remove Acknowledgement" %}
                                </a>
                            </li>
                            <li>
                                <a class="enable_on_select" title="Remove Active Downtimes" onclick="adagios.status.remove_active_downtimes();">
                                    {% trans "Remove active downtimes" %}
                                </a>
                            </li>
                        </ul>

                </div>


                {% endblock %}
                <!-- block action_buttons ends -->

                <!-- block search_box starts -->
                {% block search_box %}
                <div class=" pull-right" id="search_box">
                    <form action="{% url "status.views.services" %}" method="get" >
                        <div class="pull-left">
                            <a href="#"  data-toggle="modal" data-target="#search_modal">Advanced search</a>
                            <input name="q"  type="text" id="search_field"  value="{{ request.GET.q }}" class="input-large search-query" placeholder="search hosts, services, etc..." />
                        </div>
                    </form>
                </div>
                {% endblock search_box %}
                <!-- block search_box ends -->

            </div>
{% endblock toolbar %}




{% block footer %}
    {{ block.super }}
    <div id="modals">

        <!-- Acknowledge modal -->
        <div class="modal hide fade" id="acknowledge_modal">
            <form class="form-horizontal" onsubmit='$("#acknowledge_modal").modal("hide");  adagios.status.acknowledge(); return false;' >
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h3>{% trans "Acknowledge problem" %}</h3>
                </div>
                <div class="modal-body">
                    <p>{% trans "Acknowledge means that someone is handling this problem. It will no longer be marked as unhandled and Nagios will stop sending notifications about it until it goes back to OK status." %}</p>
                    <div class="control-group">
                        <label class="control-label" for="id_acknowledgement_author">
                            {% trans "Author" %}
                        </label>
                        <div class="controls" id="author_field">
                            <input id="id_acknowledgement_author" type="text" class="required" disabled=disabled value="{{ remote_user }}" name="author" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="id_comment">
                            Comment
                        </label>
                        <div class="controls" id="host_name_field">
                            <input id="id_comment" type="text" class="required" placeholder="Write some comment" name="comment" />
                            <label class="help-block">{% trans "Other people looking at this service will see this comment" %}.</label>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <a href="#" class="btn" onclick='$("#acknowledge_modal").modal("hide");'>{% trans "Cancel" %}</a>
                    <button type="submit" id="acknowledge_submit_button" class="btn btn-primary">{% trans "Acknowledge Problem" %}</button>
                </div>
            </form>
        </div>
        <!-- Acknowledge modal ends -->

        <!-- downtime modal -->
        <div class="modal hide fade" id="downtime_modal">
            <form name="downtime" class="form-horizontal">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h3>{% trans "Schedule Downtime" %}</h3>
                </div>
                <div class="modal-body">
                    <p>{% trans "Putting hosts and services into scheduled downtime means they are expected to fail during that period. Nagios will not send out any notifications during scheduled downtime and problems will not show up as unhandled problems." %}</p>
                    <div class="control-group">
                        <label class="control-label" for="id_downtime_author">
                            {% trans "Author" %}
                        </label>
                        <div class="controls" id="downtime_author_field">
                            <input id="id_downtime_author" type="text" class="required" disabled=disabled value="{{ remote_user }}" name="author" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="id_downtime_start_time">
                            {% trans "Start Time" %}
                        </label>
                        <div class="controls" id="downtime_start_time_field">
                            <div id="datepicker_start_time" class="date pull-left">
                                <label class="hide" for="id_start_time_picker"></label>
                                <input id="id_start_time_picker" name="start_time_picker" style="width: 70px" type="text">
                            </div>
                            <div class="control-group pull-left">
                                <label class="hide" for="id_input_start_hours"></label>
                                <input id="id_input_start_hours" name="start_hours" style="width: 40px" type="text">&nbsp;
                            </div>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="id_downtime_end_time">
                            {% trans "End Time" %}
                        </label>
                        <div class="controls" id="id_downtime_end_time_field">
                            <div id="datepicker_end_time" class="date pull-left">
                                <label class="hide" for="id_end_time_picker"></label>
                                <input id="id_end_time_picker" name="end_time_picker" style="width: 70px" type="text">
                            </div>
                            <div class="control-group pull-left">
                                <label class="hide" for="id_end_hours"></label>
                                <input id="id_end_hours" name="end_hours" style="width: 40px" type="text">&nbsp;
                            </div>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="id_downtime_comment">
                            {% trans "Comment" %}
                        </label>
                        <div class="controls" id="downtime_comment_field">
                            <input id="id_downtime_comment" type="text" class="required" placeholder="{% trans "Write some comment" %}" name="comment" />
                            <label class="help-block">{% trans "Other people looking at this service will see this comment." %}</label>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="id_downtime_recursive">
                        </label>
                        <div class="controls" id="id_downtime_recursive_field">
                            <input type="checkbox" name="recursive" id="id_downtime_recursive" />
                            {% trans "Apply to selected hosts and all their services" %}
                        </div>
                    </div>

                </div>

                <div class="modal-footer">
                    <a href="#" class="btn" onclick='$("#downtime_modal").modal("hide");'>{% trans "Cancel" %}</a>
                    <button type="submit" class="btn btn-primary">{% trans "Schedule Downtime" %}</button>
                </div>
            </form>
        </div>
        <!-- Downtime modal ends -->

        <!-- Mail modal -->
        <div class="modal hide fade" id="mail_modal">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h3>{% trans "Mail" %}</h3>
                </div>
                <div class="modal-body">
                    <p>{% trans "You can send manual e-mail notification to a specified email address." %}</p>

                    <div class="control-group">
                        <label class="control-label" for="id_mail_to">
                            {% trans "To:" %}
                        </label>
                        <div class="controls" id="mail_to_field">
                            <input id="id_mail_to" type="text" class="required" value="" name="to" style="width: 100%" />
                        </div>
                    </div>


                    <div class="control-group">
                        <label class="control-label" for="id_mail_message">
                            {% trans "Message" %}
                        </label>
                        <div class="controls" id="id_mail_message_field">
                            <textarea id="id_mail_message"
                            class="required" placeholder="{% trans "Hey everyone, please check the link and services below." %}" name="message" ></textarea>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="id_add_myself_to_cc">
                        </label>
                        <div class="controls" id="id_add_myself_to_cc_field">
                            <input type="checkbox" checked="checked" name="add_myself_to_cc" value="on" id="id_add_myself_to_cc" />
                            {% trans "Add myself to CC" %}
                        </div>
                    </div>


                    <div class="control-group">
                        <label class="control-label" for="id_acknowledge_all_problems">
                        </label>
                        <div class="controls" id="id_acknowledge_all_problems_field">
                            <input type="checkbox" checked="checked" name="acknowledge_all_problems" value="on" id="id_acknowledge_all_problems" />
                            {% trans "Acknowledge Unhandled Problems" %}
                        </div>
                    </div>


                </div>
                <div class="modal-footer">
                    <a href="#" class="btn" onclick='$("#mail_modal").modal("hide");'>{% trans "Cancel" %}</a>
                    <button type="submit" class="btn btn-primary" onclick='$("#mail_modal").modal("hide"); adagios.status.send_mail();'>{% trans "Send" %}</button>
                </div>
        </div>
        <!-- Mail modal ends -->

        <!-- Advanced search Modal -->
        <div class="modal hide fade" id="search_modal">
            <form class="form-horizontal" action="{% url "status.views.services" %}" >
                <div class="modal-header">

                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h3>{% trans "Advanced search" %}</h3>
                </div>
                <div class="modal-body">

                    <div id="search_dialog">
                        <div class="control-group">
                            <input id="id_search_modal_q" value="{{ request.GET.q }}" type="text" placeholder="Has the words" name="q" style="width: 100%"/>
                        </div>

                        <div class="control-group">
                            <table>
                                <tr>
                                    <th class="pull-left">Service state:</th>
                                    <td><input type="checkbox" name="state" value="0" /> OK</td>
                                    <td><input type="checkbox" name="state" value="1" /> Warning</td>
                                    <td><input type="checkbox" name="state" value="2" /> Critical</td>
                                    <td><input type="checkbox" name="state" value="3" /> Unknown</td>
                                </tr>
                                <tr>
                                    <th class="pull-left">Host state:</th>
                                    <td><input type="checkbox" name="host_state" value="0" />Up</td>
                                    <td><input type="checkbox" name="host_state" value="1" />Down</td>
                                    <td><input type="checkbox" name="host_state" value="2" />Unreachable</td>
                                    <td></td>
                                </tr>
                            </table>
                        </div>

                        <div class="control-group">

                            <div class="pull-left">
                                <table>
                                    <tr>
                                        <th class="pull-left">Service Properties:</th>
                                        <th>Yes</th>
                                        <th>No</th>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Scheduled Downtime:" %}</td>
                                        <td><input type="checkbox"  name="in_scheduled_downtime" value="1" /></td>
                                        <td><input type="checkbox"  name="in_scheduled_downtime" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Acknowledged:" %}</td>
                                        <td><input type="checkbox"  name="acknowledged" value="1" /></td>
                                        <td><input type="checkbox"  name="acknowledged" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Active Checks Enabled:" %}</td>
                                        <td><input type="checkbox"  name="active_checks_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="active_checks_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Event Handler Enabled:" %}</td>
                                        <td><input type="checkbox"  name="event_handler_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="event_handler_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Flap Detection Enabled:" %}</td>
                                        <td><input type="checkbox"  name="flap_detection_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="flap_detection_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Notifications Enabled:" %}</td>
                                        <td><input type="checkbox"  name="notifications_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="notifications_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Accepts Passive Checks:" %}</td>
                                        <td><input type="checkbox"  name="accept_passive_checks" value="1" /></td>
                                        <td><input type="checkbox"  name="accept_passive_checks" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Hard State:" %}</td>
                                        <td><input type="checkbox"  name="state_type" value="1" /></td>
                                        <td><input type="checkbox"  name="state_type" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Check Period:" %}</td>
                                        <td><input type="checkbox"  name="in_check_period" value="1" /></td>
                                        <td><input type="checkbox"  name="in_check_period" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Notification Period:" %}</td>
                                        <td><input type="checkbox"  name="in_notification_period" value="1" /></td>
                                        <td><input type="checkbox"  name="in_notification_period" value="0" /></td>
                                    </tr>
                                </table>
                            </div>

                            <div class="pull-right">
                                <table>
                                    <tr>
                                        <th class="pull-left">Host Properties:</th>
                                        <th>Yes</th>
                                        <th>No</th>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Scheduled Downtime:" %}</td>
                                        <td><input type="checkbox"  name="in_scheduled_downtime" value="1" /></td>
                                        <td><input type="checkbox"  name="in_scheduled_downtime" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Acknowledged:" %}</td>
                                        <td><input type="checkbox"  name="host_acknowledged" value="1" /></td>
                                        <td><input type="checkbox"  name="host_acknowledged" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Active Checks Enabled:" %}</td>
                                        <td><input type="checkbox"  name="host_active_checks_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="host_active_checks_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Event Handler Enabled:" %}</td>
                                        <td><input type="checkbox"  name="host_event_handler_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="host_event_handler_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Flap Detection Enabled:" %}</td>
                                        <td><input type="checkbox"  name="host_flap_detection_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="host_flap_detection_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Notifications Enabled:" %}</td>
                                        <td><input type="checkbox"  name="host_notifications_enabled" value="1" /></td>
                                        <td><input type="checkbox"  name="host_notifications_enabled" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "Accepts Passive Checks:" %}</td>
                                        <td><input type="checkbox"  name="host_accept_passive_checks" value="1" /></td>
                                        <td><input type="checkbox"  name="host_accept_passive_checks" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Hard State:" %}</td>
                                        <td><input type="checkbox"  name="host_state_type" value="1" /></td>
                                        <td><input type="checkbox"  name="host_state_type" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Check Period:" %}</td>
                                        <td><input type="checkbox"  name="host_in_check_period" value="1" /></td>
                                        <td><input type="checkbox"  name="host_in_check_period" value="0" /></td>
                                    </tr>
                                    <tr>
                                        <td>{% trans "In Notification Period:" %}</td>
                                        <td><input type="checkbox"  name="host_in_notification_period" value="1" /></td>
                                        <td><input type="checkbox"  name="host_in_notification_period" value="0" /></td>
                                    </tr>
                                </table>
                            </div>

                        </div>

                    </div>



                </div>

                <div class="modal-footer">
                    <a href="#" class="btn" onclick='$("#search_modal").modal("hide");'>{% trans "Cancel" %}</a>
                   <button type="submit" class="btn btn-primary">{% trans "Search" %}</button>
                 </div>
            </form>
        </div>
        <!-- Advanced search modal ends -->

        <!-- Save search modal -->
        <div class="modal hide fade" id="save_search_modal">
            <form class="form-horizontal" id="save_search_form">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h3>{% trans "Save Current View as a search" %}</h3>
                </div>

                <div class="modal-body">
                    <div class="control-group">
                        <label class="control-label" for="id_save_search_name">
                            {% trans "Name" %}
                        </label>
                        <div class="controls">
                            <input id="id_save_search_name" type="text" class="required" value="" name="name" />
                        </div>
                        <div class=hide>
                            <input type="text" id="id_save_search_url" value="">
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <a href="#" class="btn" onclick='$("#save_search_modal").modal("hide");'>{% trans "Cancel" %}</a>
                    <button type="submit" id="wish_submit_button" class="btn">{% trans "Save this search" %}</button>
                </div>
            </form>
        </div>
        <!-- Save search modal ends -->
    </div>

    <script type="text/javascript" xmlns="http://www.w3.org/1999/html">


        // When this function is called, gather all selected objects and add them to a specific group
        function add_to_group(group_type) {
            var object_type, value;
            var querystring = '';
            $( ".selectable :checked").each(function() {
                object_type = $(this).attr('name');
                value = $(this).attr('value');
                querystring += "&" + object_type + "=" + value;
            });
            window.location.href = "{% url "objectbrowser.views.add_to_group" %}/" + group_type + "?" + querystring;
        }


        // When this function is called, delete all selected objects
        function delete_selected_objects() {
            var object_type, value;
            var querystring = '';
            $( ".selectable :checked").each(function() {
                object_type = $(this).attr('name');
                value = $(this).attr('value');
                querystring += "&" + object_type + "=" + value;
            });
            window.location.href = "{% url "objectbrowser.views.bulk_delete" %}" + "?" + querystring;
        }


        // When this function is called, go to the edit page for all selected objects
        function edit_selected_objects() {
            var object_type, value;
            var querystring = '';
            $( ".selectable :checked").each(function() {
                object_type = $(this).attr('name');
                value = $(this).attr('value');
                querystring += "&" + object_type + "=" + value;
            });
            window.location.href = "{% url "objectbrowser.views.bulk_edit" %}" + "?" + querystring;
        }


        // When this function is called. Go to the copy object page
        function copy_selected_objects() {
            var object_type, value;
            var querystring = '';
            $( ".selectable :checked").each(function() {
                object_type = $(this).attr('name');
                value = $(this).attr('value');
                querystring += "&" + object_type + "=" + value;
            });
            window.location.href = "{% url "objectbrowser.views.bulk_copy" %}" + "?" + querystring;
        }
        // When this function is called. Gather all selected services and add them to a specific business process
        adagios.bi.add_to_business_process = function() {
            var object_type = [];
            var value;
            var querystring = "?";
            // special hack, change destination url if we are adding graphs
            var doing_graphs = false;
            $( ".selectable :checked").each(function() {
                object_type = $(this).attr('name');
                value = $(this).attr('value');
                querystring += "&" + object_type + "=" + value;
                if (object_type == 'graph') {
                    doing_graphs = true;
                }
            });
            if (doing_graphs == true) {
                window.location.href = "{% url "bi.views.add_graph" %}/?" + querystring;
            }
            else {
                window.location.href = "{% url "bi.views.add_subprocess" %}/?" + querystring;
            }

            return false;
        };
    </script>

    <script type="text/javascript" xmlns="http://www.w3.org/1999/html">

        $(document).ready(function() {


            // Prepare dates in the downtime modal
            adagios.status.prepare_downtime_modal();

            // When somebody clicks the acknowledge button, lets focus on the comment field
            $('#acknowledge_modal').on('shown', function () {
                document.getElementById('id_comment').focus()
            });


            // When somebody clicks the downtime button, lets focus on the comment field
            $('#downtime_modal').on('shown', function () {
                document.getElementById('id_downtime_comment').focus()
            });

            // When somebody clicks the notification button, lets load contact email addresses
            $('#mail_modal').on('shown', function () {
                adagios.rest.status.emails()
                        .done(function(data) {
                            $("#id_mail_to").select2({
                                tags:data,
                                tokenSeparators: [","],
                                placeholder:gettext("Comma seperated list of email addresses to send to")
                            });
                        });
            });



        });


        // Prepare dates and other stuff for the downtime modal
        adagios.status.prepare_downtime_modal = function() {
            var start_time = (new Date).valueOf() / 1000;
            var end_time = (new Date);
            end_time.setHours(end_time.getHours() + 2);
            end_time = end_time.valueOf() / 1000;
            var downtime_form = $("form[name='downtime']");
            downtime_form.adagios_datetimepicker(start_time, end_time);
            downtime_form.submit(function(e) {
                $("#downtime_modal").modal("hide");
                adagios.status.downtime();
                e.preventDefault();
                return false;
            });
        };



        // This function responsible for acknowledge on all selected rows
        adagios.status.acknowledge = function() {
            $('#acknowledge_modal').modal('hide');
            var comment = document.getElementById("id_comment").value;
            if (comment == '') {
                comment = gettext("No comment was entered...");
            }
            var selected_objects = adagios.status.get_selected_objects();
            var objects_done = 0;
            var my_data;
            var hostlist = '';
            var servicelist = '';

            $.each(selected_objects, function(i, item) {
                var host_name =  item['host_name'];
                var service_description = item['service_description'];
                var object_type = item['object_type'];
                if (object_type == 'host') {
                    hostlist += ';' + host_name;
                }
                else if (object_type == 'service') {
                    servicelist += ';' + host_name + ',' + service_description;
                }
            });
            my_data = {
                "hostlist": hostlist,
                "servicelist": servicelist,
                "comment":comment,
                "author":"{{ remote_user }}",
                "sticky":"1",
                "persistent":"0",
                "notify":"1"
            };
            adagios.misc.info(gettext("Sending Command to server"), 'acknowledgement');
            adagios.rest.status.acknowledge_many(my_data)
                    .done(function(data) {
                        adagios.misc.success(gettext("Acknowledgement has been set. You should reload your browser."), "acknowledgement", 5000);
                        adagios.status.clear_all_selections();
                    })
                    .fail(function(data) {
                        adagios.misc.error(gettext("Error while sending acknowledge command to the server."), "acknowledgement");
                    })
                    .always(function(data) {
                    });
            return false;

        };


        // This function responsible for downtime on all selected rows
        adagios.status.downtime = function() {
            var $form_downtime = $("form[name='downtime']");
            $('#downtime_modal').modal('hide');
            var comment = document.getElementById("id_downtime_comment").value;
            var recursive = document.getElementById("id_downtime_recursive").checked;
            if (comment == '') { comment = "No comment was entered..."; }
            var start_time = $form_downtime.find("input[name='start_time']").val();
            var end_time = $form_downtime.find("input[name='end_time']").val();
            var host_name, service_description, hostgroup_name, object_type;
            var my_data;
            var selected_objects = adagios.status.get_selected_objects();
            var services_done = 0;
            var hostlist = '';
            var hostgrouplist = '';
            var servicelist = '';
            $.each(selected_objects, function(i, item) {
                host_name =  item['host_name'];
                service_description = item['service_description'];
                hostgroup_name = item['hostgroup_name'];
                object_type = item['object_type'];
                if (object_type == 'host') {
                    hostlist = hostlist + ';' + host_name;
                }
                else if (object_type == 'service') {
                    servicelist = servicelist + ';' + host_name + ',' + service_description
                }
                else if (object_type == 'hostgroup') {
                    hostgrouplist = hostgrouplist + ';' + hostgroup_name;
                }

            });
            my_data = {
                "hostgrouplist":hostgrouplist,
                "hostlist":hostlist,
                "servicelist":servicelist,
                "comment":comment,
                "author":"{{ remote_user }}",
                "all_services_on_host":recursive,
                "start_time":start_time,
                "end_time":end_time
            };
            adagios.misc.info(gettext("Sending downtime command to server."), 'downtime');
            adagios.rest.status.downtime_many(my_data)
                    .done(function(data) {
                        adagios.misc.success(gettext("Downtime command has been sent. You should refresh your browser."), 'downtime');
                    })
                    .fail(function(data) {
                        adagios.misc.error(gettext("Could not send downtime command to the server."), 'downtime');
                    });

        };


        // This function sends email to selected recipients for all selected rows
        adagios.status.send_mail = function() {
            $('#mail_modal').modal('hide');
            var my_data = {
                "to":document.getElementById("id_mail_to").value,
                "csrfmiddlewaretoken": window.csrftoken,
                "add_myself_to_cc": document.getElementById("id_add_myself_to_cc").checked,
                "acknowledge_all_problems": document.getElementById("id_acknowledge_all_problems").checked,
                "message":document.getElementById("id_mail_message").value,
                "service": [],
                "host": []
            };

            var selected_objects = adagios.status.get_selected_objects();
            var shortname;
            for (var i=0;i<selected_objects.length;i++){
                if (selected_objects[i]['object_type'] == 'service') {
                    shortname = selected_objects[i]["host_name"] + "/" + selected_objects[i]["service_description"];
                    my_data['service'].push( shortname);
                }
                if (selected_objects[i]['object_type'] == 'host') {
                    my_data['host'].push(selected_objects[i]["host_name"]);
                }
            }
            jQuery.param( my_data["service"], true);
            $.ajax({
                type: 'POST',
                url: '{% url "misc.views.mail" %}',
                data: my_data,
                async: true,
                success: function(data) {
                    adagios.misc.success(gettext("Mail has been sent"), "mail_sent", 5000);
                },
                error: function(data) {
                  adagios.misc.error(gettext("Error sending mail"), "mail_sent");
                },
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            });

        };


        // Send a comment about specific service to our monitoring engine
        adagios.status.comment = function(comment,host_name, service_description, persistent ) {
            if (comment == '') {
                comment = gettext("No comment was entered...");
            }
            if (service_description == '_HOST_')
                service_description = undefined;
            var my_data  = {
                "host_name":host_name,
                "service_description":service_description,
                "comment":comment,
                "author":"{{ remote_user }}",
                "persistent":"1"
            };
            return adagios.rest.status.comment(my_data)
                    .done(function(data) {
                        adagios.misc.success(gettext("Comment has been sent to server."));

                    })
                    .fail(function(data) {
                        adagios.misc.error(gettext("Problem sending comment to server."))
                    });

        }

    </script>

{% endblock footer %}
