/**
 * 初始化认定条件管理详情对话框
 */
var IdentifyConditionInfoDlg = {
    identifyConditionInfoData : {},
    validateFields: {
        talentLevel: {
            validators: {
                notEmpty: {
                    message: '人才层次不能为空'
                }
            }
        },
        type :{
            validators: {
                notEmpty: {
                    message: '人才类别不能为空'
                }
            }
        },
        name: {
            validators: {
                notEmpty: {
                    message: '名称不能为空'
                }
            }
        },
        active: {
            validators: {
                notEmpty: {
                    message: '启用状态不能为空'
                }
            }
        }
    }
};

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

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

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

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

/**
 * 收集数据
 */
IdentifyConditionInfoDlg.collectData = function() {
    this
    .set('id')
    .set('type')
    .set('talentLevel')
    .set('name')
    .set('active')
    .set('description');
    var companys = $("#companyIds").val();
    var talentTypes = $("#talentTypes").val();
    var ids = "",talentTypeArr = "";
    for(var key in companys){
        if(Feng.isNotEmptyStr(companys[key])){
            ids = ids + companys[key]+",";
        }
    }
    if(Feng.isNotEmptyStr(ids)){
        ids = ids.substring(0,ids.length-1);
    }
    for(var key in talentTypes){
        if(Feng.isNotEmptyStr(talentTypes[key])){
            talentTypeArr = talentTypeArr + talentTypes[key]+",";
        }
    }
    if(Feng.isNotEmptyStr(talentTypeArr)){
        talentTypeArr = talentTypeArr.substring(0,talentTypeArr.length-1);
    }
    this.identifyConditionInfoData['companyIds'] = ids;
    this.identifyConditionInfoData['talentTypes'] = talentTypeArr;

}

/**
 * 提交添加
 */
IdentifyConditionInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    // if(this.identifyConditionInfoData.companyIds==null||this.identifyConditionInfoData.companyIds==''){
    //     Feng.error("审核单位不能为空");return ;
    // }
    //提交信息
    var ajax = new $ax("/admin/talent_condition/add", function(data){
        if(data.code=="200"){
            Feng.success(data.msg);
            window.parent.IdentifyCondition.table.refresh();
            IdentifyConditionInfoDlg.close();
        }else{
            Feng.error(data.msg);
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.identifyConditionInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
IdentifyConditionInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    // if(this.identifyConditionInfoData.companyIds==null||this.identifyConditionInfoData.companyIds==''){
    //     Feng.error("审核单位不能为空");return ;
    // }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/admin/talent_condition/edit", function(data){
        if(data.code=="200"){
            Feng.success(data.msg);
            window.parent.IdentifyCondition.table.refresh();
            IdentifyConditionInfoDlg.close();
        }else{
            Feng.error(data.msg);
        }
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.identifyConditionInfoData);
    ajax.start();
}

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

$(function() {
    Feng.initValidator("identifyConditionInfoForm", IdentifyConditionInfoDlg.validateFields);
    //下拉框数据动态加载
    // Feng.addAjaxSelect({
    //     "id": "talentLevel",
    //     "displayCode": "code",
    //     "displayName": "name",
    //     "type": "GET",
    //     "url": Feng.ctxPath + "/dict/findChildDictByCode?code=un_talentLevel"
    // });
    var arr = [{
        "name": "talentLevel",
        "code": "talent_arrange"
    }];
    Feng.findChildDictBatch(JSON.stringify(arr));
    Feng.addAjaxSelect({
        "id": "companyIds",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/common/api/getCompanyKvs"
    });
    $('#companyIds').chosen({
        // search_contains:false,
        // enable_split_word_search: true
    });
    $('#talentTypes').chosen({
        search_contains:true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width:"100%",
        enable_split_word_search: true
    });
    //下拉框数据回显
    $("select").each(function () {
        $(this).val($(this).attr("selectVal"));
    });
    var companyIds = $("#companyIds").attr("selectVal");
    var talentTypes = $("#talentTypes").attr("selectVal");
    if(Feng.isNotEmptyStr(companyIds)){
        $("#companyIds").val(companyIds.split(",")).trigger("chosen:updated");
    }
    if(Feng.isNotEmptyStr(talentTypes)){
        $("#talentTypes").val(talentTypes.split(",")).trigger("chosen:updated");
    }
});
