/**
 * 定时任务管理初始化
 */
var Schedule = {
    id: "ScheduleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Schedule.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
            {title: '名称', field: 'name', visible: true, align: 'center', valign: 'middle'},
            {title: '请求url', field: 'url', visible: true, align: 'center', valign: 'middle'},
            {title: '时间设置', field: 'timeStr', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'state', visible: true, align: 'center', valign: 'middle',
                formatter(value,row,index){
                    if (value == 1) {
                        return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-primary btn-xs\">启用</button>";
                    }
                    if (value == 2) {
                        return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-warning btn-xs\">未启用</button>";
                    }
                }
            },
            {title: '说明', field: 'description', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Schedule.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Schedule.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加定时任务
 */
Schedule.openAddSchedule = function () {
    var index = layer.open({
        type: 2,
        title: '添加定时任务',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/schedule/schedule_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看定时任务详情
 */
Schedule.openScheduleDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '定时任务详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/schedule/schedule_update/' + Schedule.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除定时任务
 */
Schedule.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/schedule/delete", function (data) {
            Feng.success("删除成功!");
            Schedule.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("scheduleId",this.seItem.id);
        ajax.start();
    }
};

Schedule.open = function(){
    if (this.check()) {
        var selected = $('#' + this.id).bootstrapTable('getSelections');
        var ids = "";
        for(var key in selected){
            ids = ids+ selected[key].id +",";
        }
        var ajax = new $ax(Feng.ctxPath + "/schedule/open", function (data) {
            Feng.success("启用成功!");
            Schedule.table.refresh();
        }, function (data) {
            Feng.error("启用成功!" + data.responseJSON.message + "!");
        });
        ajax.set("ids",ids);
        ajax.start();
    }
}

/**
 * 查询定时任务列表
 */
Schedule.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Schedule.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Schedule.initColumn();
    var table = new BSTable(Schedule.id, "/schedule/list", defaultColunms);
    table.setPaginationType("client");
    table.setSingleSelect(false);
    Schedule.table = table.init();
});
