function showRiskLabels(e){
    if($('#addRisklabels').length>0){
        $('#addRisklabels').remove();
        return;
    }

    $('#memberlist').remove();
    $('#addRisklabels').remove();

    var html = T( $('#tmpl-addRisklabels').html() , DATA.risklabels );
    $('body').append( html );
    var top=e.clientY + 10;
    if($(document).height()-e.clientY<$("#addRisklabels").height()+20){
        top = e.clientY-$("#addRisklabels").height()
    }

    $('#addRisklabels').css({
        'left': e.clientX+15,
        'top':top
    });
}

//修改标签
function doSubmitSetRiskLabel(cardid,labelid,callback,timeto){
    $.get('./?app=api&apiact=set_risklabel&cardid='+cardid+"&labelid="+labelid+"&timeto="+ (timeto||"") ,function(text){
        DATA.lastShowCardID = cardid;
        var boardid = DATA.boardid || DATA.cards[cardid].boardId;
        var _id = labelid;
        var labelInfo = getRiskLabelInfo(_id);

        if(DATA.cards[cardid].risklabelIds){
            if(DATA.cards[cardid].risklabelIds.length>1 && DATA.cards[ DATA.lastShowCardID ].risklabelIds.indexOf( labelid ) == -1){
                alert('最多只能添加2个风险标签哦~');
                return;
            }
        }

        if(labelInfo){
            var labelName = labelInfo.name;
            var myinfo = getUserInfo( MYID );
            var info = myinfo.profile.fullname || "我";
            var isCardRiskLabel = DATA.cards[ DATA.lastShowCardID ].risklabelIds;

            if( !isCardRiskLabel || DATA.cards[ DATA.lastShowCardID ].risklabelIds.indexOf( labelid ) == -1 ){
                info += '<font color=red>[增加]</font>';
            }else{
                info += '<font color=red>[删除]</font>';
            }
            info += '了风险标签['+ labelName +']';
            //提交评论
            doSubmitCommonts( info,DATA.boardid,DATA.lastShowCardID );

            if(window.MYINFO && DATA.cards[ DATA.lastShowCardID ].userId != window.MYINFO._id ){
                //如果这个单子不是我创建的
                var ownerInfo = DATA.users[DATA.cards[ DATA.lastShowCardID ].userId];
                if(ownerInfo && ownerInfo.profile.fullname){
                    var msgInfo = window.MYINFO.profile.fullname + "在任务["+ DATA.cards[ DATA.lastShowCardID  ].title+']中'+ info.replace('我','');
                    // LeguWork 面板不发送消息
                    if (DATA.boardid == 'XNt2hy8Q89tEfmwC7'){
                        console.log(DATA.boardid);
                    }else {
                        sendDDMSG(ownerInfo.profile.fullname, msgInfo, location.href);
                    }
                }
            }

            // if(window.MYINFO && DATA.cards[ DATA.lastShowCardID ].userId != window.MYINFO._id ){
            //     res = DATA.cards[ DATA.lastShowCardID ].members;
            //     for(var i=0;i<res.length;i++){
            //         uid = res[i];

            //         var boardId = DATA.cards[ DATA.lastShowCardID  ].boardId;
            //         var board_title = DATA.boards[boardId].title;
            //         var msgInfo = window.MYINFO.profile.fullname + "给你分配了一条任务："+  board_title + ' ' + DATA.cards[ DATA.lastShowCardID  ].title;                    // LeguWork 面板不发送消息
            //         if (DATA.boardid == 'XNt2hy8Q89tEfmwC7'){
            //             console.log(DATA.boardid);
            //         }else {
            //             sendDDMSG(ownerInfo.profile.fullname, msgInfo, location.href);
            //         }
            //     }
            // }

            if(window.MYINFO && DATA.cards[ DATA.lastShowCardID ].userId != window.MYINFO._id ){
                res = DATA.cards[ DATA.lastShowCardID ].members;
                for(var i=0;i<res.length;i++){
                    uid = res[i];
                    var boardId = DATA.cards[ DATA.lastShowCardID  ].boardId;
                    var board_title = DATA.boards[boardId].title;
                    var msgInfo = '任务："' + board_title + ' '+ DATA.cards[ DATA.lastShowCardID  ].title + '"添加了风险提示标签，请注意查看！' ;
                    // LeguWork 面板不发送消息
                    if (DATA.boardid == 'XNt2hy8Q89tEfmwC7'){
                        console.log(DATA.boardid);
                    }else {
                        sendDDMSG(msgInfo, location.href);
                    }
                }
            }

        }

        var json = JSON.parse( text );
        emitEventByJSON(json);
        callback && callback(json);
    });
}

//增加或修改标签自身
function doSubmitModifyRiskLabel(boardid,labelid,labelname,labelcolor,callback){
    console.log(boardid,labelid,labelname,labelcolor);

    checkPermission('changeLabel',function (isok) {
        if(isok) {
            console.log('======',isok);
            $.get('./?app=api&apiact=modify_risklabel&boardid='+boardid+"&labelid="+labelid+"&labelname="+ encodeURIComponent(labelname)+"&labelcolor="+labelcolor,function(text){

                var json = JSON.parse( text );
                emitEventByJSON(json);
                callback && callback(json);
            });
        }else {
            alert('你没有权限');
        }

    })

}

//删掉标签
function doSubmitDelRiskLabel(boardid,labelid,callback){
    console.log(boardid,labelid);

    checkPermission('changeLabel',function (isok) {
        if(isok) {
            $.get('./?app=api&apiact=del_risklabel&boardid='+boardid+"&labelid="+labelid+"&labelname=",function(text){

                var json = JSON.parse( text );
                emitEventByJSON(json);
                callback && callback(json);
            });
        }else {
            alert('你没有权限');
        }

    });

}

$(function(){
    //关闭x
    var _labelColor;
    $('body').on('click','.close_addRisklabels',function(){
        $('#addRisklabels').remove();
    });

    //标签后的编辑按钮
    $('body').on('click','.labelEdit',function(e){
        var name = $(this).parent().data('labelname');
        var labelcolor = $(this).parent().data('labelcolor');
        var id = $(this).parent().data('id');

        $('.add_risklabel_li2').click();
        $('.dellabelbtn').show();
        $('.addLabelName').val(name);
        $('.addLabelID').val(id);
        $('.addLabelUL .card-label-'+labelcolor).click();
        _labelColor = labelcolor;
        return false;
    });

    $('body').on('click','.labelSetTimer',function(e){
        var me = this;
        jeDate({
            dateCell:"#"+ $(this).attr('id'),
            format:"YYYY-MM-DD",
            isinitVal:false,
            isTime:false,
            festival:true,
            okfun : function(val){
                setLabelWithTimer(val,me);
            },
            choosefun : function(val){
                setLabelWithTimer(val,me);
            }
        });
        return false;
    });

    function setLabelWithTimer(val,obj){
        //alert(val);
        //var valArr = val.split('-');
        $(obj).html("");
        $(obj).parent().data('timeto',val).click();
    }

    //列表点击
    $('body').on('click','.risklabel_li',function(e){

        var id = $(this).data('id');
        var timeto = $(this).data('timeto');
        $(this).data('timeto','');
        if(!id){
            //增加标签按钮
            $('.chooseLabelUL').hide();
            $('.addLabelUL').show();
            $('.risklabelback').show();

            $('.addLabelName').val('');
            $('.addLabelID').val('');
            $('.dellabelbtn').hide();
            return;
        }

        var card = DATA.cards[ DATA.lastShowCardID ];
        var board = DATA.boards[ card.boardId ];

        // ui验 美验
        // 已经设置的标签 再次点击是去除标签
        if(card.risklabelIds && card.risklabelIds.indexOf(id) != -1){
            // 删掉标签
            var labelName = $(this).data('labelname');

            var isOk = checkMeiShuLabelRemove(labelName);
            if(!isOk){
                return;
            }
        }


        doSubmitSetRiskLabel( DATA.lastShowCardID , id, function(json){
            //var html = T( $('#tmpl-addRisklabels').html() , DATA.risklabels );
            ////html = html.replace(/\t/g,'').replace(/\n/g,'').replace(/\r/g,'');
            //$('#addRisklabels .card-labelid-'+ id).html( $(html).find('.card-labelid-'+ id).html() );

        },timeto);
    });



    //新增or修改标签

    $('body').on('click','.risklabelback',function(){
        var _left = $('#addRisklabels').css('left');
        var _top = $('#addRisklabels').css('top');

        $('.close_addRisklabels').click();
        $('.cardinfo_addRisklabel').click();

        $('#addRisklabels').css({left:_left,top:_top});
    });

    $('body').on('click','.add_risklabel_li',function(){
        $('.add_risklabel_li').html('&nbsp;');
        $(this).html('<i class="fa fa-check"></i>');
        _labelColor = $(this).data('labelcolor');
    });

    $('body').on('click','.addRisklabelbtn',function(){
        var _name = $('.addLabelName').val();
        var _id = $('.addLabelID').val();
        if(_name.length==0)return;

        doSubmitModifyRiskLabel(DATA.boardid,_id,_name,_labelColor,function(){
            $('.risklabelback').click();
        });
    });

    $('body').on('click','.delRisklabelbtn',function(){
        // var _name = $('.addRiskLabelName').val();
        var _id = $('.addLabelID').val();

        // if(_name.length==0)return;

        doSubmitDelRiskLabel(DATA.boardid,_id,function(){
            $('.risklabelback').click();
        });
    });
});
