/**
 * 初始化房租补助详情对话框
 */
var HouseRentingFeesInfoDlg = {
    houseRentingFeesInfoData : {},
    validateFields: {
        baseId: {validators: {notEmpty: {message: '申报对象不能为空'}}},
        email: {validators: {notEmpty: {message: '电子邮箱不能为空'}}},
        bankAddress: {validators: {notEmpty: {message: '开户银行网点不能为空'}}},
        bankAccount: {validators: {notEmpty: {message: '银行账号不能为空'}}},
    }
};

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

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

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

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

/**
 * 收集数据
 */
HouseRentingFeesInfoDlg.collectData = function() {
    this
    .set('id')
    .set('baseId')
    .set('phone')
    .set('email')
    .set('bankAddress')
    .set('bankAccount')
    .set('checkState');
}


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


HouseRentingFeesInfoDlg.nameChange = function(){
    var baseId = $("#baseId").val();
    if(Feng.isNotEmptyStr(baseId)){
        var ajax = new $ax(Feng.ctxPath + "/api/houseRenting/detail/"+baseId, function(data){
            var talentInfo = data;
            $("#sex").val(talentInfo.sex);
            $("#cardTypeName").val(talentInfo.cardTypeName);
            $("#idCard").val(talentInfo.idCard);
            $("#nativePlace").val(talentInfo.nativePlace);
            $("#talentArrangeName").val(talentInfo.talentArrangeName);
            $("#entryTime").val(talentInfo.entryTime);
            $("#post").val(talentInfo.post);
            $("#enterpriseAddress").val(talentInfo.enterpriseAddress);
            $("#phone").val(talentInfo.phone);
            $("#email").val(talentInfo.email);
            $("#marryStatusName").val(talentInfo.marryStatusName);
            $("#houseTypeName").val(talentInfo.houseTypeName);
            $("#rentAddress").val(talentInfo.rentAddress);
            $("#rentStartTime").val(talentInfo.rentStartTime);
            $("#rentEndTime").val(talentInfo.rentEndTime);
            $("#rentMoney").val(talentInfo.rentMoney);
            $("#spouseName").val(talentInfo.spouseName);
            $("#spouseSex").val(talentInfo.spouseSex);
            $("#spouseCardTypeName").val(talentInfo.spouseCardTypeName);
            $("#spouseIdCard").val(talentInfo.spouseIdCard);
            $("#spouseNativePlace").val(talentInfo.spouseNativePlace);
            $("#spouseTalentArrangeName").val(talentInfo.spouseTalentArrangeName);
            $("#spouseEntryTime").val(talentInfo.spouseEntryTime);
            $("#spousePost").val(talentInfo.spousePost);
            $("#spouseEnterpriseName").val(talentInfo.spouseEnterpriseName);
            $("#spouseEnterpriseAddress").val(talentInfo.spouseEnterpriseAddress);
            $("#spousePhone").val(talentInfo.spousePhone);
            $("#spouseEmail").val(talentInfo.spouseEmail);
        },function(data){
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}



/**
 * 提交添加
 */
HouseRentingFeesInfoDlg.addSubmit = function() {
    var id = $("#id").val();
    if(Feng.isNotEmptyStr(id)){
        HouseRentingFeesInfoDlg.editSubmit(1);
        return;
    }
    this.clearData();
    this.collectData();
    if(!HouseRentingFeesInfoDlg.validate()){
        return ;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/api/houseRentingFees/add", function(data){
        if(data.code == 200){
            Feng.success(data.msg);
            $("#id").val(data.obj.id);
            $("#fileLi").removeAttr("style");
            $("#checkState").val(data.obj.checkState);
        }else{
            Feng.info(data.msg);
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.houseRentingFeesInfoData);
    ajax.start();
}

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

/**
 * 提交审核
 */
HouseRentingFeesInfoDlg.submitToCheck = function(){
    if(!validateIsEdit())return;
    var operation = function() {
        var ajax = new $ax(Feng.ctxPath + "/api/houseRentingFees/submitToCheck", function (data) {
            if(data.code==200){
                Feng.success(data.msg);
                window.parent.HouseRentingFees.table.refresh();
                HouseRentingFeesInfoDlg.close();
            }else{
                Feng.error(data.msg);
            }
        }, function (data) {
            Feng.error("提交审核失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id", $("#id").val());
        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 )){
        return type == 1?
            "<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>"
            :
            "<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_rentingFees+"')\" class=\"btn btn-xs btn-danger\">" +
            "<i class=\"fa fa-times\"></i>删除" +
            "</button>";
    }else{
        return type == 1?"":"" ;
    }
}

function validateIsEdit(){
    var id = $("#id").val();
    var checkState = $("#checkState").val();
    if(Feng.isEmptyStr(id)){
        Feng.info("请先填写基础信息并上传附件");
        return false;
    }
    if(checkState != 1 && checkState != 4){
        Feng.info("正在审核中或已审核完成，无法操作");
        return false;
    }
    return true;
}

$(function() {
    Feng.initValidatorTip("houseRentingFeesInfoForm", HouseRentingFeesInfoDlg.validateFields);
    var id = $("#id").val();
    if(Feng.isNotEmptyStr(id)){
        $("select").each(function () {$(this).val($(this).attr("value")).trigger("change");});
        $("#fileLi").removeAttr("style");
        Feng.getCheckLog("logTable",{"type":CONFIG.project_rentingFees,"mainId":id,"typeFileId":"","active":1})
    }else{
        $("#fileLi").attr("style","pointer-events: none");
    }
});
