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


{% block content %}
{% if myhost %}
    <div>
    <h3>{{ host_name }} information:</h3>
        <dl class="dl-horizontal">
            <dt>Host Name</dt>
            <dd>{{ myhost.host_name }}</dd>
            <dt>Address</dt>
            <dd>{{ myhost.address }}</dd>
            <dt>Status</dt>
            <dd><span id="host_status" class="label">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">Install Agent</a>
        <a href="{% url okconfig_.views.addtemplate %}/?host_name={{ myhost.host_name }}" class="btn btn-success">Add templates</a>
    </div>

    <h3>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">Save</button>
                    </td>
                </tr>

        {% endfor %}
        </table>

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

{% endblock %}

{% block footer %}
    {{ block.super }}
    <script>
    $(document).ready(function() {
        {% if myhost %}
        $.getJSON('{% url rest/pynag %}json/run_check_command',
                { object_id: '{{ myhost.id }}' },
                function(data) {
                    var host_status = $('span#host_status');
                    var host_status_output = $('span#host_status_output');
                    host_status_output.text(data[1]);
                    host_status.addClass('label-success');
                    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);
            $.getJSON('{% url rest/pynag %}json/run_check_command',
                    { object_id: $this.attr('data-object-id') },
                    function(data) {
                        var returncode = parseInt(data[0]);
                        var output = "";
                        if (data[1]) {
                            output += data[1];
                        } else {
                            output += 'No data returned on stdout';
                        }
                        if (data[2]) {
                            output += "<br/><br/><strong>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 %}