{% extends base.html %}

{% block title %}Worker Interface{% end %}

{% block content %}
<!-- Worker Info Area -->
<div class="row-fluid" id="workerInfo">
</div>
<!-- End Worker Info Area -->

<script>
var mySpace = {
                    worker_api_url:"{{ worker_api_url }}",
                    targets_api_url:"{{ targets_api_url }}",
                    targets_ui_url:"{{ targets_ui_url }}",
                    target_configs:{},
                };

// Function to abort a worker using API
function abortWorker(){
    $.get(mySpace.worker_api_url+'/abort');
}

function pauseWorker(){
    $.get(mySpace.worker_api_url+'/pause');
}

function resumeWorker(workerId){
    $.get(mySpace.worker_api_url+'/resume');
}

function getTargetConfigs(){
    $.getJSON(mySpace.targets_api_url, function(data){
            $.each(data, function(index, obj){
                mySpace.target_configs[obj.ID] = obj;
            });
        });
}

function getControlButtons(){
    html = '<div class="btn-group pull-right">';
    if (obj.busy){
        if(obj.paused){
            html += '<button class="btn btn-success btn-xs" href="#" onclick="resumeWorker();"><i class="fa fa-play"></i></button>';
        }else{
            html += '<button class="btn btn-primary btn-xs" href="#" onclick="pauseWorker();"><i class="fa fa-pause"></i></button>';
        }
    }
    html += '<button class="btn btn-danger btn-xs" href="#" onclick="abortWorker();"><i class="fa fa-times"></i></button>';
    html += '</div>';
    return(html);
}

function displayWorkersInfo(){
    $.ajax({url: mySpace.worker_api_url,
            ifModified: true,
            dataType:'json',
            success:function(obj, textStatus, jqXHR){
                if (304 != jqXHR.status){
                }
            }
    });
    setTimeout(displayWorkersInfo, 5000);
}

$(document).ready(function() {
        getTargetConfigs();
        displayWorkersInfo();
});
</script>
{% end %}
