{% extends "admin/scheduler/scheduler_base.html" %}
{% load static scheduler_tags %}

{% block title %}Job {{ job.id }} {{ block.super }}{% endblock %}

{% block breadcrumbs %}
    <div class="breadcrumbs">
        <a href="{% url 'admin:index' %}">Home</a> &rsaquo;
        <a href="{% url 'queues_home' %}">Queues</a> &rsaquo;
        <a href="{% url 'queue_registry_jobs' queue.name 'queued' %}">{{ queue.name }}</a> &rsaquo;
        <a href="{% url 'job_details' job.name %}">{{ job.name }}</a>
    </div>
{% endblock %}

{% block content_title %}
    <h2>Job {{ job.name }}
        {% if job.is_scheduled_task %}
            <small>
                <a href="{{ job|scheduled_task }}">Link to scheduled job</a>
            </small>
        {% endif %}
    </h2>
{% endblock %}

{% block content %}
    <div id="content-main">
        <fieldset class="module aligned">
            <div class="form-row">
                <label>Queue:</label>
                <div class="readonly">{{ job.queue_name }}</div>
            </div>

            <div class="field-row">
                <div class="flex-container form-multiline">
                    <div class="fieldBox">
                        <label>Direct callable:</label>
                        <div class="readonly">
                            {% if data_is_valid %}
                                {{ job.func_name }}(
                                {% if job.args %}
                                    {% for arg in job.args %}
                                        {{ arg|force_escape }},
                                    {% endfor %}
                                {% endif %}
                                {% for key, value in job.kwargs.items %}
                                    {{ key }}={{ value|force_escape }},
                                {% endfor %})
                            {% else %}
                                Unpickling Error
                            {% endif %}
                        </div>
                    </div>
                    <div class="fieldBox">
                        <label>Scheduler callable:</label>
                        <div class="data">{{ job | show_func_name }}</div>
                    </div>

                    <div class="fieldBox">
                        <label>Meta:</label>
                        <div class="data">
                            {% for k in job.meta %}
                                <div class="form-row">
                                    <label>{{ k }}</label>
                                    <div class="data">{{ job.meta | get_item:k }}</div>
                                </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>

            <div class="field-row">
                <div class="flex-container form-multiline">
                    <div class="fieldBox">
                        <label>Timeout:</label>
                        <div class="readonly">{{ job.timeout }}</div>
                    </div>
                    <div class="fieldBox">
                        <label>Result TTL:</label>
                        <div class="readonly">{{ job.success_ttl }}</div>
                    </div>
                </div>
            </div>
        </fieldset>
        <fieldset class="module aligned">
            <h2 class="fieldset-heading">Job queue interation</h2>
            <div class="field-row">
                <div class="flex-container form-multiline">
                    <div class="fieldBox">
                        <label>Status:</label>
                        <div class="readonly">{{ job.status.value | capfirst }}</div>
                    </div>
                    <div class="fieldBox">
                        <label>Created:</label>
                        <div class="readonly">{{ job.created_at|date:"Y-m-d, H:i:s"|default:"-" }}</div>
                    </div>

                    <div class="fieldBox">
                        <label>Queued:</label>
                        <div class="readonly">{{ job.enqueued_at|date:"Y-m-d, H:i:s"|default:"-" }}</div>
                    </div>

                    <div class="fieldBox">
                        <label>Started:</label>
                        <div class="readonly">{{ job.started_at|date:"Y-m-d, H:i:s"|default:"-" }}</div>
                    </div>

                    <div class="fieldBox">
                        <label>Ended:</label>
                        <div class="readonly">{{ job.ended_at|date:"Y-m-d, H:i:s"|default:"-" }}</div>
                    </div>
                </div>
            </div>
        </fieldset>
        <fieldset class="module aligned">
            <h2 class="fieldset-heading">Last result</h2>
            <div class="flex-container form-multiline">
                <div class="fieldBox">
                    <label>Created at:</label>
                    <div class="readonly">{{ last_result.created_at|date:"Y-m-d, H:i:s" }}</div>
                </div>
                <div class="fieldBox">
                    <label>Worker name:</label>
                    <div class="readonly">
                        {% if last_result.worker_name %}
                            <a href="{% url 'worker_details' last_result.worker_name %}"> {{ last_result.worker_name }}</a>
                        {% else %}
                            -
                        {% endif %}
                    </div>
                </div>
            </div>

            {% if last_result.exc_string %}
                <div class="form-row">
                    <label>Exception:</label>
                    <div>
                        <pre>{{ last_result.exc_string|default:"-"|linebreaks }}</pre>
                    </div>
                </div>
            {% endif %}

            <div class="form-row">
                <label>Result return value:</label>
                <div>
                    <pre>{{ last_result.return_value|default:'-'|linebreaks }}</pre>
                </div>
            </div>
        </fieldset>
        <div class="submit-row">
            <div class="deletelink-box">
                <a href="{% url 'job_detail_action' job.name 'delete' %}" class="deletelink">Delete</a>
            </div>
            {% if job.is_started %}
                <div class="deletelink-box">
                    <form method='POST' action="{% url 'job_detail_action' job.name 'cancel' %}">
                        {% csrf_token %}
                        <input type="submit" value="cancel" class="default" name="cancel">
                    </form>
                </div>
            {% endif %}
            {% if job.is_failed %}
                <div class="deletelink-box">
                    <form method='POST' action="{% url 'job_detail_action' job.name 'requeue' %}">
                        {% csrf_token %}
                        <input type="submit" value="Requeue" class="default" name="requeue">
                    </form>
                </div>
            {% endif %}
            {% if not job.is_queued and not job.is_failed %}
                <div class="deletelink-box">
                    <form method='POST' action="{% url 'job_detail_action' job.name 'enqueue' %}">
                        {% csrf_token %}
                        <input type="submit" value="Enqueue" class="default" name="Requeue">
                    </form>
                </div>
            {% endif %}
        </div>
    </div>

    <div class="module aligned">
        <h2 class="fieldset-heading">All results</h2>
        {% for result in results %}
            <div class="form-row">
                <h2>Result from {{ result.created_at|date:"Y-m-d, H:i:s" }}</h2>
                <div class="fieldBox">
                    <label>Type:</label>
                    <div class="readonly">{{ result.type.value | capfirst }}</div>
                </div>
                {% if result.type.value == "successful" %}
                    <div class="fieldBox field-votes">
                        <div>
                            <label>Return value:</label>
                            <div>
                                <pre>{{ result.return_value }}</pre>
                            </div>
                        </div>
                    </div>
                {% elif result.type.value == "failed" %}
                    <div class="fieldBox field-votes">
                        <div>
                            <label>Exception:</label>
                            <div>
                                <pre>{{ result.exc_string }}</pre>
                            </div>
                        </div>
                    </div>
                {% elif result.type.value == "stopped" %}
                    <div class="fieldBox field-votes">
                        <div>
                            <label>Job stopped:</label>
                            <div>
                                <pre>{{ result.exc_string }}</pre>
                            </div>
                        </div>
                    </div>
                {% endif %}
            </div>
        {% endfor %}
    </div>


{% endblock %}
