{% extends "base.html" %}

{% block title %}Jobs - IceProd{% end %}

{% block page_title %}Jobs{% end %}

{% block body %}

<table class='task_table'>
    <tr>
        <th>Job Index</th>
        <th>Status</th>
        <th>Status Changed</th>
        {% if (current_user) %}
        <th><button class="set_status" onclick='set_jobs_status(dataset_id, job_ids_on_page, "processing", "{{ passkey }}", ["queued", "processing", "errors", "suspended"]);'>Reset All</button></th>
        <th><button class="set_status" onclick='set_jobs_status(dataset_id, job_ids_on_page, "processing", "{{ passkey }}");'>Hard Reset All</button></th>
        <th><button class="set_status" onclick='set_jobs_status(dataset_id, job_ids_on_page, "suspended", "{{ passkey }}", ["idle", "waiting", "queued", "processing", "reset"]);'>Suspend All</button></th>
        {% end %}
    </tr>
    {% for j in jobs %}
    <tr>
        <td><a href="/dataset/{{ url_escape(jobs[j]['dataset_id']) }}/job/{{ url_escape(jobs[j]['job_id']) }}">{{ jobs[j]['job_index'] }}</a></td>
        <td>{{ jobs[j]['status'] }}</td>
        <td>{{ jobs[j]['status_changed'] }}</td>
        {% if (current_user) %}
        <td><button class="set_status" onclick='set_jobs_status(dataset_id, ["{{ jobs[j]['job_id'] }}"], "processing", "{{ passkey }}", ["queued", "processing", "errors", "suspended"]);'>Reset</button></td>
        <td><button class="set_status" onclick='set_jobs_status(dataset_id, ["{{ jobs[j]['job_id'] }}"], "processing", "{{ passkey }}");'>Hard Reset</button></td>
        <td><button class="set_status" onclick='set_jobs_status(dataset_id, ["{{ jobs[j]['job_id'] }}"], "suspended", "{{ passkey }}", ["idle", "waiting", "queued", "processing", "reset"]);'>Suspend</button></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 dataset_id = "{{ list(jobs.values())[0]['dataset_id'] if jobs else '' }}";
var job_ids_on_page = [
    {% for j in jobs %}
        "{{ jobs[j]['job_id'] }}", 
    {% end %}
    ];
{% end %}
</script>
{% end %}
