{% extends "base.html" %}

{% block title %}IceProd{% end %}

{% block page_title %}Tasks by Status{% end %}

{% block body %}

<table class='task_table'>
    <tr>
        <th>Job / Task Index</th>
        <th>Name</th>
        <th>Failures</th>
        {% if (current_user) %}
        <th><button class="set_status" onclick='task_reset(task_ids_on_page);'>Reset All</button></th>
        <th><button class="set_status" onclick='task_hard_reset(task_ids_on_page);'>Hard Reset All</button></th>
        <th><button class="set_status" onclick='task_suspend(task_ids_on_page);'>Suspend All</button></th>
        {% end %}
    </tr>
    {% for t in tasks %}
    <tr>
        <td><a href="/dataset/{{ url_escape(tasks[t]['dataset_id']) }}/task/{{ url_escape(t) }}">{{ tasks[t]['job_index'] }} / {{ tasks[t]['task_index'] }}</a></td>
        <td>{{ tasks[t]['name'] }}</td>
        <td>{{ tasks[t]['failures'] }}</td>
        {% if (current_user) %}
        <td>{% if tasks[t]['status'] in states.task_prev_statuses(states.TASK_STATUS_START) %}
        <button class="set_status" onclick='task_reset(["{{ tasks[t]["task_id"] }}"]);'>Reset</button>
        {% end %}</td>
        <td><button class="set_status" onclick='task_hard_reset(["{{ tasks[t]["task_id"] }}"]);'>Hard Reset</button></td>
        <td>{% if tasks[t]['status'] in states.task_prev_statuses('suspended') %}
        <button class="set_status" onclick='task_suspend(["{{ tasks[t]["task_id"] }}"]);'>Suspend</button>
        {% end %}</td>
        {% end %}
    </tr>
    {% end %}
</table>

{% end %}

{% block body_scripts %}

{% if (current_user) %}
<script type="text/javascript" src="/static/fetch.js"></script>
<script type="text/javascript" src="/static/rest.js"></script>
<script type="text/javascript">
var rest_api = '{{ rest_api }}';
const passkey = "{{ passkey }}";
const dataset_id = "{{ list(tasks.values())[0]['dataset_id'] if tasks else '' }}";
const task_ids_on_page = [
    {% for t in tasks %}
        "{{tasks[t]['task_id']}}",
    {% end %}
];

async function task_reset(task_ids) {
    try {
        const url = '/datasets/' + dataset_id + '/task_actions/bulk_reset';
        const ret = await fetch_json('POST', url, {'tasks': task_ids}, passkey);
        if ('error' in ret) {
            message_alert('error - '+ret['error']);
            return false;
        }
    } catch(err) {
        message_alert('error - '+err);
        return false;
    }
    reload();
    return true;
}

async function task_hard_reset(task_ids) {
    try {
        const url = '/datasets/' + dataset_id + '/task_actions/bulk_hard_reset';
        const ret = await fetch_json('POST', url, {'tasks': task_ids}, passkey);
        if ('error' in ret) {
            message_alert('error - '+ret['error']);
            return false;
        }
    } catch(err) {
        message_alert('error - '+err);
        return false;
    }
    reload();
    return true;
}

async function task_suspend(task_ids) {
    try {
        const url = '/datasets/' + dataset_id + '/task_actions/bulk_suspend';
        const ret = await fetch_json('POST', url, {'tasks': task_ids}, passkey);
        if ('error' in ret) {
            message_alert('error - '+ret['error']);
            return false;
        }
    } catch(err) {
        message_alert('error - '+err);
        return false;
    }
    reload();
    return true;
}
{% end %}
</script>
{% end %}
