{% extends "base.html" %}

{% load i18n %}

{% block title %}{% trans "Edit Host" %}{% endblock %}
{% block smallheader %}{% trans "Edit" %}{% endblock %}
{% block largeheader %}{% trans "Host" %}{% endblock %}
{% block nav2 %}<a href="{% url "adagios.okconfig_.views.edit" host_name %}">{% trans "Edit Host" %}</a>{% endblock %}
{% block toolbar %}{% endblock %}

{% block content %}
{% if myhost %}
    <div>
    <h3>{% blocktrans %}{{ host_name }} information:{% endblocktrans %}</h3>
        <dl class="dl-horizontal">
            <dt>{% trans "Host Name" %}</dt>
            <dd>{{ myhost.host_name }}</dd>
            <dt>{% trans "Address" %}</dt>
            <dd>{{ myhost.address }}</dd>
            <dt>{% trans "Status" %}</dt>
            <dd><span id="host_status" class="label">{% trans "Pending" %}</span> </dd>
        </dl>
    </div>


    <!-- <button class="btn btn-success">Run all Service checks</button> -->
    <div style="margin: 16px">
        <a href="{% url "okconfig_.views.install_agent" %}/?remote_host={{ myhost.host_name }}" class="btn btn-success">{% trans "Install Agent" %}</a>
        <a href="{% url "okconfig_.views.addtemplate" %}/?host_name={{ myhost.host_name }}" class="btn btn-success">{% trans "Add templates" %}</a>
    </div>

    <h3>{% trans "Services of this host:" %}</h3>
    <hr>
    <form action="#" method="post" class="form-horizontal">{% csrf_token %}
        <table class="table">
        {% for form in forms %}
                <tr class="alert-info">
                    <th >
                        {% for field in form %}
                            {% if field.label == "register" %}
                            {{ field }}
                            {% for err in field.errors %}
                                <p class="help-inline error">{{ err }}</p>
                            {% endfor %}
                            {% endif %}
                        {% endfor %}
                    </th>
                    <th colspan=2>{{ form.description }}
                        <a href="#pop"
                                id="{{ form.service.id }}_status"
                                data-object-id="{{ form.service.id }}"
                                class="btn pull-right service_status run_check_command"
                                rel="popover"
                                title="Plugin Output"
                                data-content="Pending"
                                data-placement="left">
                            Pending
                        </a>
                        {% if fields.non_field_errors %}
                            <div class="alert alert-warning">{{ fields.non_field_errors  }}</div>
                        {%  endif %}
                    </th>
                </tr>

                {% for field in form %}
                    {% if field.label == "register" %}
                    {% else %}
                        <tr style="padding:0px; font-size:11px">
                            <td></td>
                            <td style="margin:0;" >
                                {{ field.label }}
                            </td>
                            <td width="80%" style="padding:0; height:12px; line-height:12px;">
                                {{ field }}
                                {% for err in field.errors %}
                                    <p class="help-inline error">{{ err }}</p>
                                {% endfor %}

                            </td>
                        </tr>
                   {% endif %}
                {% endfor %}
                <tr>
                    <td colspan=3>
                        <code>{{ form.command_line }}</code>
                    </td>
                </tr>
                <tr>
                    <td colspan=3>
                        <button class="pull-right btn" type="submit">{% trans "Save" %}</button>
                    </td>
                </tr>

        {% endfor %}
        </table>

    </form>
{% else %}
    {% trans "Host not found." %}
{% endif %}

{% endblock %}

{% block footer %}
    {{ block.super }}
    <script>
    $(document).ready(function() {
        {% if myhost %}
            var parameters = { object_id: '{{ myhost.id }}' };
            adagios.rest.pynag.run_check_command(parameters)
                    .done( function(data) {
                        var host_status = $('span#host_status');
                        var host_status_output = $('span#host_status_output');
                        host_status_output.text(data[1]);
                        if (parseInt(data[0]) < 1) {
                            host_status.text('UP');
                            host_status.addClass('label-success');
                        } else {
                            host_status.text('DOWN');
                            host_status.addClass('label-danger');
                        }
                }).error(function() {
                    host_status.text('Unknown');
                });
        {% endif %}
        $('a.service_status.run_check_command').each(function() {
            var $this = $(this);
            var parameters = { object_id: $this.attr('data-object-id') };
            adagios.rest.pynag.run_check_command(parameters)
                    .done( function(data) {
                                    var returncode = parseInt(data[0]);
                                    var output = "";
                                    if (data[1]) {
                                        output += data[1];
                                    } else {
                                        output += '{% trans "No data returned on stdout" %}';
                                    }
                                    if (data[2]) {
                                        output += "<br/><br/><strong>{% trans "Stderr Output:" %}</strong><br/>" + data[2];
                                    }
                                    $this.attr('data-content', output);

                                    if (returncode == 0) {
                                        $this.text('OK');
                                        $this.addClass('btn-success');
                                        return true;
                                    }
                                    if (returncode == 1) {
                                        $this.text('Warning');
                                        $this.addClass('btn-warning');
                                        return true;
                                    }
                                    if (returncode == 2) {
                                        $this.text('Critical');
                                        $this.addClass('btn-danger');
                                        return true;
                                    }
                                    $this.text('Unknown');
                                    $this.addClass('btn-inverse');
                                    return true;
                    });

        });
        $('a[rel="popover"]').popover();
    });
    //$(".collapse").collapse()
    </script>
{% endblock %}
