/**
 * 初始化子女就学详情对话框
 */
var EducationSchoolInfoDlg = {
    educationSchoolInfoData : {},
    validateFields: {
        project: {validators: {notEmpty: {message: '申报项目不能为空'}}},
        pName: {validators: {notEmpty: {message: '申报人姓名不能为空'}}},
        pSex: {validators: {notEmpty: {message: '申报人性别不能为空'}}},
        pIdcard: {validators: {notEmpty: {message: '申报人证件号码不能为空'}}},
        talentArrange: {validators: {notEmpty: {message: '人才层次不能为空'}}},
        certificateNo: {validators: {notEmpty: {message: '人才编号不能为空'}}},
        nativePlace: {validators: {notEmpty: {message: '籍贯不能为空'}}},
        address: {validators: {notEmpty: {message: '现居地址不能为空'}}},
        enterpriseName:{validators: {notEmpty: {message: '工作单位不能为空'}}},
        enterpriseAddress:{validators: {notEmpty: {message: '工作单位详细地址不能为空'}}},
        phone: {
            validators: {
                notEmpty: {
                    message: '联系电话不能为空'
                },
                regexp: {
                    regexp: /0?(13|14|15|17|18|19)[0-9]{9}/,
                    message: "手机号码格式不正确"
                }
            }
        },
        cName: {validators: {notEmpty: {message: '子女姓名不能为空'}}},
        cSex: {validators: {notEmpty: {message: '子女性别不能为空'}}},
        cIdcard: {validators: {notEmpty: {message: '子女证件号码不能为空'}}},
        cBirthday: {validators: {notEmpty: {message: '子女出生日期不能为空'}}},
        cRelation: {validators: {notEmpty: {message: '与申报人关系不能为空'}}},
        nowSchool: {validators: {notEmpty: {message: '现就读学校不能为空'}}},
        nowGrade: {validators: {notEmpty: {message: '现就读年级不能为空'}}},
        companyStreet: {validators: {notEmpty: {message: '工作单位所属镇街不能为空'}}},
        houseStreet: {validators: {notEmpty: {message: '房产所在镇街不能为空'}}},
    }
};

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

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

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

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

/**
 * 收集数据
 */
EducationSchoolInfoDlg.collectData = function() {
    this
    .set('id')
    .set('year')
    .set('talentId')
    .set('personId')
    .set('type')
    .set('project')
    .set('pName')
    .set('pSex')
    .set('pIdcard')
    .set('enterpriseName')
    .set('enterpriseAddress')
    .set('talentArrange')
    .set('certificateNo')
        .set('certificateStartTime')
        .set('qzgccrcActiveTime')
    .set('nativePlace')
    .set('address')
    .set('phone')
    .set('cName')
    .set('cSex')
    .set('cIdcard')
    .set('cBirthday')
    .set('stuNumber')
    .set('cRelation')
    .set('nowSchool')
    .set('nowGrade')
    .set('applySchool')
    .set('companyStreet')
    .set('houseStreet')
    .set('checkState');
}

/**
 * 校验字段
 */
EducationSchoolInfoDlg.validate = function(){
    $('#educationSchoolForm').data("bootstrapValidator").resetForm();
    $('#educationSchoolForm').bootstrapValidator('validate');
    return $("#educationSchoolForm").data('bootstrapValidator').isValid();
}

/**
 * 申报人变更
 * @param content
 */
EducationSchoolInfoDlg.nameChange = function(content){
    var talentId = $(content).val();
    if(Feng.isEmptyStr(talentId)){
        $("#talentId,#pSex,#pIdcard,#talentArrange,#certificateNo,#nativePlace").val("");
        return ;
    }
    var ajax = new $ax(Feng.ctxPath + "/api/talentInfo/detail/"+talentId, function(data){
        $("#pName").val(data.name);
        $("#pSex").val(data.sex);
        $("#pIdcard").val(data.idCard);
        $("#talentArrange").val(data.talentArrange);
        $("#certificateNo").val(data.certificateNO);
        $("#nativePlace").val(data.provinceName+data.cityName+data.countyName);
        $("#address").val(data.streetName);
        $("#phone").val(data.phone);
    },function(data){
        Feng.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.start();
}

/**
 * 项目选择变更
 * @param content
 */
EducationSchoolInfoDlg.projectChange = function(content){
    var project = $(content).val();
    if(Feng.isEmptyStr(project)){
        $("#applySchool").val("").parent().css("display","none");
    }
    if(project == CONFIG.education_school){
        $("#applySchool").val("").parent().css("display","block");
    }
    if(project == CONFIG.education_score){
        $("#applySchool").val("").parent().css("display","none");
    }
}


/**
 * 提交添加
 */
EducationSchoolInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if(!EducationSchoolInfoDlg.validate()){
        return ;
    }
    if(!validateIsEdit())return;
    var ajax = new $ax(Feng.ctxPath + "/api/educationSchool/upsert", function(data){
        if(data.code == 200){
            Feng.success(data.msg);
            window.parent.EducationSchool.table.refresh();
            $("#fileLi").removeAttr("style");
            $("#id").val(data.obj.id);
            $("#checkState").val(data.obj.checkState);
        }else{
            Feng.info(data.msg);
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.educationSchoolInfoData);
    ajax.start();
}

/**
 * 提交审核
 */
EducationSchoolInfoDlg.submitToCheck = function () {
    var id = $("#id").val();
    if(id==null || id==""){
        Feng.info("请先填写基础信息并上传附件");
        return ;
    }
    if(!validateIsEdit())return;
    var operation = function() {
        var ajax = new $ax(Feng.ctxPath + "/api/educationSchool/submitToCheck", function (data) {
            if(data.code==200){
                Feng.success(data.msg);
                // $("#checkState").val(data.obj);
                window.parent.EducationSchool.table.refresh();
                EducationSchoolInfoDlg.close();
            }else{
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("提交审核失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", id);
        ajax.start();
    }
    Feng.confirm("请确认基础信息已核对无误，相应附件已上传，一旦提交，无法修改", operation);
}

/**
 * 校验是否可以操作
 */
function validateIsEdit () {
    var checkState = $("#checkState").val();
    if(checkState == 1){
        Feng.error("您的申报正在审核中，无法操作");
        return false;
    }if(checkState == 3){
        Feng.error("您的申报已审核通过，无法操作");
        return false;
    }if(checkState == 9){
        Feng.error("您的申报正在审核中，无法操作");
        return false;
    }
    return true;
}

/**
 * 校验是否显示按钮
 * @param type      类型  1-上传按钮，2-修改删除按钮
 * @param row
 * @returns {string}
 */
function validUploadButton(type,row,fileId){
    var checkState = $("#checkState").val();
    if(Feng.isEmptyStr(checkState)|| checkState ==-2 ||checkState==2){
        if(type == 1){          //上传
            return "<button type='button' onclick=\"checkFile(this,'"+row.id+"','"+null+"')\" style='margin-right: 10px' class=\"btn btn-xs btn-info\">" +
                "<i class=\"fa fa-upload\"></i>上传" +
                "</button>" ;
        }else{
            return "<button type=\'button\' onclick=\"checkFile(this,'"+row.id+"','"+fileId+"')\" style=\'margin-right: 10px\' class=\"btn btn-xs btn-info\">" +
                "<i class=\"fa fa-paste\"></i>修改" +
                "</button>" +
                "<button type='button' onclick=\"deleteFile('"+fileId+"','"+CONFIG.project_school+"')\" class=\"btn btn-xs btn-danger\">" +
                "<i class=\"fa fa-times\"></i>删除" +
                "</button>";
        }
    }else{
        return type == 1?"":"";
    }
}


$(function() {
    Feng.initValidatorTip("educationSchoolForm",EducationSchoolInfoDlg.validateFields);
    //批量加载字典表数据
    var arr = [
        {"name":"companyStreet","code":"un_street"},
        {"name":"houseStreet","code":"un_street"},
        {"name":"talentArrange","code":"un_talentLevel"},
        {"name":"cRelation","code":"un_education_relation"},
        {"name":"nowGrade","code":"un_grade"},
        {"name":"project","code":"un_educationSchool_project"},
        {"name":"applySchool","code":"un_school_pool"}];
    Feng.findChildDictBatch(JSON.stringify(arr));
    //批量加载时间控件
    $(".date").each(function(){laydate.render({elem: ".date",type: "date",trigger: 'click'});});
    var type = $("#type").val();
    var talentArrange = $("#talentArrange").val();
    if(type == 2){
        $("#project").val(1).attr("style","pointer-events: none;background-color: #eee;").trigger("change");
    }
    if(talentArrange != 1 && talentArrange != 2 && talentArrange != 3){
        $("#project").val(1).attr("style","pointer-events: none;background-color: #eee;").trigger("change");
    }
    $("select").each(function () {
        if (Feng.isNotEmptyStr($(this).attr("value"))){
            $(this).val($(this).attr("value")).trigger("change");
        }
    });
    $("#applySchool").on('chosen:ready', function(e, params) {
        $(".chosen-container-single .chosen-single").css("padding","3px 0px 0px 2px");
    });
    $("#applySchool").chosen({
        search_contains:true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
        disable_search: false,
        width:"100%",
        enable_split_word_search: true
    });
    var id = $("#id").val();
    if(Feng.isNotEmptyStr(id)){
        $("#fileLi").removeAttr("style");
        Feng.getCheckLog("logTable",{"type":CONFIG.project_school,"mainId":id,"typeFileId":"","active":1})
    }else{
        $("#fileLi").attr("style","pointer-events: none");
    }
});
