$tasks = null;
$agentState = null;
$agentInfo = null;
$managerStart = null;
$managerStop = null;

function managerInit(Interval) {
    $tasks = $('#tasks tbody');
    $agentState = $('#agent-state');
    $agentInfo = $('#agent-info');
    $managerStart = $('#manager-start');
    $managerStop = $('#manager-stop');
    $managerStart.hide();
    $managerStop.hide();

    $.ajaxSetup({
        cache: false
    });

    Refresh();

    setInterval(Refresh, Interval);
}

function managerStart() {
    $.get("/ra/manager/start");
}

function managerStop() {
    $.get("/ra/manager/stop");
}

function taskStart(id) {
    $.get("/ra/tasks/" + id + "/start");
}

function taskStop(id) {
    $.get("/ra/tasks/" + id + "/stop");
}

function Refresh() {
    var jqxhr = $.get("/ra/tasks", function (data) {
        $tasks.empty();

        if (data) {
            var $agent = $(data).find('agent');
            
            $agentState.removeClass('label-important');
            $agentState.removeClass('label-success');
            $agentState.hide();
            if ($agent.attr('active') == "1") {
                $agentState.addClass('label-success');
                $agentState.text('Подключено');
                $managerStart.hide();
                $managerStop.show();
            } else {
                $agentState.text('Остановлен');
                $managerStart.show();
                $managerStop.hide();
            }
            $agentInfo.text($agent.attr('app') + '  (' + $agent.attr('host') + ':' + $agent.attr('port') + ')');

            $agent.find('task').each(function () {
                
                var row = $('<tr id="task' + $(this).attr('id') + '"></tr>');
                row.appendTo($tasks);
                if ($(this).attr('overdued') == "1") {
                    row.append('<td class="text-warning"><i class="icon-exclamation-sign"></i>&nbsp;Просрочено</td>');
                    row.addClass('warning');
                }
                else if ($(this).attr('executing') == "1") {
                    row.append('<td class="text-success"><i class="icon-play"></i>&nbsp;Выполняется</td>');
                }
                else if ($(this).attr('enabled') != "1") {
                    row.append('<td class="muted"><i class="icon-ban-circle"></i>&nbsp;Остановлено</td>');
                }
                else if ($(this).attr('error')) {
                    row.append('<td class="text-error"><i class="icon-warning-sign"></i>&nbsp;<span class="label label-important">Ошибка</span></td>')
                    row.addClass('error');
                }
                else
                    row.append('<td><i class="icon-time"></i>&nbsp;Ожидание</td>');

                var dd = '<td>';
                dd += $(this).attr('name');
                if ($(this).attr('enabled') == "1") {
                    dd += '<button class="btn btn-mini pull-right" data-toggle="tooltip" title="Отключить" onclick="taskStop(' + $(this).attr('id') + ');"><i class="icon-stop"></i></button>';
                } else {
                    dd += '<button class="btn btn-mini pull-right" data-toggle="tooltip" title="Включить" onclick="taskStart(' + $(this).attr('id') + ');"><i class="icon-play"></i></button>'
                }

                row.append(dd);

                row.append('<td>' + $(this).attr('duration') + '</td>');
                row.append('<td>' + $(this).attr('last') + '</td>');
                row.append('<td>' + $(this).attr('next') + '</td>');
                row.append('<td>' + $(this).attr('error') + '</td>');

            });

            
            $('#tasks button').tooltip({ placement: "right" });
        }
    })
            .fail(function () {
                $agentInfo.text('');
                $tasks.empty();
                $agentState.removeClass('label-success');
                $agentState.addClass('label-important');
                $agentState.text('Нет соединения');
                $agentState.show();
                $managerStart.hide();
                $managerStop.hide();
            });
}
