/**
 * 初始化认定条件管理详情对话框
 */
var IntegralItemMgrInfo = {
    integralItemMgrInfoData: {},
    validateFields: {
        projectId: {
            validators: {
                notEmpty: {
                    message: '请选择所属积分项目'
                }
            }
        },
        name: {
            validators: {
                notEmpty: {
                    message: '请输入标准名称'
                }
            }
        },
        active: {
            validators: {
                notEmpty: {
                    message: '请选择启用状态'
                }
            }
        },
        plan: {
            validators: {
                notEmpty: {
                    message: '请选择积分方案'
                }
            }
        },
        unit: {
            validators: {
                notEmpty: {
                    message: '请输入计量单位'
                }
            }
        },
        fstNeedAmount: {
            validators: {
                notEmpty: {
                    message: '请输入首次达成量'
                }
            }
        },
        fstGainPoints: {
            validators: {
                notEmpty: {
                    message: '请输入首次获得积分'
                }
            }
        },
        stepNeedAmount: {
            validators: {
                notEmpty: {
                    message: '请输入每新增量'
                }
            }
        },
        stepGainPoints: {
            validators: {
                notEmpty: {
                    message: '请输入每新增获得积分'
                }
            }
        }
    }
};

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

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

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

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

/**
 * 收集数据
 */
IntegralItemMgrInfo.collectData = function () {
    this
            .set('id')
            .set('type')
            .set('projectId')
            .set('name')
            .set('plan')
            .set('unit')
            .set('fstNeedAmount')
            .set('fstGainPoints')
            .set('stepNeedAmount')
            .set('stepGainPoints')
            .set('fileTypeId')
            .set('limit')
            .set('yearly')
            .set('max')
            .set('active');
}

IntegralItemMgrInfo.onTypeChange = function () {
    var type = $("#type").val();
    Feng.addAjaxSelect({
        "id": "projectId",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": "/admin/integral_mgr/getProjectsByType/type/" + type
    });
    var projectId = $("#projectId").attr("selectVal");
    $("#projectId").val(projectId);
    Feng.addAjaxSelect({
        "id": "fileTypeId",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": "/admin/integral_mgr/getFilesByType/type/" + type
    });
    $("#fileTypeId").trigger('chosen:updated');
}

//切换积分方案
IntegralItemMgrInfo.onIntegralPlanChange = function () {
    var plan = $("#plan").val();
    switch (plan) {
        case "1":
            $(".planb").css("display", "none");
            $('#integraMgrInfoForm').bootstrapValidator('removeField', "stepNeedAmount");
            $('#integraMgrInfoForm').bootstrapValidator('removeField', "stepGainPoints");
            break;
        case "2":
            $(".planb").css("display", "block");
            $('#integraMgrInfoForm').bootstrapValidator('addField', "stepNeedAmount", {validators: {notEmpty: {message: '请填写每新增量'}}});
            $('#integraMgrInfoForm').bootstrapValidator('addField', "stepGainPoints", {validators: {notEmpty: {message: '请填写每新增获得积分'}}});
            break;
    }
}

IntegralItemMgrInfo.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");
    }
}

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

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

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

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

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

    $("#fileTypeId").chosen({
        no_results_text: "没有找到结果！",
        width: '100%',
        search_contains: true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        enable_split_word_search: true,
        rtl: true
    });
    var fileTypeIds = $("#fileTypeId").attr("selectVal")
    $("#fileTypeId").val(fileTypeIds.split(",")).trigger("chosen:updated");
});
