/**
 * 待办事项管理初始化
 */
var Task = {
    id: "TaskTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Task.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '用户id', field: 'userId', visible: true, align: 'center', valign: 'middle'},
            {title: '事件类型', field: 'type', visible: true, align: 'center', valign: 'middle'},
            {title: '是否已读', field: 'active', visible: true, align: 'center', valign: 'middle'},
            {title: '说明', field: 'description', visible: true, align: 'center', valign: 'middle'},
    ];
};

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

/**
 * 点击添加待办事项
 */
Task.openAddTask = function () {
    var index = layer.open({
        type: 2,
        title: '添加待办事项',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/task/task_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看待办事项详情
 */
Task.openTaskDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '待办事项详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/task/task_update/' + Task.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除待办事项
 */
Task.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/task/delete", function (data) {
            Feng.success("删除成功!");
            Task.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("taskId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询待办事项列表
 */
Task.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Task.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Task.initColumn();
    var table = new BSTable(Task.id, "/task/list", defaultColunms);
    table.setPaginationType("client");
    Task.table = table.init();
});