{% extends "admin/change_list.html" %}

{% load static django_rq %}

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

{% block extrastyle %}
    {{ block.super }}
    <style>
        .data {
            display: inline-block;
            float: left;
            width: 80%;
            font-size: 12px;
            padding-top: 3px;
        }
    </style>
    <link href="{% static 'admin/css/forms.css' %}" type="text/css" rel="stylesheet">
{% endblock %}

{% block breadcrumbs %}
    <div class="breadcrumbs">
        <a href="{% url 'admin:index' %}">Home</a> &rsaquo;
        <a href="{% rq_url 'home' %}">Django RQ</a> &rsaquo;
        <a href = "{% rq_url 'jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
        <a href = "{% rq_url 'job_detail' queue_index job.id %}">{{ job.id }}</a>
    </div>
{% endblock %}

{% block content_title %}<h2>Job {{ job.id }}</h2>{% endblock %}

{# do not render object-tools (fix until https://github.com/django/django/pull/19389/files is released) #}
{% block object-tools %}{% endblock %}

{% block content %}

<div id="content-main">

    <fieldset class="module aligned ">

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Queue:</label>
                    <div class="readonly">{{ job.origin }}</div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Timeout:</label>
                    <div class="readonly">{{ job.timeout }}</div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Result TTL:</label>
                    <div class="readonly">{{ job.result_ttl }}</div>
                </div>
            </div>
        </div>

        {% if job.created_at %}
        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Created:</label>
                    <div class="readonly">{{ job.created_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
                </div>
            </div>
        </div>
        {% endif %}

        {% if job.enqueued_at %}
        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Queued:</label>
                    <div class="readonly">{{ job.enqueued_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
                </div>
            </div>
        </div>
        {% endif %}

        {% if job.started_at %}
        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Started:</label>
                    <div class="readonly">{{ job.started_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
                </div>
            </div>
        </div>
        {% endif %}

        {% if job.ended_at %}
        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Ended:</label>
                    <div class="readonly">{{ job.ended_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
                </div>
            </div>
        </div>
        {% endif %}

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Status:</label>
                    <div class="readonly">{{ job.get_status.value }}</div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Callable:</label>
                    <div class="readonly">{{ job|show_func_name }}</div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Meta:</label>
                    <div class="data">{{ job.meta }}</div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Args:</label>
                    <div>

                        {% if data_is_valid %}
                            {% if job.args %}
                            <ul>
                                {% for arg in job.args %}
                                    <li>{{ arg|force_escape }}</li>
                                {% endfor %}
                            </ul>
                            {% endif %}
                        {% else %}
                            Unpickling Error
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Kwargs:</label>
                    <div>
                        {% if data_is_valid %}
                            {% if job.kwargs %}
                                <ul>
                                    {% for key, value in job.kwargs|items %}
                                        <li>{{ key }}: {{ value|force_escape }}</li>
                                    {% endfor %}
                                </ul>
                            {% endif %}
                        {% else %}
                            Unpickling Error
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        {% if dependencies %}
        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Depends On:</label>
                    <div class="readonly">

                        {% for dependency in dependencies %}
                            {% if not forloop.first %}
                                <br>
                            {% endif %}
                            {% if dependency.1 %}
                                {{ dependency.1.func_name }}
                            {% else %}
                                Deleted
                            {% endif %}
                            <a href = "{% rq_url 'job_detail' queue_index dependency.0 %}">
                                ({{ dependency.0 }})
                            </a>
                        {% endfor %}

                    </div>
                </div>
            </div>
        </div>
        {% endif %}

        {% if dependents %}
        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Dependents:</label>
                    <div class="readonly">

                        {% for dependent in dependents %}
                            {% if not forloop.first %}
                                <br>
                            {% endif %}
                            {% if dependent.1 %}
                                {{ dependent.1.func_name }}
                            {% else %}
                                Deleted
                            {% endif %}
                            <a href = "{% rq_url 'job_detail' queue_index dependent.0 %}">
                                ({{ dependent.0 }})
                            </a>
                        {% endfor %}

                    </div>
                </div>
            </div>
        </div>
        {% endif %}

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

        {% if job.legacy_result %}
        <div class="form-row">
            <div>
                <div class = "flex-container">
                    <label>Result:</label>
                    <div class="readonly">{{ job.result }}</div>
                </div>
            </div>
        </div>
        {% endif %}

    </fieldset>


    <div class="submit-row">
        <div class="deletelink-box"><a href="delete/" class="deletelink">Delete</a></div>
        {% if job.is_started %}
            <form method = 'POST' action = "{% rq_url 'stop_job' queue_index job.id %}">
                {% csrf_token %}
                <input type="submit" value="Stop" class="deletelink" name="stop">
            </form>
        {% endif %}
        {% if job.is_failed %}
            <form method = 'POST' action = "{% rq_url 'requeue_job' queue_index job.id %}">
                {% csrf_token %}
                <input type="submit" value="Requeue" class="default" name="requeue">
            </form>
        {% endif %}
        {% if not job.is_queued and not job.is_failed %}
            <form method = 'POST' action = "{% rq_url 'enqueue_job' queue_index job.id %}">
                {% csrf_token %}
                <input type="submit" value="Enqueue" class="default" name="Enqueue">
            </form>
        {% endif %}
    </div>

    {% if job.results %}
    <div id="job-results">
        <h3>Results</h3>
        <div class="results">
            <table id="result_list">
                <thead>
                    <tr>
                        <th scope="col" class="sortable">
                            <div class="text"><span>ID</span></div>
                            <div class="clear"></div>
                        </th>
                        <th scope="col" class="sortable">
                            <div class="text"><span>Type</span></div>
                            <div class="clear"></div>
                        </th>
                        <th scope="col" class="sortable">
                            <div class="text"><span>Created At</span></div>
                            <div class="clear"></div>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    {% for result in job.results %}
                    <tr>
                        <th>
                            <a href="{% rq_url 'result_detail' queue_index job.id result.id %}">{{ result.id }}</a>
                        </th>
                        <td>{{ result.type.name }}</td>
                        <td>{{ result.created_at|to_localtime|date:"Y-m-d, H:i:s" }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}

</div>

{% endblock %}
