/**
 * 初始化认定条件管理详情对话框
 */
var IntegralMgrInfo = {
    integralMgrInfoData: {},
    validateFields: {
        type: {
            validators: {
                notEmpty: {
                    message: '请选择人才类型'
                }
            }
        },
        name: {
            validators: {
                notEmpty: {
                    message: '请填写项目名称'
                }
            }
        },
        projectType: {
            validators: {
                notEmpty: {
                    message: '请选择项目类别'
                }
            }
        },
        active: {
            validators: {
                notEmpty: {
                    message: '请选择启用状态'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
IntegralMgrInfo.clearData = function () {
    this.integralMgrInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
IntegralMgrInfo.set = function (key, val) {
    this.integralMgrInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
IntegralMgrInfo.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
IntegralMgrInfo.close = function () {
    parent.layer.close(window.parent.IntegralMgr.layerIndex);
}

/**
 * 收集数据
 */
IntegralMgrInfo.collectData = function () {
    this
            .set('id')
            .set('type')
            .set('name')
            .set('projectType')
            .set('limit')
            .set('yearly')
            .set('max')
            .set('active');
}

IntegralMgrInfo.onLimitChange = function () {
    var limit = $("#limit").val();
    switch (limit) {
        case "1":
            $('#integraMgrInfoForm').bootstrapValidator('addField', "yearly", {validators: {notEmpty: {message: '请选择上限方案'}}});
            $('#integraMgrInfoForm').bootstrapValidator('addField', "max", {validators: {notEmpty: {message: '请填写积分上限'}}});
            $(".limit-setting").css("display", "block");
            break;
        default:
            $('#integraMgrInfoForm').bootstrapValidator('removeField', "yearly");
            $('#integraMgrInfoForm').bootstrapValidator('removeField', "max");
            $(".limit-setting").css("display", "none");
    }
}

/**
 * 提交添加
 */
IntegralMgrInfo.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }

    var ajax = new $ax("/admin/integral_mgr/add", function (data) {
        if (data.code == "200") {
            Feng.success(data.msg);
            window.parent.IntegralMgr.table.refresh();
            IntegralMgrInfo.close();
        } else {
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.integralMgrInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
IntegralMgrInfo.editSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/admin/integral_mgr/edit", function (data) {
        if (data.code == "200") {
            Feng.success(data.msg);
            window.parent.IntegralMgr.table.refresh();
            IntegralMgrInfo.close();
        } else {
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.integralMgrInfoData);
    ajax.start();
}

/**
 * 验证数据是否为空
 */
IntegralMgrInfo.validate = function () {
    $('#integraMgrInfoForm').data("bootstrapValidator").resetForm();
    $('#integraMgrInfoForm').bootstrapValidator('validate');
    return $("#integraMgrInfoForm").data('bootstrapValidator').isValid();
}

$(function () {
    Feng.initValidator("integraMgrInfoForm", IntegralMgrInfo.validateFields);
    //下拉框数据回显
    $("select").each(function () {
        $(this).val($(this).attr("selectVal"));
    });
    IntegralMgrInfo.onLimitChange();
});
