/**
 * 初始化培训补贴详情对话框
 */
var TrainingSubsidyInfoDlg = {
    trainingSubsidyInfoData : {},
    validateFields: {
        enterpriseId: {validators: {notEmpty: {message: '所属企业不能为空' }}},
        name: {validators: {notEmpty: {message: '姓名不能为空'}}},
        sex: {validators: {notEmpty: {message: '性别不能为空'}}},
        cardType:{validators:{notEmpty:{message:'证件类型不能为空'}}},
        idCard: {validators: {notEmpty: {message: '证件号码不能为空'}}},
        nationality: {validators: {notEmpty: {message: '国籍/地区不能为空'}}},
        entryTime: {validators: {notEmpty: {message: '入职时间不能为空'}}},
        startTime: {validators: {notEmpty: {message: '工作合同开始时间不能为空'}}},
        endTime: {validators: {notEmpty: {message: '工作合同结束时间不能为空'}}},
        address:{validators: {notEmpty: {message: '联系地址不能为空'}}},
        phone: {
            validators: {
                notEmpty: {
                    message: '手机号码不能为空'
                },
                regexp :{
                    regexp:/0?(13|14|15|17|18|19)[0-9]{9}/,
                    message:"手机号码格式不正确"
                }
            }
        },
        bankName: {
            validators: {
                notEmpty: {
                    message: '开户银行不能为空'
                },
                regexp :{
                    regexp: /^[\u4e00-\u9fa5]*银行$/,
                    message:"开户银行格式不正确"
                }
            }
        },
        bankAddress:{
            validators: {
                notEmpty: {
                    message: '开户银行网点不能为空'
                },
                regexp :{
                    regexp: /^[\u4e00-\u9fa5]*银行[\u4e00-\u9fa5]*省?[\u4e00-\u9fa5]+市[\u4e00-\u9fa5]*$/,
                    message:"开户银行格式不正确"
                }
            }
        },
        bankNumber: {
            validators: {
                notEmpty: {
                    message: '银行账号不能为空'
                },
                regexp :{
                    regexp:/^\d+$/,
                    message:"银行账号格式不正确"
                }
            }
        }
    }
};

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

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

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

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

/**
 * 收集数据
 */
TrainingSubsidyInfoDlg.collectData = function() {
    this
    .set('id')
    .set('type')
    .set('enterpriseId')
    .set('year')
    .set('name')
    .set('sex')
    .set('cardType')
    .set('idCard')
    .set('nationality')
    .set('entryTime')
    .set('startTime')
    .set('endTime')
    .set('yearSalary')
    .set('yearTax')
    .set('bankName')
    .set('bankAddress')
    .set('bankNumber')
    .set('phone')
    .set('address');
}

TrainingSubsidyInfoDlg.validate = function(){
    $('#trainingInfoForm').data("bootstrapValidator").resetForm();
    $('#trainingInfoForm').bootstrapValidator('validate');
    return $("#trainingInfoForm").data('bootstrapValidator').isValid();
}

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

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

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


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

/**
 * 校验是否显示按钮
 * @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_training+"')\" class=\"btn btn-xs btn-danger\">" +
                "<i class=\"fa fa-times\"></i>删除" +
                "</button>";
        }
    }else{
        return type == 1?"":"";
    }
}

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


$(function() {
    Feng.initValidatorTip("trainingInfoForm",TrainingSubsidyInfoDlg.validateFields);
    var arr = [{"name":"nationality","code":"un_nationality"},
                {"name":"cardType","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_training,"mainId":$("#id").val(),"typeFileId":"","active":1})
    }
    TrainingSubsidyInfoDlg.validId();
});
