{% extends 'layout/admin_page.html' %}

{% block title %}
    {% trans %}Tasks{% endtrans %}
{% endblock %}

{% block content %}
    <div class="tasks">
        <h2>{% trans %}Periodic Tasks{% endtrans %}</h2>
        <table class="i-table-widget fixed-width">
            <thead>
                <tr>
                    <th>{% trans %}Name{% endtrans %}</th>
                    <th>{% trans %}Schedule{% endtrans %}</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {%- for task in tasks %}
                    <tr>
                        <td>{{ task.name }}</td>
                        <td>{{ render_task_schedule(task) }}</td>
                        <td>{{ render_task_label(task) }}</td>
                    </tr>
                {%- endfor %}
            </tbody>
        </table>
    </div>
{% endblock %}

{% macro render_task_schedule(task) -%}
    {%- if not task.disabled -%}
        {% set schedule = task.custom_schedule or task.schedule %}
        {%- if schedule is instanceof timedelta -%}
            {%- trans interval=schedule|format_human_timedelta -%}every {{ interval }}{%- endtrans -%}
        {%- else -%}
            <span class="mono">{{ schedule }}</span>
        {%- endif -%}
    {%- endif -%}
{%- endmacro %}

{% macro render_task_label(task) -%}
    {%- if task.disabled -%}
        <span class="i-label danger right">{%- trans -%}disabled{%- endtrans -%}</span>
    {%- elif task.custom_schedule -%}
        <span class="i-label highlight right">{% trans %}custom schedule{% endtrans %}</span>
    {%- else -%}
        <span class="i-label right">{% trans %}default{% endtrans %}</span>
    {%- endif -%}
{%- endmacro %}
