/**
 * 初始化学杂费补助详情对话框
 */
var ChildschoolFeesInfoDlg = {
    childschoolFeesInfoData : {},
    validateFields: {
        pSex: {validators: {notEmpty: {message: '申报人性别不能为空'}}},
        pIdcard: {validators: {notEmpty: {message: '申报人证件号码不能为空'}}},
        talentArrange: {validators: {notEmpty: {message: '人才层次不能为空'}}},
        certificateNo: {validators: {notEmpty: {message: '人才编号不能为空'}}},
        nativePlace: {validators: {notEmpty: {message: '籍贯不能为空'}}},
        address: {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: '现就读年级不能为空'}}},
        paidInMoeny: {validators: {notEmpty: {message: '实缴学杂费补助不能为空'}}},
    }
};

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

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

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

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

/**
 * 收集数据
 */
ChildschoolFeesInfoDlg.collectData = function() {
    this
    .set('id')
    .set('year')
    .set('enterpriseId')
    .set('talentId')
    .set('type')
    .set('pName')
    .set('pSex')
    .set('pCardType')
    .set('pIdcard')
    .set('talentArrange')
    .set('certificateNo')
    .set('nativePlace')
    .set('address')
    .set('phone')
    .set('cName')
    .set('cSex')
    .set('cCardType')
    .set('cIdcard')
    .set('cBirthday')
    .set('stuNumber')
    .set('cRelation')
    .set('nowSchool')
    .set('nowGrade')
    .set('paidInMoeny')
    .set('bankName')
    .set('bankAddress')
    .set('bankNumber');
    this.childschoolFeesInfoData['pensionDetail'] = Feng.getCheckBoxValues('pensionDetail');
    this.childschoolFeesInfoData['unemploymentDetail'] = Feng.getCheckBoxValues('unemploymentDetail');
    this.childschoolFeesInfoData['medicaDetail'] = Feng.getCheckBoxValues('medicaDetail');
    this.childschoolFeesInfoData['taxDetail'] = Feng.getCheckBoxValues('taxDetail');
}




/**
 * 根据所选人才对象初始化相应数据
 */
ChildschoolFeesInfoDlg.objChange = function(){
    var talentId = $("#talentId").val();
    if(Feng.isEmptyStr(talentId)){
        $("#pSex,#pCardType,#pIdcard,#talentArrange,#certificateNo,#nativePlace,#phone,#bankName,#bankAddress,#bankNumber").val("");
    }else{
        var ajax = new $ax(Feng.ctxPath + "/api/talentInfo/detail/"+talentId, function(data){
            $("#pSex").val(data.sex);
            $("#pCardType").val(data.cardType);
            $("#pIdcard").val(data.idCard);
            $("#talentArrange").val(data.talentArrange);
            $("#certificateNo").val(data.certificateNO);
            $("#nativePlace").val(data.provinceName+data.cityName+data.countyName);
            $("#phone").val(data.phone);
            $("#bankName").val(data.bank);
            $("#bankAddress").val(data.bankNetwork);
            $("#bankNumber").val(data.bankAccount);
        },function(data){
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}



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


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

/**
 * 提交添加
 */
ChildschoolFeesInfoDlg.addSubmit = function() {
    if(Feng.isNotEmptyStr($("#id").val())){
        ChildschoolFeesInfoDlg.editSubmit();
        return;
    }
    this.clearData();
    this.collectData();
    if(!ChildschoolFeesInfoDlg.validate()){
        return ;
    }
    if(!validateIsEdit())return;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/api/childschoolFees/add", function(data){
        if(data.code == 200){
            Feng.success(data.msg);
            window.parent.ChildschoolFees.table.refresh();
            $("#id").val(data.obj.id);
            $("#checkState").val(data.obj.checkState);
            ChildschoolFeesInfoDlg.validId();
        }else{
            Feng.info(data.msg);
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.childschoolFeesInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ChildschoolFeesInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if(!ChildschoolFeesInfoDlg.validate()){
        return ;
    }
    if(!validateIsEdit())return;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/api/childschoolFees/update", function(data){
        if(data.code == 200){
            Feng.success(data.msg);
            window.parent.ChildschoolFees.table.refresh();
        }else{
            Feng.info(data.msg);
        }
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.childschoolFeesInfoData);
    ajax.start();
}

/**
 * 提交审核
 */
ChildschoolFeesInfoDlg.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/childschoolFees/submitToCheck", function (data) {
            if(data.code==200){
                Feng.success(data.msg);
                // $("#checkState").val(data.obj);
                window.parent.ChildschoolFees.table.refresh();
                ChildschoolFeesInfoDlg.close();
            }else{
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("提交审核失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", id);
        ajax.start();
    }
    Feng.confirm("请确认基础信息已核对无误，相应附件已上传，一旦提交，无法修改", operation);
}

/**
 * 校验是否显示按钮
 * @param type      类型  1-上传按钮，2-修改删除按钮
 * @param row
 * @returns {string}
 */
function validUploadButton(type,row,fileId){
    var checkState = $("#checkState").val();
    if(Feng.isEmptyStr(checkState)|| checkState ==1 ||checkState== 4){
        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_schoolFees+"')\" class=\"btn btn-xs btn-danger\">" +
                "<i class=\"fa fa-times\"></i>删除" +
                "</button>";
        }
    }else{
        return type == 1?"":"";
    }
}


//校验是否保存基础信息
ChildschoolFeesInfoDlg.validId = function (){
    $("#fileLi").css("pointer-events",Feng.isEmptyStr($("#id").val())?"none":"auto");
}

ChildschoolFeesInfoDlg.checkSbAndTax = function(){
    var pensionDetail = $("#pensionDetail").attr("data-month");
    var unemploymentDetail = $("#unemploymentDetail").attr("value");
    var medicaDetail = $("#medicaDetail").attr("value");
    var taxDetail = $("#taxDetail").attr("value");
    $("#pensionDetail input").each(function () {
        if(pensionDetail.indexOf($(this).val())!= -1){
            $(this).attr("checked", true);
        }
    })
    $("#unemploymentDetail input").each(function () {
        if(unemploymentDetail.indexOf($(this).val())!= -1){
            $(this).attr("checked", true);
        }
    })
    $("#medicaDetail input").each(function () {
        if(medicaDetail.indexOf($(this).val())!= -1){
            $(this).attr("checked", true);
        }
    })
    $("#taxDetail input").each(function () {
        if(taxDetail.indexOf($(this).val())!= -1){
            $(this).attr("checked", true);
        }
    })
}


$(function() {
    Feng.initValidatorTip("childschoolFeesForm",ChildschoolFeesInfoDlg.validateFields);
    var arr = [{"name":"talentArrange","code":"un_talentLevel"},
        {"name":"cRelation","code":"un_education_relation"},
        {"name":"nowGrade","code":"un_grade"},
        {"name":"pCardType","code":"un_cardType"},
        {"name":"cCardType","code":"un_cardType"}];
    Feng.findChildDictBatch(JSON.stringify(arr))
    //批量加载时间控件
    $(".date").each(function(){
        laydate.render({
            elem: this
            ,type: 'date'
            ,trigger: 'click'
        });
    });
    if(Feng.isNotEmptyStr($("#id").val())){
        $("select").each(function () {
            $(this).val($(this).attr("value")).trigger("change");
        });
        Feng.getCheckLog("logTable",{"type":CONFIG.project_schoolFees,"mainId":$("#id").val(),"typeFileId":"","active":1})
        ChildschoolFeesInfoDlg.checkSbAndTax();
    }
    ChildschoolFeesInfoDlg.validId();
});
