{% extends "app_home/base.html" %}
{% load static %}
{% load i18n %}

{% block navbar_app %}
{% include 'app_home/_sidebar.html' %}
{% endblock %}

{% block title %}
<h2><i class="fa fa-tasks"></i> &nbsp; {% trans "Task Detail" %}</h2>
<hr>
{% endblock %}

{% block content %}

<div class="mb-3">
    <a href="{% url 'app_data:task_list' %}" class="btn btn-sm btn-outline-secondary">
        <i class="fa fa-arrow-left fa-fw"></i> {% trans "Back to tasks" %}
    </a>
    {% if task.state == "QUEUED" or task.state == "RUNNING" %}
    <form method="post" action="{% url 'app_data:task_stop' task.handle %}" class="d-inline ms-2">
        {% csrf_token %}
        <input type="hidden" name="next" value="{% url 'app_data:task_detail' task.handle %}">
        <button type="submit" class="btn btn-sm btn-outline-danger">
            <i class="fa fa-stop fa-fw"></i> {% trans "Stop" %}
        </button>
    </form>
    {% endif %}
</div>

{% if task.state == "QUEUED" or task.state == "RUNNING" %}
<div id="datatask-detail-card" class="datatask-detail-card"
     hx-get="{% url 'app_data:task_detail' task.handle %}"
     hx-trigger="every 2s"
     hx-select="#datatask-detail-card"
     hx-swap="outerHTML">
{% else %}
<div id="datatask-detail-card" class="datatask-detail-card">
{% endif %}

    <table class="table table-sm datatask-detail-table">
        <tbody>
            <tr>
                <th>{% trans "Name" %}</th>
                <td>{{ task.name }}</td>
            </tr>
            <tr>
                <th>{% trans "State" %}</th>
                <td><span class="datatask-state datatask-state-{{ task.state|lower }}">{{ task.state }}</span></td>
            </tr>
            <tr>
                <th>{% trans "Handle" %}</th>
                <td><code>{{ task.handle }}</code></td>
            </tr>
            <tr>
                <th>{% trans "Owner" %}</th>
                <td>{{ task.owner_id }} <span class="text-muted">({{ task.owner_type }})</span></td>
            </tr>
            <tr>
                <th>{% trans "Singleton" %}</th>
                <td>{{ task.singleton|default:"-" }}</td>
            </tr>
            <tr>
                <th>{% trans "Created" %}</th>
                <td>{{ task.created_at|date:"Y-m-d H:i:s" }}</td>
            </tr>
            <tr>
                <th>{% trans "Finished" %}</th>
                <td>{{ task.finished_at|date:"Y-m-d H:i:s"|default:"-" }}</td>
            </tr>
            <tr>
                <th>{% trans "Duration" %}</th>
                <td>{% if task.duration %}{{ task.duration|floatformat:1 }}s{% else %}-{% endif %}</td>
            </tr>
            {% if estimated %}
            <tr>
                <th>{% trans "Estimated remaining" %}</th>
                <td>{{ estimated|floatformat:1 }}s</td>
            </tr>
            {% endif %}
            {% if task.progress and task.progress.percent != 100 %}
            <tr>
                <th>{% trans "Progress" %}</th>
                <td>
                    <div class="datatask-progress-bar-wrap">
                        <div class="datatask-progress-bar" style="width: {{ task.progress.percent|default:0 }}%"></div>
                    </div>
                    <div class="datatask-progress-text">
                        {{ task.progress.percent|default:0 }}%
                        {% if task.progress.message %} — {{ task.progress.message }}{% endif %}
                        {% if task.progress.count and task.progress.total %} ({{ task.progress.count }}/{{ task.progress.total }}){% endif %}
                    </div>
                </td>
            </tr>
            {% endif %}
            {% if output_items %}
            <tr>
                <th colspan="2" class="datatask-section-header">{% trans "Output" %}</th>
            </tr>
            {% for key, val in output_items %}
            <tr>
                <th class="datatask-detail-subkey">{{ key }}</th>
                <td><pre class="datatask-detail-json">{{ val }}</pre></td>
            </tr>
            {% endfor %}
            {% elif task.output %}
            <tr>
                <th>{% trans "Output" %}</th>
                <td><pre class="datatask-detail-json">{{ task.output }}</pre></td>
            </tr>
            {% endif %}
            {% if task.attachment %}
            <tr>
                <th>{% trans "Attachment" %}</th>
                <td><pre class="datatask-detail-json">{{ task.attachment }}</pre></td>
            </tr>
            {% endif %}
            {% if params_items %}
            <tr>
                <th colspan="2" class="datatask-section-header">{% trans "Params" %}</th>
            </tr>
            {% for key, val in params_items %}
            <tr>
                <th class="datatask-detail-subkey">{{ key }}</th>
                <td><pre class="datatask-detail-json">{{ val }}</pre></td>
            </tr>
            {% endfor %}
            {% elif task.params %}
            <tr>
                <th>{% trans "Params" %}</th>
                <td><pre class="datatask-detail-json">{{ task.params }}</pre></td>
            </tr>
            {% endif %}
        </tbody>
    </table>

</div>

{% endblock %}
