/**
 * 初始化走访小组配置详情对话框
 */
var VisitGroupInfoDlg = {
    visitGroupInfoData : {},
    validateFields: {
        year: {
            validators: {
                notEmpty: {
                    message: '申报年度不能为空'
                }
            }
        },
        name:{
            validators: {
                notEmpty: {
                    message: '审核小组不能为空'
                }
            }
        },
    }
};

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

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

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

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

/**
 * 收集数据
 */
VisitGroupInfoDlg.collectData = function() {
    this
    .set('id')
    .set('type')
    .set('year')
    .set('name');
}

/**
 * 提交添加
 */
VisitGroupInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if(Feng.isEmptyStr(this.visitGroupInfoData.name)){
        Feng.info("核查小组不能为空");
        return;
    }
    var talentTypeArr = new Array();
    var talentTypes = "";
    var errorMsg = "";
    $(".child").each(function (index){
        var talentType = $(this).find("[name='talentType']").val();
        if(Feng.isEmptyStr(talentType)){
            errorMsg = "人才标签不能为空";
            return false;
        }
        if(talentTypeArr.indexOf(talentType)>=0){
            errorMsg = "存在重复的人才标签";
            return false;
        }
        var streets = $(this).find("[name='street']").val();
        if(Feng.isEmptyStr(streets)){
            errorMsg = "核查镇街不能为空";
            return false;
        }
        talentTypeArr.push(talentType);
        talentTypes = talentTypes + talentType +"-" + streets.join(",") + ";"
    });
    if(Feng.isNotEmptyStr(errorMsg)){
        Feng.info(errorMsg);
        return;
    }
    this.visitGroupInfoData['talentType'] = talentTypes.substring(0,talentTypes.length-1);;
    if(Feng.isEmptyStr(this.visitGroupInfoData.talentType)){
        Feng.error("标签镇街关系不能为空");return ;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/visitGroup/add", function(data){
        if(data.code=="200"){
            Feng.success(data.msg);
            window.parent.VisitGroup.table.refresh();
            VisitGroupInfoDlg.close();
        }else{
            Feng.error(data.msg);
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.visitGroupInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
VisitGroupInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if(Feng.isEmptyStr(this.visitGroupInfoData.talentType)){
        Feng.error("核查人才标签不能为空");return ;
    }
    if(Feng.isEmptyStr(this.visitGroupInfoData.street)){
        Feng.error("核查镇街不能为空");return ;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/visitGroup/update", function(data){
        if(data.code=="200"){
            Feng.success(data.msg);
            window.parent.VisitGroup.table.refresh();
            VisitGroupInfoDlg.close();
        }else{
            Feng.error(data.msg);
        }
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.visitGroupInfoData);
    ajax.start();
}

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

VisitGroupInfoDlg.insertRow = function (){
    $("#childData").append("<div class=\"col-sm-12 child\">\n" +
        "                                        <div class=\"rowGroup col-sm-5\">\n" +
        "                                            <select class=\"form-control\"  name=\"talentType\" data-placeholder=\"核查人才标签\" onchange=\"VisitGroupInfoDlg.talentTypeChange(this)\">\n" +
        $("#talentTypeData").html()+
        "                                            </select>\n" +
        "                                        </div>\n" +
        "                                        <div class=\"rowGroup col-sm-5\">\n" +
        "                                            <select class=\"chosen\"  name=\"street\" placeholder=\"核查镇街\" multiple>\n" +
        "                                            </select>\n" +
        "                                        </div>\n" +
        "                                        <div class=\"reduce\"  onclick=\"$(this).parent().remove()\"></div>\n" +
        "                                    </div>");
    $(".chosen").on('chosen:ready', function(e, params) {
        $(".chosen-container-single .chosen-single").css("padding","3px 0px 0px 2px");
    });
    $(".chosen").chosen({
        width:"100%",
    });
}

VisitGroupInfoDlg.talentTypeChange = function (content){
    var talentType = $(content).val();
    var year = $("#year").val();
    var next = $(content).parent().next().children().eq(0);
    next.empty();
    if(Feng.isEmptyStr(year)){
        Feng.info("请先选择申报年度");
        $(content).val("");
        return ;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/visitGroup/getStreetByTalentType", function(data){
        if(data == null || data.length == 0){
            Feng.info("当前年度该人才标签无未绑定的镇街");
            return;
        }
        var html = '<option value="">请选择</option>';
        for(var obj of data){
            html = html + '<option value="'+obj.code+'">'+obj.name+'</option>';
        }
        next.append(html);
        next.trigger('chosen:updated');
    },function(data){
        Feng.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set("year",year);
    ajax.set("type",$("#type").val());
    ajax.set("talentType",talentType);
    ajax.start();
}


$(function() {
    // Feng.initValidator("visitGroupInfoForm", VisitGroupInfoDlg.validateFields);
    $(".chosen").on('chosen:ready', function(e, params) {
        $(".chosen-container-single .chosen-single").css("padding","4px 0px 0px 4px");
    });
    $('.chosen').chosen({
        // search_contains:false,
        // enable_split_word_search: true
        width:"100%",
    });
    //下拉框数据回显
    if(Feng.isNotEmptyStr($("#id").val())){
        $("select").each(function () {
            $(this).val($(this).attr("value"));
        });
        $(".street").each(function (){
            $(this).val($(this).attr("value").split(",")).trigger('chosen:updated');
        })

    }

    // $("[name='street']").chosen({width: '100%',placeholder_text_multiple: '请选择核查镇街'});
    // $("#talentType").val($("#talentType").attr("value").split(",")).trigger("chosen:updated");
    // $("#street").val($("#street").attr("value").split(",")).trigger("chosen:updated");
});
