/**
 * 初始化人才基础信息变更详情对话框
 */
var TalentBasicChangeInfoDlg = {
    talentBasicChangeInfoData: {},
    validateFields: {
        talentId: {validators: {notEmpty: {message: '原姓名不能为空'}}},
        newName: {validators: {notEmpty: {message: '现姓名不能为空'}}},
        newBirthday: {validators: {notEmpty: {message: '现出生日期不能为空'}}},
        newNationality: {validators: {notEmpty: {message: '现国家/地区不能为空'}}},
        newNation: {validators: {notEmpty: {message: '现民族不能为空'}}},
        newPolitics: {validators: {notEmpty: {message: '现政治面貌不能为空'}}},
        newCardType: {validators: {notEmpty: {message: '现证件类型不能为空'}}},
        newIdCard: {validators: {notEmpty: {message: '现证件号码不能为空'}}},
        newEmail: {validators: {notEmpty: {message: '现电子邮箱不能为空'}}},
    }
};

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

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

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

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

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


/**
 * 收集数据
 */
TalentBasicChangeInfoDlg.collectData = function () {
    this
        .set('id')
        .set('type')
        .set('enterpriseId')
        .set('talentId')
        .set('newName')
        .set('newBirthday')
        .set('newNationality')
        .set('newNation')
        .set('newPolitics')
        .set('newCardType')
        .set('newIdCard')
        .set('newEmail');
}


TalentBasicChangeInfoDlg.nameChange = function () {
    var talentId = $("#talentId").val();
    if (Feng.isNotEmptyStr(talentId)) {
        var ajax = new $ax(Feng.ctxPath + "/api/talentInfo/detail/" + talentId, function (data) {
            var talentInfo = data;
            $("#oldBirthday").val(talentInfo.birthday);
            $("#oldNationality").val(talentInfo.nationality);
            $("#oldNation").val(talentInfo.nation);
            $("#oldPolitics").val(talentInfo.politics);
            $("#oldCardType").val(talentInfo.cardType);
            $("#oldIdCard").val(talentInfo.idCard);
            $("#oldEmail").val(talentInfo.email);
            $("#newName").val(talentInfo.name);
            $("#newBirthday").val(talentInfo.birthday);
            $("#newNationality").val(talentInfo.nationality);
            $("#newNation").val(talentInfo.nation);
            $("#newPolitics").val(talentInfo.politics);
            $("#newCardType").val(talentInfo.cardType);
            $("#newIdCard").val(talentInfo.idCard);
            $("#newEmail").val(talentInfo.email);
        }, function (data) {
            Feng.error("查询失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}

/**
 * 提交添加
 */
TalentBasicChangeInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!TalentBasicChangeInfoDlg.validate()) {
        return;
    }
    var id = $('#id').val();
    if (Feng.isNotEmptyStr(id)) {
        TalentBasicChangeInfoDlg.editSubmit();
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/api/talentBasicChange/add", function (data) {
        if (data.code == 200) {
            Feng.success(data.msg);
            $("#id").val(data.obj.id);
            $("#talentId").prop("disabled", true).trigger("chosen:updated");
            $("#fileLi").removeAttr("style");
            $("#checkState").val(data.obj.checkState);
        } else {
            Feng.info(data.msg);
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.talentBasicChangeInfoData);
    ajax.start();
}

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

function validateIsEdit () {
    var id = $("#id").val();
    if (id == null || id == '') {
        Feng.info("请先添加基本信息并保存后再试");
        return false;
    }
    var checkState = $("#checkState").val();
    if (checkState != -1 && checkState != 2) {
        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 ==-1 ||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_basicchange+"')\" class=\"btn btn-xs btn-danger\">" +
                "<i class=\"fa fa-times\"></i>删除" +
                "</button>";
        }
    }else{
        return type == 1?"":"";
    }
}



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


$(function () {
    Feng.initValidatorTip("basicChangeForm", TalentBasicChangeInfoDlg.validateFields);
    var hand = $("#hand").val();
    var id = $("#id").val();
    var checkState = $("#checkState").val();
    //批量加载字典表数据
    var arr = [
        {"name": "oldNationality", "code": "un_nationality"},
        {"name": "oldNation", "code": "un_nation"},
        {"name": "oldPolitics", "code": "un_political"},
        {"name": "oldCardType", "code": "un_cardType"},
        {"name": "newNationality", "code": "un_nationality"},
        {"name": "newNation", "code": "un_nation"},
        {"name": "newPolitics", "code": "un_political"},
        {"name": "newCardType", "code": "un_cardType"}
    ];
    Feng.findChildDictBatch(JSON.stringify(arr));
    if (Feng.isNotEmptyStr(id)) {
        //select初始化
        $("select").each(function () {
            $(this).val($(this).attr("value")).trigger("change");
        });
        $("#fileLi").removeAttr("style");
        Feng.getCheckLog("logTable", {"type": CONFIG.project_basicchange, "mainId": id, "typeFileId": "", "active": 1})
    } else {
        $("#fileLi").attr("style","pointer-events: none");
        $("#talentId").on('chosen:ready', function (e, params) {
            $(".chosen-container-single .chosen-single").css("padding", "4px 0px 0px 8px");
        });
        $("#talentId").chosen({
            search_contains: true,       //关键字模糊搜索。设置为true，只要选项包含搜索词就会显示；设置为false，则要求从选项开头开始匹配
            disable_search: false,
            width: "100%",
            enable_split_word_search: true,
            rtl: true
        });
    }
});
