{% extends "header.html" %}

<!-- Macro for toggle button to display _output fields of checks -->
{% macro toggle_output(output, target, title) %}
    <button class="commonstyle own-line btn btn-default btn-sm" data-toggle="collapse" data-target={{'#' + target}}>
        {{title}}
    </button>
    <div class="collapse" id={{target}}>
        <pre class="commonstyle"><code>{{output}}</code></pre>
    </div>
{% endmacro %}

<!-- Macro for one check result -->
{% macro check_result(check, title, environment, is_admin, context) %}
    <!-- check-result is a reserved class name -->
    {% set check_summary = check['summary'] if check.get('summary') != None else '' %}
    <div class="{{'check-' + check['status'].lower() + ' boxstyle'}} check-result">
        <h5 style='margin:10px 5px 10px 5px;'}}>
            <div class='row'>
                <div class="col-sm-4">
                    <div class="col-sm-1" style="padding:0;" align="center">
                        {% if check.get('assc_action_status')%}
                            <span class="{{'action-icon assc-action-' + check['assc_action_status'].lower()}}"></span>
                        {% endif %}
                    </div>
                    <div class="col-sm-11" style="padding:0;">
                        <a class="title-hover" data-toggle="collapse" id={{'toggle-' + environment + '-' + check['name']}} href={{'#' + environment + '-' + check['name']}}>
                            {{title}}
                        </a>
                    </div>
                </div>
                <div class="col-sm-6">
                    <span class="opacity-transition" id={{'summary-' + environment + '-' + check['name']}} style="font-weight:350;">
                        {{check_summary[:77] + '...' if (check_summary and check_summary|length > 80) else check_summary}}
                    </span>
                </div>
                <div class="col-sm-2 pull-right">
                    <span style="font-weight:350;">
                        {{check.get('local_time', 'No timestamp')}}
                    </span>
                </div>
            </div>
        </h5>
        <div class="collapse collapse-div commonstyle container-fluid" id={{environment + '-' + check['name']}}>
            <hr style="margin:0px 0px 8px 0px; border-top:1px solid #ccc;">
            <div class='row'>
                <div class="col-sm-9">
                    {% if check.get('description') %}
                        <div class="commonstyle own-line">{{check['description']}}</div>
                    {% endif %}
                    {% if check.get('ff_link') %}
                        <a href={{check['ff_link']}} target="_blank" class="commonstyle own-line">{{check['ff_link']}}</a>
                    {% endif %}
                    {% if check.get('brief_output')%}
                        {{toggle_output(check['brief_output'], environment + '-' + check['name'] + '-brief', 'Toggle brief output')}}
                    {% endif %}
                    {% if check.get('full_output')%}
                        {{toggle_output(check['full_output'], environment + '-' + check['name'] + '-full', 'Toggle full output')}}
                    {% endif %}
                    {% if check.get('admin_output') and is_admin %}
                        {{toggle_output(check['admin_output'], environment + '-' + check['name'] + '-admin', 'Toggle admin output')}}
                    {% endif %}
                    {% if check.get('assc_action') %}
                        {{toggle_output(check['assc_action'], environment + '-' + check['name'] + '-assc-action', 'Toggle associated action')}}
                    {% endif %}
                    {% if check.get('action') and is_admin %}
                        <form class="commonstyle own-line" action={{context + "view_run/" + environment + '/' + check['action'] + '/action'}} method="GET">
                            <input type="hidden" name="check_name" value={{check['name']}} />
                            <input type="hidden" name="called_by" value={{check['kwargs']['uuid']}} />
                            {% if not check.get('allow_action') %}
                                <button id={{"action-" + check['name'] + "-" + check['kwargs']['uuid']}} class="btn btn-warning btn-sm" disabled>
                                    {{' '.join(check['action'].split('_')).title()}}
                                </button>
                            {% else %}
                                <button id={{"action-" + check['name'] + "-" + check['kwargs']['uuid']}} class="btn btn-warning btn-sm" onclick="{{"return confirm_action('" + check['name'] + "-" + check['kwargs']['uuid'] + "', '" + check.get('action_message', 'Confirm run?') + "');" | safe}}" type="button">
                                    {{' '.join(check['action'].split('_')).title()}}
                                </button>
                            {% endif %}
                        </form>
                        <div id={{"action-message-" +check['name'] + "-" + check['kwargs']['uuid']}}></div>
                    {% endif %}
                </div>
                <div class="col-sm-3 pull-right" style="text-align: right;">
                    <a class="commonstyle own-line" target="_blank" href={{context + 'history/' + environment + '/' + check['name']}}>
                        Result History
                    </a>
                    {% if check.get('action_history') %}
                      <a class="commonstyle own-line" target="_blank" href={{context + 'history/' + environment + '/' + check['action_history']}}>
                        Action History
                      </a>
                    {% endif %}
                    {% if check.get('kwargs') %}
                        <form action={{context + 'view_run/' + environment + '/' + check['name'] + '/check'}} method="GET" target="_blank">
                            {% for kwarg in check['kwargs'] %}
                                {% if kwarg not in ['uuid', '_run_info', 'runtime_seconds', 'queue_action'] %}
                                    <span style="font-weight: bold; margin-right: 5px">{{kwarg}}</span>
                                    {% if is_admin %}
                                        <input style="width: 35%;" type="text" name={{kwarg}} value="{{check['kwargs'][kwarg]}}" /><br>
                                    {% else %}
                                        <span>{{check['kwargs'][kwarg]}}</span><br>
                                    {% endif %}
                                {% elif kwarg == 'queue_action' %}
                                    {% if check.get('action') %}
                                        <span style="font-weight: bold; margin-right: 5px">{{kwarg}}</span>
                                        {% if is_admin %}
                                            <input style="width: 35%;" type="text" name={{kwarg}} value="{{check['kwargs'][kwarg]}}" /><br>
                                        {% else %}
                                            <span>{{check['kwargs'][kwarg]}}</span><br>
                                        {% endif %}
                                    {% endif %}
                                {% endif %}
                            {% endfor %}
                            {% if is_admin %}
                                <button style="margin-top: 5px;" class="btn btn-default btn-sm" type="submit">
                                    Queue check
                                </button>
                            {% endif %}
                        </form>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endmacro %}
