$(function () {
    loadGrid();
    bindEvent();
});

function bindEvent() {
    $("#table_div").delegate('.disable', 'click', function (event) {
        var jobName = $(this).data("jobName");
        var url = g.ctx + 'pc/sys/job/pause'
        sendRequest(url, jobName);
    });
    $("#table_div").delegate('.able', 'click', function (event) {
        var jobName = $(this).data("jobName");
        var url = g.ctx + 'pc/sys/job/resume'
        sendRequest(url, jobName);
    });
    $("#table_div").delegate('.stop', 'click', function (event) {
        var jobName = $(this).data("jobName");
        var url = g.ctx + 'pc/sys/job/stop'
        sendRequest(url, jobName);
    });
    $("#table_div").delegate('.run', 'click', function (event) {
        var jobName = $(this).data("jobName");
        var url = g.ctx + 'pc/sys/job/run'
        sendRequest(url, jobName);
    });
}

function remove(jobName) {
    var url = g.ctx + 'pc/sys/job/remove'
    sendRequest(url, jobName);
}

function sendRequest(url, jobName) {
    $.ajax({
        url: url,
        method: "POST",
        data: {jobName: jobName},
        success: function (data) {
            $('#tb').datagrid("reload")
        },
        error: function (data) {
            topWin.layer.alert(data.responseJSON.message, {icon: 2});
        }
    });
}

function loadGrid() {
    $('#tb').datagrid({
        iconCls: 'icon-list',
        title: '',
        method: "GET",
        height: gridheight + 20,
        width: gridwidth,
        url: g.ctx + 'pc/sys/job/list',
        singleSelect: true,
        pagination: false,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        remoteSort: true,
        onLoadSuccess: function () {
        },
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'jobName',
                title: '定时任务名称',
                width: '170',
                align: 'center'

            },
            {
                field: 'scheduleTime',
                title: '执行时间',
                width: '110',
                align: 'center'

            },
            {
                field: 'lastFiredTime',
                title: '上次执行时间',
                width: '135',
                align: 'center'
            },
            {
                field: 'nextFireTime',
                title: '下次执行时间',
                width: '135',
                align: 'center'

            },
            {
                field: 'cronEx',
                title: 'CRON表达式',
                width: '125',
                align: 'center'
            },
            {
                field: 'jobStatus',
                title: '任务状态',
                width: '105',
                align: 'center',
                formatter: function (value, row) {
                    var txt = ''
                    switch (value) {
                        case 'SCHEDULED':
                            txt = "正常"
                            break
                        case 'PAUSED':
                            txt = "禁用"
                            break
                        case 'RUNNING':
                            txt = "正在执行"
                            break
                        case 'ERROR':
                            txt = "出错"
                            break
                        case 'BLOCKED':
                            txt = "阻塞"
                            break
                        default:
                            txt = "未知"
                            break;
                    }
                    return txt;
                }
            },
            {
                field: 'remark',
                title: '备注',
                width: '17%',
                align: 'center'

            },
            {
                field: 'id',
                title: '操作',
                align: 'center',
                width: 320,
                formatter: function (value, row) {
                    var actions = [];
                    if (row.jobStatus == 'PAUSED') {
                        actions.push('<a data-job-name="' + row.jobName + '" class="btn btn-primary btn-xs able" style="background-color:burlywood" href="javascript:void(0)"><i class="fa fa-search"></i>启用</a> ');
                    } else {
                        actions.push('<a data-job-name="' + row.jobName + '" class="btn btn-primary btn-xs disable" style="background-color:burlywood" href="javascript:void(0)"><i class="fa fa-search"></i>禁用</a> ');
                        actions.push('<a data-job-name="' + row.jobName + '"  class="btn btn-primary btn-xs run" href="javascript:void(0)"><i class="fa fa-play-circle-o"></i>立即执行</a> ');
                    }

                    return actions.join('');
                }
            }
        ]],
        toolbar: [{
            text: '刷新',
            iconCls: 'icon-refresh',
            handler: function () {
                $('#tb').datagrid('reload');
            }
        }, {
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                jobAddOrEdit('');
            }
        }, {
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                var cbox = $('#tb').datagrid('getSelected');
                if (cbox === null || cbox === undefined) {
                    topWin.layer.msg("请选中一条记录");
                    return;
                }
                jobAddOrEdit(cbox.jobName);
            }
        }, {
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                var cbox = $('#tb').datagrid('getSelected');
                if (cbox === null || cbox === undefined) {
                    topWin.layer.msg("请选中一条记录");
                    return;
                }
                remove(cbox.jobName);
            }
        }
        ]
    });
}


function jobAddOrEdit(jobName) {
    var jobAddDialog = openDialog('jobAddDialog', g.ctx + 'pc/sys/job/addOrEdit?jobName=' + jobName, {
            title: "新增",
            width: '400px',
            height: '435px',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                jobAddDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.save,
                    handler: function () {
                        topWin.frames['jobAddDialog'].save(function () {
                            jobAddDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    }
                },
                {
                    text: itablei8n.close,
                    handler: function () {
                        jobAddDialog.dialog('close');
                    }
                }
            ]
        }
    );
}




