{% 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 Admin" %}</h2>
<hr>
{% endblock %}

{% block content %}

<form method="get" class="mb-3 d-flex gap-2 align-items-center">
    <label class="form-label mb-0 text-nowrap">{% trans "State" %}</label>
    <select name="state" class="form-select form-select-sm" style="width:10rem">
        {% for s in states %}
        <option value="{{ s }}" {% if s == state_filter %}selected{% endif %}>{{ s|default:"All" }}</option>
        {% endfor %}
    </select>
    <input type="text" name="search" value="{{ search }}" placeholder="{% trans 'Search name, owner…' %}"
           class="form-control form-control-sm">
    <button type="submit" class="btn btn-sm btn-outline-secondary text-nowrap">{% trans "Filter" %}</button>
</form>

<table class="table table-sm table-hover">
    <thead>
        <tr>
            <th>{% trans "Name" %}</th>
            <th>{% trans "Owner" %}</th>
            <th>{% trans "Type" %}</th>
            <th>{% trans "State" %}</th>
            <th>{% trans "Progress" %}</th>
            <th>{% trans "Created" %}</th>
            <th>{% trans "Finished" %}</th>
            <th>{% trans "Duration" %}</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        {% for task in tasks %}
        <tr class="cv-row-clickable" onclick="location.href='{% url 'app_data:task_detail' task.handle %}'">
            <td>{{ task.name }}</td>
            <td>{{ task.owner_id }}</td>
            <td class="text-muted">{{ task.owner_type }}</td>
            <td><span class="datatask-state datatask-state-{{ task.state|lower }}">{{ task.state }}</span></td>
            <td>
                {% if task.progress %}
                {{ task.progress.percent|default:0 }}%
                {% endif %}
            </td>
            <td>{{ task.created_at|date:"Y-m-d H:i" }}</td>
            <td>{{ task.finished_at|date:"Y-m-d H:i"|default:"-" }}</td>
            <td>{% if task.duration %}{{ task.duration|floatformat:1 }}s{% else %}-{% endif %}</td>
            <td>
                {% if task.state == "QUEUED" or task.state == "RUNNING" %}
                <form method="post" action="{% url 'app_data:task_stop' task.handle %}">
                    {% csrf_token %}
                    <button type="submit" class="btn btn-sm btn-outline-danger">{% trans "Stop" %}</button>
                </form>
                {% endif %}
            </td>
        </tr>
        {% empty %}
        <tr><td colspan="9">{% trans "No tasks." %}</td></tr>
        {% endfor %}
    </tbody>
</table>

{% endblock %}
