/**
 * 批次管理管理初始化
 */
var Batch = {
    id: "BatchTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};
Feng.ctxPath = "/admin";
/**
 * 初始化表格的列
 */
Batch.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '申报类别', field: 'type', visible: true, align: 'center', valign: 'middle'},
        {title: '人才类型', field: 'source', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "晋江市现代产业体系人才";
                }
                if (value == 2) {
                    return "晋江市集成电路优秀人才";
                }
            }
        },
        {title: '申报批次', field: 'batch', visible: true, align: 'center', valign: 'middle'},
        {title: '申报开始时间', field: 'startTime', visible: true, align: 'center', valign: 'middle'},
        {title: '申报截止时间', field: 'endTime', visible: true, align: 'center', valign: 'middle'},
        {title: '提交截止时间', field: 'submitEndTime', visible: true, align: 'center', valign: 'middle'},
        {title: '公示开始时间', field: 'publicStartTime', visible: true, align: 'center', valign: 'middle'},
        {title: '公示截止时间', field: 'publicEndTime', visible: true, align: 'center', valign: 'middle'},
        {title: '平均工资', field: 'averageWage', visible: true, align: 'center', valign: 'middle'},
        {title: '是否有效', field: 'active', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span style='color: green'>是</span>";
                }
                if (value == 2) {
                    return "<span style='color: red'>否</span>";
                }
            }
        },
        {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle'}

    ];
};

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

/**
 * 点击添加批次管理
 */
Batch.openAddBatch = function () {
    var index = layer.open({
        type: 2,
        title: '添加批次',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/batch/add'
    });
    Batch.layerIndex = index;
};

/**
 * 打开查看批次管理详情
 */
Batch.openBatchDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '批次管理详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/batch/edit/id/' + Batch.seItem.id
        });
        Batch.layerIndex = index;
    }
};

/**
 * 删除批次管理
 */
Batch.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/batch/delete", function (data) {
                Feng.success("删除成功!");
                Batch.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("batchId", Batch.seItem.id);
            ajax.start();
        };
        Feng.confirm("是否刪除该批次?", operation);
    }
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Batch.formParams = function () {
    var queryData = {};
    queryData['type'] = $("#type").val();
    queryData['source'] = $("#source").val();

    queryData['active'] = $("#active").val();
    return queryData;
}

/**
 * 查询批次管理列表
 */
Batch.search = function () {
    Batch.table.refresh({query: Batch.formParams()});
};

/**
 * 重置
 */
Batch.reset = function () {
    $("#type").val("");
    $("#active").val("");
    $("#source").val("");
}

/**
 * 设置生效
 */
Batch.setActive = function () {
    if (this.check()) {
        if (Batch.seItem.active == 1) {
            Feng.error("该批次已生效，无需再次操作");
            return;
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/batch/setActive", function (data) {
                if (data.code == "200") {
                    Feng.success(data.msg);
                    Batch.table.refresh();
                } else {
                    Feng.error(data.msg);
                }
            }, function (data) {
                Feng.error("设置生效失败!" + data.responseJSON.message + "!");
            });
            ajax.set("batchId", Batch.seItem.id);
            ajax.start();
        };
        Feng.confirm("一旦设置生效，该类别其他批次将无效，确认生效吗?", operation);
    }
}

Batch.setNotActive = function () {
    if (this.check()) {
        if (Batch.seItem.active == 2) {
            Feng.error("该批次未生效，无需再次操作");
            return;
        }
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/batch/setNotActive", function (data) {
                if (data.code == "200") {
                    Feng.success(data.msg);
                    Batch.table.refresh();
                } else {
                    Feng.error(data.msg);
                }
            }, function (data) {
                Feng.error("设置失效失败!" + data.responseJSON.message + "!");
            });
            ajax.set("batchId", Batch.seItem.id);
            ajax.start();
        };
        Feng.confirm("确认设置失效吗?", operation);
    }
}

$(function () {
    var defaultColunms = Batch.initColumn();
    var table = new BSTable(Batch.id, "/batch/list", defaultColunms);
    table.setPaginationType("server");
    Batch.table = table.init();
    //下拉框数据动态加载
    Feng.addAjaxSelect({
        "id": "type",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/dict/findChildDictByCode?code=declare_type"
    });
});
