function showMemberList(e){
	if($('#memberlist').length>0){
		$('#memberlist').remove();
		return;
	}
	
	$('#memberlist').remove();
	$('#addlabels').remove();
	
	var html = T( $('#tmpl-memberlist').html() , DATA.users );
	$('body').append( html );

	var top=e.clientY + 10;
	if($(document).height()-e.clientY<$("#memberlist").height()+20){
		top = e.clientY-$("#memberlist").height()
	}
	
	$('#memberlist').css({
		'left': e.clientX+15,
		'top':top
	});
}

function getMemberOrder(){
	var v = localStorage['memberOrder'];
	if(!v)return {};
	return JSON.parse(v);
}

function setMemberOrder(memberid){
	var old = getMemberOrder();
	old[ memberid ] = new Date().getTime();
	localStorage['memberOrder'] = JSON.stringify(old);
}

//修改成员
function doSubmitSetMember(boardid,cardid,memberid,callback){
	setMemberOrder(memberid);
	$.get('./?app=api&apiact=set_member&boardid='+ boardid +'&cardid='+cardid+"&memberid="+memberid,function(text){
		var json = JSON.parse( text );
		emitEventByJSON(json);
		callback && callback(json);
	});
}

// 删掉延期成员
function doSubmitRemoveYanQiMember(boardid,cardid,memberid,callback){
	setMemberOrder(memberid);
	$.get('./?app=api&apiact=remove_yq_member&boardid='+ boardid +'&cardid='+cardid+"&memberid="+memberid,function(text){
		var json = JSON.parse( text );
		emitEventByJSON(json);
		callback && callback(json);
	});
}


$(function(){
	//关闭x
	$('body').on('click','.close_memberlist',function(){
		$('#memberlist').remove();
	});
	
	//列表点击
	$('body').on('click','.memlist_li',function(){
		console.log('====',$(this).find('i.fa')[0]);
		var isSelect = $(this).find('i.fa')[0];
		var uid = $(this).data('uid');
		doSubmitSetMember( DATA.boardid,DATA.lastShowCardID , uid, function(json){
			
			var html = T( $('#tmpl-memberlist').html() , DATA.users );
			html = html.replace(/\t/g,'').replace(/\n/g,'').replace(/\r/g,'');
			
			$('#memberlist .member_list_'+ uid).html( $(html).find('.member_list_'+ uid).html() ); 
			
			console.log( window.MYINFO,DATA.cards[ DATA.lastShowCardID  ].members.indexOf(uid) );
			if( window.MYINFO && DATA.cards[ DATA.lastShowCardID  ].members.indexOf(uid) != -1){
				//增加成员
				var touser = getUserInfo(uid);
				console.log('touser',touser);
				if(touser){
					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(touser.profile.fullname,msgInfo,location.href,touser.dduserid);
					}
				}
				
			}

			// 记录日志
			var uinfo = getUserInfo( uid );
			var myinfo = getUserInfo( MYID );
			var uNmae = uinfo?uinfo.profile.fullname:data.userId;
			var commontText = `${myinfo.profile.fullname}<font color=blue>[添加]</font>成员 [${uNmae}]`;
			// 有就是已经选择了
			if (isSelect){
				var commontText = `${myinfo.profile.fullname}<font color=red>[删除]</font>成员 [${uNmae}]`;
			}
			doSubmitCommonts(commontText,DATA.boardid,DATA.lastShowCardID);

		});
	});
	
	//输入框
	$('body').on('keyup','.memberlist_input',function(){
		var me = this;
		setTimeout(function(){
			var val = $(me).val();
			$('.js-card-member-list li').each(function(idx,o){
				if($(o).text().indexOf(val) != -1 ){
					$(o).show();
				}else{
					$(o).hide();
				}
			});
		},0);
	})
	
});
