/**
 * 初始化银行账号变更详情对话框
 */
var locked = false;
var TalentBankChangeInfoDlg = {
    talentBankChangeInfoData : {},
    validateFields: {
        newBankName: {
            validators: {
                notEmpty: {
                    message: '现开户银行不能为空'
                },regexp :{
                    regexp: /^[\u4e00-\u9fa5]*银行$/,
                    message:"现开户银行格式不正确"
                }
            }
        },
        newBankNerPoint : {
            validators: {
                notEmpty: {
                    message: '现开户银行网点不能为空'
                },regexp :{
                    regexp: /^[\u4e00-\u9fa5]*银行[\u4e00-\u9fa5]*省?[\u4e00-\u9fa5]+市[\u4e00-\u9fa5]*$/,
                    message:"现开户银行格式不正确"
                }
            }
        },
        newBankAccount : {
            validators: {
                notEmpty: {
                    message: '现银行账号不能为空'
                },
                regexp :{
                    regexp:/^\d+$/,
                    message:"现银行账号格式不正确"
                }
            }
        },
    }
};

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

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

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

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

/**
 * 收集数据
 */
TalentBankChangeInfoDlg.collectData = function() {
    this.set('id')
    .set('type')
    .set('talentName')
    .set('talentId')
    .set('enterpriseId')
    .set('enterpriseName')
    .set('idCard')
    .set('talentArrange')
    .set('oldBankName')
    .set('oldBankNerPoint')
    .set('oldBankAccount')
    .set('oldBankNumber')
    .set('newBankName')
    .set('newBankNerPoint')
    .set('newBankAccount')
    .set('newBankNumber')
    .set('description');
}

/**
 * 验证数据
 */
TalentBankChangeInfoDlg.validate = function () {
    $('#talentBankChangeForm').data("bootstrapValidator").resetForm();
    $('#talentBankChangeForm').bootstrapValidator('validate');
    return $("#talentBankChangeForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
TalentBankChangeInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    //提交信息
    if(!this.validate()){
        return;
    }
    //提交信息
    if(locked) return ;
    locked = true;
    if(this.talentBankChangeInfoData.id!=null && this.talentBankChangeInfoData.id!=''){
        var ajax = new $ax(Feng.ctxPath + "/api/talentBankChange/update", function(data){
            if(data.code==200){
                Feng.success(data.msg);
                window.parent.TalentBankChange.table.refresh();
            }else{
                Feng.info(data.msg);
            }
            locked = false;
        },function(data){
            Feng.error("修改失败!" + data.responseJSON.message + "!");
            locked = false;
        });
        ajax.set(this.talentBankChangeInfoData);
        ajax.start();
    }else{
        var ajax = new $ax(Feng.ctxPath + "/api/talentBankChange/add", function(data){
            if(data.code==200){
                $("#id").val(data.obj.id);
                Feng.success(data.msg);
                window.parent.TalentBankChange.table.refresh();
            }else{
                Feng.info(data.msg);
            }
            locked = false;
        },function(data){
            Feng.error("添加失败!" + data.responseJSON.message + "!");
            locked = false;
        });
        ajax.set(this.talentBankChangeInfoData);
        ajax.start();
    }
}

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

/**
 * 初始化
 */
TalentBankChangeInfoDlg.talentInfoDetail = function(){
    var talentId = $("#talentId").val();
    var ajax = new $ax(Feng.ctxPath + "/api/talentInfo/detail/"+talentId, function(data){
        $("#type").val(data.type);
        $("#enterpriseId").val(data.enterpriseId);
        $("#talentName").val(data.name);
        $("#idCard").val(data.idCard);
        $("#enterpriseName").val(data.enterpriseName);
        $("#talentArrangeName").val(data.talentArrangeName);
        $("#talentArrange").val(data.talentArrange);
        $("#oldBankName").val(data.bank);
        $("#oldBankNerPoint").val(data.bankNetwork);
        $("#oldBankAccount").val(data.bankAccount);
        $("#oldBankNumber").val(data.bankNumber);
        if(data.type == 1){
            $("#newBankNumberDiv,#oldBankNumberDiv").css("display","block");
        }else{
            $("#newBankNumberDiv,#oldBankNumberDiv").css("display","none");
        }
    },function(data){
        Feng.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set();
    ajax.start();
}


TalentBankChangeInfoDlg.bankChange = function () {
    var bank = $("#newBankName").val();
    if($.trim(bank)=='中国工商银行'){
        $("#newBankNumber").val('102391050013');
    }else {
        $("#newBankNumber").val('');
    }
}


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

$(function() {
    Feng.initValidator("talentBankChangeForm", TalentBankChangeInfoDlg.validateFields);
    Feng.addAjaxSelect({
        "id": 'talentArrange',
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/api/common/findChildDictByCode?code=un_talentLevel"
    });
    var id = $("#id").val();
    var url = "";
    if(id==null||id==''){
        url = "/api/talentInfo/findTalentInfoInLibrary/"+CONFIG.project_bankchange;
        Feng.addAjaxSelect({
            "id": 'talentId',
            "displayCode": "id",
            "displayName": "name",
            "type": "GET",
            "url": Feng.ctxPath + url
        });
    }else{
        if($("#type").val() == 1){
            $("#newBankNumberDiv,#oldBankNumberDiv").css("display","block");
        }else{
            $("#newBankNumberDiv,#oldBankNumberDiv").css("display","none");
        }
    }
    $("select").each(function () {
        $(this).val($(this).attr("selectVal"));
    });
    if(id!=null && id!=''){
        $("#talentId").attr("disabled","disabled");
    }
});
