var d = dialog({contentType:'load', skin:'bk-popup'});
var dd = dialog({contentType:'load', skin:'bk-popup', content:'提交中...'});
var id = 0;
var openid =null;
var userUid =null;
//var firstUid = window.localStorage.getItem("firstUid");
//var firstName = window.localStorage.getItem("firstName");
var userAgent = window.localStorage.getItem(agentName);
var username = "";
var userphoto = "../../../images/p-default.png";
var index = 0;
var usercode = "";
var chooseName = "";
var pagetype=11;
if(userAgent){
	var jsonstr = $.parseJSON(userAgent);
	openid = jsonstr.openid;
	userUid = jsonstr.uid;
}
var scroller1 = new IScrollPullUpDown('wrapper',{
	probeType:2,
	bounceTime: 250,
	bounceEasing: 'quadratic',
	mouseWheel:false,
	scrollbars:true,
	fadeScrollbars:true,
	click:true,
	interactiveScrollbars:false
}, pullDownAction, null);

$(function(){
	$(".tips a").on("click", function () {
		window.location.href = server + "wx/html/home/html/zhmm-login.html?type=11&openid=" + openid;
	});
	// 判断是否登录
	checkUserAgent();
	// 点击咨询详情
	$("body").on("tap", ".n-list", function () {
		index = $(this).parents("li").index();
		if ($(this).is(".list-last")) {
			window.location.href = "../../jtgx/html/add-step1.html";
		} else if ($(this).is(".on")) {
			return ;
		} else {
			dd.show();
			chooseName = $(this).find("h2").html();
			var data = {};
			data.family = $(this).data("code");
			data.openid = openid;
			sendPost("patient/switch_account", data, "json", "post", changeFailed, changeSuccesss);
		}
	});
	// 点击登录
	$("#login").on("click", function () {
			clearAgent();
			window.location.href = "../../home/html/zhmm-login.html?type=11&openid="+openid;
	});
});

//下拉刷新
function pullDownAction(theScrollerTemp) {
	$(".pullDown").show();
	setTimeout(function () {
		$("#ul_family").html("");
		queryInit();
	}, 1000);
}

//上拉加载数据
function pullUpAction(theScrollerTemp) {
	$(".pullUp").show();
	setTimeout(function () {
		queryInit();
	}, 1000);
}

// 禁止拖动
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

/**
 * 界面数据初始化
 */
function queryInit() {
	d.show();
	var data={};
	sendPost('patient/baseinfo', data, 'json', 'post', queryFailed, querySuccess);
}

function queryFailed(res) {
	d.close();
	if (res && res.msg) {
		var d1 = dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();     
	} else {
		var d2 = dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'加载失败'}).show(); 
	}
}

function querySuccess(res) {
	if (res.status == 200) {
		var resData = res.data;
		if (resData.name) {
			username = resData.name;
			// 提示
			// var text;
			// if (userUid == firstUid) {
			// 	text = '您可在首次登录账号的家人中自由切换，若需切换到其他账号，请直接登录其账号。<a id="login" href="javascript:;">登陆其他账号</a>'
			// } else {
			// 	text = '因您通过' + firstName + '账号切换过来，故只可切换至与' + firstName + '有家庭关系的账号，已自动过滤不可切换的账号。若想切换到' + username + '的其他家人账号，请直接登录' + username + '账号后切换。<a id="login" href="javascript:;">登陆其他账号</a>';
			// }
			// $("#tip").html(text);
		}
		if (resData.photo && resData.photo != "") {
			userphoto = getImgUrl(resData.photo);
		}
		var data = {};
		data.patient = userUid;
		usercode = resData.code;
		data.isAgree = 1;//已经同意授权
		sendPost("patient/family/members", data, "json", "get", queryListFailed, queryAppoListSuccesss);
	} else {
		//非200则为失败
		queryFailed(res);
	}
}

function queryListFailed(res) {
	d.close();
	if (res && res.msg) {
		var d1 = dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();     
	} else {
		var d2 = dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'加载失败'}).show(); 
	}
}

function queryAppoListSuccesss(res) {
	if (res.status == 200) {
		var $list = document.querySelector("#ul_family");
		var $liFirst = document.createElement("li");
	    firstContent = '<div class="n-list dr-item c-border on" data-code="' + usercode + '">'+
							'<div class="n-list-li">'+
								'<div class="n-list-key">'+
									'<div class="n-avatar-l">'+
										'<img class="c-images-cycle" src="' + userphoto + '">'+
									'</div>'+
								'</div>'+
								'<div class="n-list-info pl15">'+
									'<h2>' + username + '</h2>'+
									// '<p>自己</p>'+
								'</div>'+
								'<div class="n-list-key checked">'+
									'<i class="icon-checked"></i>'+
								'</div>'+
							'</div>'+
						'</div>';
	    $liFirst.innerHTML = firstContent;
		$list.appendChild($liFirst);
		if (res.data.normalmembers.length > 0) {
			var list = res.data.normalmembers;
			for (var i = 0; i < list.length; i++) {
				var $li = document.createElement("li");
				var code = list[i].code;
				var name = list[i].name;
				var familyRelationName = list[i].familyRelationName;
				var photo = getImgUrl(list[i].photo);
				
				if(userUid == code){
					continue;
				}

				// 头像为空设置默认头像
				if(photo == undefined || photo == null || photo == ""){
					photo = "../../../images/p-default.png";
				}

				var childContent = "";
				childContent =  '<div class="n-list dr-item c-border" data-code="' + code + '">'+
									'<div class="n-list-li">'+
										'<div class="n-list-key">'+
											'<div class="n-avatar-l">'+
												'<img class="c-images-cycle" src="' + photo + '">'+
											'</div>'+
										'</div>'+
										'<div class="n-list-info pl15">'+
											'<h2>' + name + '</h2>'+
											// '<p>' + familyRelationName + '</p>'+
										'</div>'+
										'<div class="n-list-key checked">'+
											'<i class="icon-checked"></i>'+
										'</div>'+
									'</div>'+
								'</div>';
				$li.innerHTML = childContent;
				$list.appendChild($li);
			}
			// var $liLast = document.createElement("li");
		 //    lastContent = '<div class="n-list list-last dr-item c-t-center">'+
		 //            			'<i class="icon-add"></i>'+
		 //            			'<p>添加家人</p>'+
		 //            		'</div>';
		 //    $liLast.innerHTML = lastContent;
			// $list.appendChild($liLast);
		} 
		// else {
		// 	var $li = document.createElement("li");
		// 	var content =   '<div class="n-list list-last dr-item c-t-center">'+
		//             			'<i class="icon-add"></i>'+
		//             			'<p>添加家人</p>'+
	 //            			'</div>';
		// 	$li.innerHTML = content;
		// 	$list.appendChild($li);
		// }
		d.close();
		scroller1.myScroll.refresh();
		$(".pullUp").hide();
		$(".pullDown").hide();
	} else {
		//非200则为失败
		queryListFailed(res);
	}
}

function changeFailed(res) {
	dd.close();
	if (res && res.msg) {
		var d1 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show();
	} else {
		var d2 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show();
	}
}

function changeSuccesss(res) {
	if (res.status == 200) {
		dd.close();
		var d1 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show();
		var resData = res.data;
		$("#ul_family").find(".n-list").removeClass("on");
		$("#ul_family").find("li").eq(index).find(".n-list").addClass("on");
		wxSaveUserAgent(resData.id, resData.uid, openid, resData.token,resData.lastUid);
		wxSaveUserAgent1(resData.uid, resData.name, resData.photo);
		 setTimeout(function () {
		 	window.location.href = "my-detail.html";
		 }, 1000);
	} else {
		//非200则为失败
		changeFailed(res);
	}
	// else if (res.status == -2) {
	// 	dd.close();
	// 	var d2 = dialog({
	// 				content:'对不起，您与' + chooseName + '无家庭关系哦，如需登录' + chooseName + '账号，请直接登录或登录' + chooseName + '账号后切换',
	// 				okValue: "直接登录",
	// 				ok: function (){
	// 					var jsonstr = $.parseJSON(agentOpenid);
	// 				    var openid = jsonstr.openid;
	// 				    window.location.href = server + "wx/html/home/html/zhmm-login.html?type=11&openid=" + openid;
	// 				},
	// 				cancelValue: "不了，谢谢",
	// 				cancel: function () {
						
	// 				}
	// 			}).showModal();
	// }
}