{% extends 'base.html' %}
{% load helpers %}
{% load form_helpers %}
{% load log_levels %}
{% load static %}

{% block title %}{{ script }} - {{ result.get_status_display }}{% endblock %}

{% block content %}
    <div class="row noprint">
        <div class="col-md-12">
            <ol class="breadcrumb">
                <li><a href="{% url 'extras:script_list' %}">Scripts</a></li>
                <li><a href="{% url 'extras:script_list' %}#module.{{ script.module }}">{{ script.module|bettertitle }}</a></li>
                <li><a href="{% url 'extras:script' module=script.module name=class_name %}">{{ script }}</a></li>
                <li>{{ result.created }}</li>
            </ol>
        </div>
    </div>
    <h1>{{ script }}</h1>
    <p>{{ script.Meta.description }}</p>
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active">
            <a href="#log" role="tab" data-toggle="tab" class="active">Log</a>
        </li>
        <li role="presentation">
            <a href="#output" role="tab" data-toggle="tab">Output</a>
        </li>
        <li role="presentation">
            <a href="#source" role="tab" data-toggle="tab">Source</a>
        </li>
    </ul>
    <div class="tab-content">
        <p>
            Run: <strong>{{ result.created }}</strong>
            {% if result.completed %}
                Duration: <strong>{{ result.duration }}</strong>
            {% else %}
                <img id="pending-result-loader" src="{% static 'img/ajax-loader.gif' %}" />
            {% endif %}
            <span id="pending-result-label">{% include 'extras/inc/job_label.html' with result=result %}</span>
        </p>
        <div role="tabpanel" class="tab-pane active" id="log">
            {% if result.completed %}
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <strong>Script Log</strong>
                            </div>
                            <table class="table table-hover panel-body">
                                <tr>
                                    <th>Line</th>
                                    <th>Level</th>
                                    <th>Message</th>
                                </tr>
                                {% for log in result.data.log %}
                                    <tr>
                                        <td>{{ forloop.counter }}</td>
                                        <td>{% log_level log.status %}</td>
                                        <td class="rendered-markdown">{{ log.message|render_markdown }}</td>
                                    </tr>
                                {% empty %}
                                    <tr>
                                        <td colspan="3" class="text-center text-muted">
                                            No log output
                                        </td>
                                    </tr>
                                {% endfor %}
                            </table>
                            {% if execution_time %}
                                <div class="panel-footer text-right text-muted">
                                    <small>Exec time: {{ execution_time|floatformat:3 }}s</small>
                                </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
            {% else %}
                <div class="row">
                    <div class="col-md-12">
                        <div class="well">Pending results</div>
                    </div>
                </div>
            {% endif %}
        </div>
        <div role="tabpanel" class="tab-pane" id="output">
            <pre>{{ result.data.output }}</pre>
        </div>
        <div role="tabpanel" class="tab-pane" id="source">
            <p><code>{{ script.filename }}</code></p>
            <pre>{{ script.source }}</pre>
        </div>
    </div>
{% endblock %}


{% block javascript %}
<script type="text/javascript">
{% if not result.completed %}
var pending_result_id = {{ result.pk }};
{% else %}
var pending_result_id = null;
{% endif %}

function jobTerminatedAction(){
    refreshWindow()
}

</script>
<script src="{% static 'js/job_result.js' %}?v{{ settings.VERSION }}"
        onerror="window.location='{% url 'media_failure' %}?filename=js/job_result.js'"></script>
{% endblock %}