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

{% load static django_rq %}

{% block extrastyle %}
    {{ block.super }}
    <link rel="stylesheet" href="{% static 'admin/css/forms.css' %}">
    <link rel="stylesheet" href="{% static 'admin/css/changelists.css' %}">
{% endblock %}

{% block title %}Cron Scheduler: {{ scheduler.name }}{% endblock %}

{% block content_title %}<h1>Cron Scheduler: {{ scheduler.name }}</h1>{% endblock %}

{% block breadcrumbs %}
    <div class="breadcrumbs">
        <a href="{% url 'admin:index' %}">Home</a> &rsaquo;
        <a href="{% rq_url 'home' %}">Django RQ</a> &rsaquo;
        Cron Scheduler: {{ scheduler.name }}
    </div>
{% endblock %}

{% block content %}
<div id="content-main">

    <fieldset class="module aligned">

        <div class="form-row">
            <div>
                <div class="flex-container">
                    <label>Name:</label>
                    <div class="readonly">{{ scheduler.name }}</div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class="flex-container">
                    <label>Hostname:</label>
                    <div class="readonly">{{ scheduler.hostname }}</div>
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <div class="flex-container">
                    <label>PID:</label>
                    <div class="readonly">{{ scheduler.pid }}</div>
                </div>
            </div>
        </div>

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

        <div class="form-row">
            <div>
                <div class="flex-container">
                    <label>Last Heartbeat:</label>
                    <div class="readonly">
                        {% if scheduler.last_heartbeat %}
                            {{ scheduler.last_heartbeat|to_localtime|date:"Y-m-d, H:i:s" }}
                        {% else %}
                            -
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>

        {% if scheduler.config_file %}
        <div class="form-row">
            <div>
                <div class="flex-container">
                    <label>Config File:</label>
                    <div class="readonly">{{ scheduler.config_file }}</div>
                </div>
            </div>
        </div>
        {% endif %}

    </fieldset>

    <div class="module" id="changelist">
        <h2>Cron Jobs</h2>
        <div class="results">
            {% if cron_jobs %}
            <table id="result_list">
                <thead>
                    <tr>
                        <th><div class="text"><span>Callable</span></div></th>
                        <th><div class="text"><span>Queue</span></div></th>
                        <th><div class="text"><span>Schedule</span></div></th>
                        <th><div class="text"><span>Next Enqueue</span></div></th>
                        <th><div class="text"><span>Latest Enqueue</span></div></th>
                        <th><div class="text"><span>Args</span></div></th>
                        <th><div class="text"><span>Kwargs</span></div></th>
                        <th><div class="text"><span>Meta</span></div></th>
                        <th><div class="text"><span>Options</span></div></th>
                    </tr>
                </thead>
                <tbody>
                    {% for cron_job in cron_jobs %}
                    <tr class="{% cycle 'row1' 'row2' %}">
                        <td>{{ cron_job.func_name|default:"-" }}</td>
                        <td>{{ cron_job.queue_name|default:"-" }}</td>
                        <td>{{ cron_job.schedule }}</td>
                        <td>
                            {% if cron_job.next_enqueue_time %}
                                {{ cron_job.next_enqueue_time|to_localtime|date:"Y-m-d, H:i:s" }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            {% if cron_job.latest_enqueue_time %}
                                {{ cron_job.latest_enqueue_time|to_localtime|date:"Y-m-d, H:i:s" }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>{{ cron_job.args|default_if_none:"-" }}</td>
                        <td>{{ cron_job.kwargs|default_if_none:"-" }}</td>
                        <td>{{ cron_job.meta|default_if_none:"-" }}</td>
                        <td>
                            {% for option in cron_job.options %}
                                {{ option }}{% if not forloop.last %}<br>{% endif %}
                            {% empty %}
                                -
                            {% endfor %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% else %}
            <p class="paginator">No persisted cron jobs found</p>
            {% endif %}
        </div>
    </div>

</div>
{% endblock %}
