//基于mui的scroll之上
function appendFamilyMember(obj,fun,cssfun,opts){
	var data = {},
		$obj = obj;
	var userAgent = JSON.parse(window.localStorage.getItem(agentName));
	if(userAgent){
		//删除represented字段	
		if(userAgent.represented){
			delete userAgent.represented
		}
		window.localStorage.setItem(agentName,JSON.stringify(userAgent))
	}
	
	sendPost("patient/family/authorize_members", data, "json", "get", queryListFailed, queryAppoListSuccesss);
	
	function queryAppoListSuccesss(res){
		if (res.status == 200) {
			if (res.data.length > 0) {
				showMember(res);
				if(res.data.length == 1){
					$obj.hide();
				}else{
				    cssfun && cssfun.call(this);
				}
			}else{
				dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'加载家庭成员失败'}).show(); 
			}
		}else{
			//非200则为失败
//			queryListFailed(res);
		}	
	}
	
	function queryListFailed(res) {
//		if (res && res.msg) {
//			dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();     
//		} else {
//			dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'加载家庭成员失败'}).show(); 
//		}
	}
	
	function showMember(res){
		var html='',
			data=res.data;
		for(var i=0;i<data.length;i++){
			html = html + '<div class="mui-control-item" data-code="'+data[i].code+'" data-name="'+data[i].name+'" style="display: inline-block;margin-right: 15px;-webkit-user-select: none;vertical-align: middle;text-align: center;">\
							<img style="width: 40px;height: 40px;border-radius: 50%;overflow: hidden;border:solid 1px #dcdcdc;" src="'+ getImgUrl(data[i].photo)+'"/>\
							<div class="name" style="font-size: 12px;color: #333;line-height: 20px;">'+data[i].name+'('+data[i].familyRelationName+')</div></div>'
		}
		
		if(opts && opts.btns) {
			for(var i=0;i<opts.btns.length;i++){
				html = html + '<div id="'+(opts.btns[i].id||"")+'" style="display: inline-block;margin-right: 15px;-webkit-user-select: none;vertical-align: middle;text-align: center;">\
								<img style="width: 40px;height: 40px;border-radius: 50%;overflow: hidden;border:solid 1px #dcdcdc;" src="'+ opts.btns[i].img +'"/>\
								<div class="name" style="font-size: 12px;color: #333;line-height: 20px;">'+opts.btns[i].text+'</div></div>'
			}
		}
		
		html = '<div class="mui-scroll-wrapper family-member" style="position: absolute;z-index: 999;top: 0;bottom: 0;left: 0;overflow: hidden;width: 100%;height: 90px;background-color:#f3f3f3;padding-left: 15px;padding-top: 7px;border-bottom:solid 1px #dcdcdc;box-sizing: border-box;">\
		<div class="mui-scroll" style="position: absolute;width: auto;z-index: 99;white-space: nowrap;-webkit-transform: translateZ(0);transform: translateZ(0);">'+html+'</div></div>';

		$obj.append(html);
		setTimeout(function(){
			addEvent();
		},100)
	}

	function addEvent(){
		mui('.family-member').scroll({
			scrollY: false, //是否竖向滚动
			scrollX: true, //是否横向滚动
		});
		
		function active(one){
			one.find('img').css({width: '55px',height: '55px'});
			one.find('.name').css({'font-size': '14px','line-height':'23px'});
		}

		//关联被代理人
		var $ul = $obj.find('.mui-control-item');
		if(userAgent.represented){
			for(var i=0;i<$ul.length;i++){
				if($ul.eq(i).data('code') == userAgent.represented){
					active($ul.eq(i));
					window.localStorage.setItem('nowPatientName',$ul.eq(i).data('name'))
				}
			}
		}else{
			active($ul.eq(0));
			window.localStorage.setItem('nowPatientName',$ul.eq(0).data('name'))
		}
		
		$obj.on('tap','.mui-control-item',function(){
			var $this = $(this),
				$code = $this.data('code');
				window.localStorage.setItem('nowPatientName',$this.data('name'))

			//添加represented字段	
			var userobj = userAgent;
				userobj.represented = $code;
			var str = JSON.stringify(userobj);
			window.localStorage.setItem(agentName,str)
			
			$('.mui-control-item').find('img').css({width: '40px',height: '40px'});
			$('.mui-control-item').find('.name').css({'font-size': '12px','line-height':'20px'});
			active($this);
			fun && fun.call(this,$code);
		})	
	}		
}

//获取家庭所有成员
function familyAllMembers(){
	var data = {};
	return new Promise(function(resolve, reject) {
		sendPost("patient/family/authorize_members", data, "json", "get", queryFailed, function(res){
			if (res.status == 200) {
				if (res.data.length > 0){
					resolve(res.data);
				} else {
					
				}
			}else{
				//非200则为失败
				queryFailed(res);
			}	
		});
	})
	function queryFailed(res) {
		if (res && res.msg) {
			dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();     
		} else {
			dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'加载家庭成员失败'}).show(); 
		}
	}
}
	