/**
 * 人才认定申报管理初始化
 */
var TalentInfo = {
    id: "TalentInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TalentInfo.initColumn = function () {
    var type = $("#usertype").val();
    var isShow = true;
    if(type==2){
        isShow = false;
    };
    return [
        {field: 'selectItem', radio: true},
        {title: '申报年度', field: 'year', visible: true, align: 'center', valign: 'middle',width:'80px'},
        {title: '离职状态', field: 'active', visible: true, align: 'center', valign: 'middle',width:'80px',
            formatter : function (value,row,index) {
                if(value==1){
                    return '<span style="color:#6495ED">在职</span>';
                }if(value==2){
                    return '<span style="color:#FF82AB">离职</span>';
                }
            }
        },
        {title: '企业名称', field: 'enterpriseName', visible: true, align: 'center', valign: 'middle',width:'150px'},
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle',width:'120px',
            formatter : function(value,row,index){
                if(row.sex==1){
                    return value+'<span style="color:#6495ED">【男】</span>';
                }if(row.sex==2){
                    return value+'<span style="color:#FF82AB">【女】</span>';
                }
            }
        },
        {title: '人才层次', field: 'talentArrangeName', visible: true, align: 'center', valign: 'middle',width:'100px'},
        {title: '人才标签', field: 'talentTypeName', visible: isShow, align: 'center', valign: 'middle',width:'100px'},
        {title: '证件号码', field: 'idCard', visible: true, align: 'center', valign: 'middle',width:'150px','class': 'uitd_showTip'},
        {title: '认定条件', field: 'identifyConditionText', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:'150px'},
        {title: '认定条件名称', field: 'identifyConditionName', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:'100px'},
        {title: '人才证书有效期', field: 'identifyGetTime', visible: isShow, align: 'center', valign: 'middle','class': 'uitd_showTip',width:'150px',
            formatter:function (value,row,index) {
                return row.certificateStartTime + "至" + row.qzgccrcActiveTime ;
            }
        },
        {title: '公布入选月份',field: 'identifyMonth',visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:'100px'},
        {title: '人才编号', field: 'certificateNO', visible: true, align: 'center', valign: 'middle','class': 'uitd_showTip',width:'100px'},
        {title: '是否过期', field: 'isEffect', visible: isShow, align: 'center', valign: 'middle',width:'120px',
            formatter : function (value,row,index){
                if(value==1){
                    return '有效';
                }else if(value == 4){
                    return "失效";
                }else{
                    return "已过期";
                }
            }
        },
        {title: '操作', field: 'id', visible: true, align: 'center', valign: 'middle',width:'80px',
            formatter : function (value,row,index){
                return "<span class='label label-success' onclick=\"TalentInfo.showLog('"+value+"')\" >" +
                    "<i class=\"fa fa-book\"></i>日志" +
                    "</span>";
            }
        }
    ];
};
/**
 * 检查是否选中
 */
TalentInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TalentInfo.seItem = selected[0];
        return true;
    }
};


/**
 * 打开查看人才认定-初级审核详情
 */
TalentInfo.openTalentInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才认定申报详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/talentInfo/talentInfo_toCommonCheck/' + TalentInfo.seItem.id+'/5'
        });
        layer.full(index);
        TalentInfo.layerIndex = index;
    }
};


//取消优秀人才
TalentInfo.cancle = function(){
    if(this.check()){
        if(TalentInfo.seItem.isEffect == 4){
            Feng.info("无法重复取消");
            return ;
        }
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/talentInfo/removeFromLibrary", function (data) {
                if(data.code==200){
                    Feng.success(data.msg);
                    TalentInfo.table.refresh();
                }else{
                    Feng.error(data.msg);
                }
            }, function (data) {
                Feng.error("取消优秀人才失败!" + data.responseJSON.message + "!");
            });
            ajax.setData({"id":TalentInfo.seItem.id});
            ajax.start();
        }
        Feng.confirm("一旦取消将无法恢复，确认取消吗?", operation);
    }
}

/**
 * 恢复优秀人才
 */
TalentInfo.recovery = function(){
    if(this.check()){
        if(TalentInfo.seItem.isEffect != 4){
            Feng.info("无法恢复有效数据");
            return ;
        }
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/talentInfo/recovery", function (data) {
                if(data.code==200){
                    Feng.success(data.msg);
                    TalentInfo.table.refresh();
                }else{
                    Feng.error(data.msg);
                }
            }, function (data) {
                Feng.error("恢复优秀人才失败!" + data.responseJSON.message + "!");
            });
            ajax.setData({"id":TalentInfo.seItem.id});
            ajax.start();
        }
        Feng.confirm("确认恢复吗?", operation);
    }
}

//公示导出
TalentInfo.showExportModal = function(){
    $("#exportForm")[0].reset();
    $("#exportModal").modal("show");
}
//公示导出提交
TalentInfo.export = function(){
    var names = '';
    var values = '';
    $("#field_info li input").each(function(index){
        if($(this).is(":checked")){
            values = values + $(this).val() + ",";
            names = names + $(this).next().text() + ",";
        }
    });
    var name = $("#name").val();
    var sex = $("#sex").val();
    var idCard = $("#idCard").val();
    var nation = $("#nation").val();
    var nationality = $("#nationality").val();
    var talentType = $("#talentType").val();
    var talentArrange = $("#talentArrange").val();
    var active = $("#active").val();
    $("#exportModal").modal('hide');
    var url = Feng.ctxPath + "/talentInfoExport/libraryExport?name="+name+"&names="+names+"&values="+values
        +"&sex="+sex+"&idCard="+idCard+"&nation="+nation+"&nationality="+nationality
        +"&talentType="+talentType+"&talentArrange="+talentArrange+"&active="+active;
    window.location.href = encodeURI(encodeURI(url));
}

TalentInfo.editBasicInfo = function(){
    if(this.check()){
        layer.open({
            type: 1,
            id:"neewFieldFormModel",
            title: '审核',
            area: ['800px', '450px'], //宽高
            fix: false, //不固定
            shade:0,
            maxmin: true,
            content:'<form id="editBaiscInfo">' +
                '           <div class="form-group" style="margin: 10px;">' +
                '           <label for="checkMsgNotPass" class="control-label" >引进方式</label>' +
                '           <select class="form-control" id="introduceMode"></select>' +
                '           </div>' +
                '           <div class="form-group" style="margin: 10px;">' +
                '            <label for="checkMsgNotPass" class="control-label" >是否同步至津补贴申报</label>' +
                '            <select class="form-control" id="isSync">' +
                '                 <option value="2">否</option>                                 '+
                '                 <option value="1">是</option>                                 '+
                '            </select>' +
                '          </div>' +
                '            </form>',
            btn: ['<i class="fa fa-save"></i>&nbsp;&nbsp;提交' ,'<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
            btnAlign: 'c',
            zIndex: layer.zIndex,
            success:function(layero, index){
                var arr = [{"name":"introduceMode","code":"un_introduction_mode"}];
                Feng.findChildDictBatch(JSON.stringify(arr));
            },
            yes: function (index, layero) {
                var introduceMode = $("#introduceMode").val();
                var isSync = $("#isSync").val();
                if(Feng.isEmptyStr(introduceMode)){
                    Feng.info("请选择引进方式");return ;
                }
                var operation = function(){
                    var ajax = new $ax(Feng.ctxPath + "/talentInfo/editBasicInfo", function (data) {
                        if(data.code==200){
                            Feng.success(data.msg);
                            TalentInfo.table.refresh();
                            layer.close(index);
                        }else{
                            Feng.error(data.msg);
                        }
                    }, function (data) {
                        Feng.error("修改失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("id",TalentInfo.seItem.id);
                    ajax.set("introductionMode",introduceMode);
                    ajax.set("isSync",isSync);
                    ajax.start();
                }
                Feng.confirm("确认修改吗?", operation);
            }
        });
    }
}

//回调
TalentInfo.callBack = function (data){
    Feng.info(data.msg);
}

/**
 * 查看首次申报基础信息
 */
TalentInfo.selectFirstInfo = function(){
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '人才认定首次申报信息',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/talentInfo/talentInfo_toCommonCheck/' + TalentInfo.seItem.id+'/6'
        });
        layer.full(index);
        TalentInfo.layerIndex = index;
    }
}



$(function () {
    var defaultColunms = TalentInfo.initColumn();
    var table = new BSTable(TalentInfo.id, "/talentInfo/list/5", defaultColunms);
    table.setPaginationType("server");
    table.setOnDblClickRow(function () {
        TalentInfo.openTalentInfoDetail();
    });
    table.setRowStyle(function (row,index) {
        if(row.isEffect==1){
            return {};
        }
        if(row.isEffect==2){
            return {classes:'warning'};
        }
        if(row.isEffect==3){
            return {classes:'danger'};
        }
        if(row.isEffect == 4){
            return {classes: 'info'}
        }
    });
    var t = TalentInfo.table = table.init();
    TalentInfo.init();
    //批量加载时间控件
    $(".time").each(function(){
        laydate.render({
            elem: "#"+$(this).attr("id")
            ,type: "date"
            ,trigger: 'click'
        });
    });
});
