/**
 * 初始化批次管理详情对话框
 */
Feng.ctxPath = "/admin";
var BatchInfoDlg = {
    batchInfoData: {},
    validateFields: {
        type: {
            validators: {
                notEmpty: {
                    message: '申报类别不能为空'
                }
            }
        },
        source: {
            validators: {
                notEmpty: {
                    message: '人才类型不能为空'
                }
            }
        },
        batch: {
            validators: {
                notEmpty: {
                    message: '申报批次不能为空'
                },
                regexp: {
                    regexp: /^\d+$/,
                    message: "只能输入数字"
                }
            }
        },
        active: {
            validators: {
                notEmpty: {
                    message: '有效状态不能为空'
                }
            }
        },
        startTime: {
            validators: {
                notEmpty: {
                    message: '申报开始时间不能为空'
                }
            }
        },
        endTime: {
            validators: {
                notEmpty: {
                    message: '申报截止时间不能为空'
                }
            }
        },
    }
};

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

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

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

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

/**
 * 收集数据
 */
BatchInfoDlg.collectData = function () {
    this
            .set('id')
            .set('type')
            .set('source')
            .set('batch')
            .set('startTime')
            .set('endTime')
            .set('submitEndTime')
            .set('publicStartTime')
            .set('publicEndTime')
            .set('averageWage')
            .set('active')
            .set('description');
}

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


/**
 * 提交添加
 */
BatchInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/batch/add", function (data) {
        if (data.code == "200") {
            Feng.success(data.msg);
            window.parent.Batch.table.refresh();
            BatchInfoDlg.close();
        } else {
            Feng.error(data.msg);
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.batchInfoData);
    ajax.start();
}

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

$(function () {
    Feng.initValidator("batchInfoForm", BatchInfoDlg.validateFields);
    //下拉框数据动态加载
    Feng.addAjaxSelect({
        "id": "type",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/dict/findChildDictByCode?code=declare_type"
    });
    //批量加载时间控件
    $("input[time='time']").each(function () {
        laydate.render({
            elem: "#" + $(this).attr("id")
            , type: $(this).attr("format")
            , trigger: 'click'
        });
    });
    //下拉框数据回显
    $("select").each(function () {
        console.log($(this).attr("selectVal"))
        $(this).val($(this).attr("selectVal"));
    });
});
