/**
 * 初始化重大专项经费配套详情对话框
 */
var MajorProjectsInfoDlg = {
    majorProjectsInfoData : {}
};

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

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

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

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

/**
 * 收集数据
 */
MajorProjectsInfoDlg.collectData = function() {
    this
    .set('id')
    .set('type')
    .set('talentId')
    .set('enterpriseId')
    .set('name')
    .set('sex')
    .set('cardType')
    .set('idCard')
    .set('talentArrange')
    .set('nationality')
    .set('entryTime')
    .set('startTime')
    .set('endTime')
    .set('post')
    .set('bankName')
    .set('bankAddress')
    .set('bankNumber')
    .set('phone')
    .set('address')
    .set('email')
    .set('documentName')
    .set('degree')
    .set('fundsSupport')
    .set('quotaProportion');
}

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

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

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

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



/**
 * 提交审核
 */
MajorProjectsInfoDlg.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.MajorProjects.table.refresh();
                MajorProjectsInfoDlg.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){
        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_major+"')\" class=\"btn btn-xs btn-danger\">" +
                "<i class=\"fa fa-times\"></i>删除" +
                "</button>";
        }
    }else{
        return type == 1?"":"";
    }
}

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


MajorProjectsInfoDlg.sourceChange = function(){
    var source = $("#source").val();
    if(Feng.isEmptyStr(source)){
        $("input,select").not("input[type='hidden'],#source").val("");
    }else if(source == 1){
        $("#approveObj").empty().append(
            '<label class=" control-label spacing"><span style="color: red">*</span>姓名</label>\n' +
            '<select class="form-control" id="talentId" name="talentId">\n' +
            '</select>\n' +
            '<input type="hidden" id="name" name="name" />');
        MajorProjectsInfoDlg.getTalent();
        $("#talentArrange").css("display","block");
        $("#sex,#cardType,#idCard,#nationality,#talentArrange,#entryTime,#startTime,#endTime,#post,#phone,#email,#address,#bankName,#bankAddress,#bankNumber").attr("style","pointer-events: none;background-color: #eee;");
    }else if(source == 2){
        $("#approveObj").empty().append(
            '<label class=" control-label spacing"><span style="color: red">*</span>姓名</label>\n' +
            '<input class="form-control" id="name" name="name">\n' );
        $("#talentArrange").css("display","none");
        $("#sex,#cardType,#idCard,#nationality,#talentArrange,#entryTime,#startTime,#endTime,#post,#email,#address,#bankName,#bankAddress,#bankNumber").removeAttr("style");
    }
}


MajorProjectsInfoDlg.getTalent = function(){
    Feng.addAjaxSelect({
        "id": "talentId",
        "displayCode": "id",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/api/talentInfo/findTalentInfoInLibrary/"+CONFIG.project_major
    });
}

$(function() {
    MajorProjectsInfoDlg.getTalent();
    Feng.initValidatorTip("majorInfoForm",MajorProjectsInfoDlg.validateFields);
    var arr = [{"name":"nationality","code":"un_nationality"},
        {"name":"cardType","code":"un_cardType"},
        {"name":"talentArrange","code":"un_talentLevel"},
        {"name":"degree","code":"un_major_degree"},
        {"name":"quotaProportion","code":"un_major_proportion"}];
    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_major,"mainId":$("#id").val(),"typeFileId":"","active":1})
    }
    MajorProjectsInfoDlg.validId();
});
