//需要 dialog插件，wx插件  //目前采用pagetype = 0
var d = dialog({contentType:'load', skin:'bk-popup'});
var Request = new Object();
	Request = GetRequest();
var userAgent = window.localStorage.getItem(agentName);
var toUser = Request["toUser"];
var toName = decodeURIComponent(Request["toName"]);
var openid = Request["openid"];
var represented = Request["represented"];

var params = {};
params.pageUrl = window.location.href;
$.ajax(server + "weixin/getSign", {
	data: params,
	dataType: "json",
	type: "post",
	success: function(res){
		if (res.status == 200) {
			var t = res.data.timestamp;
			var noncestr = res.data.noncestr;
			var signature = res.data.signature;
			wx.config({		
				//debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
			    appId: appId, // 必填，公众号的唯一标识
			    timestamp: t, // 必填，生成签名的时间戳
			    nonceStr: noncestr, // 必填，生成签名的随机串
			    signature: signature,// 必填，签名，见附录1
			    jsApiList: [
			    	'closeWindow'
			    ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
			})
		} 
	}
})

function isRepresent(fun){
	if(!userAgent) {
		window.localStorage.setItem(pageName, JSON.stringify({pageurl: location.href}))
        window.location.href = server + "wx/html/home/html/zhmm-login.html?type=0&openid=" + openid;
		return false;	
	}
	var newUaObj = typeof(userAgent)=='object'?userAgent:JSON.parse(userAgent)
	//判断关系
	function hasFamilyRelation(a,b){
		return new Promise(function(resolve, reject) {
			var data={
				patient:a,
				familyMember:b
			}
			sendPost("patient/family/is_authorize", data, "json", "get", function(res){
				dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请求家庭成员关系失败'}).show(); 
			}, function(res){
				if(res.status == 200) {
					window.localStorage.setItem('nowPatientName',res.data.name);
					resolve(res)
				}else if(res.status == 100) {
					dialog({
						content:'对不起，'+res.data.name+'未授权给您，如需查看，请切换'+res.data.name+'账号登录',
						okValue:'切换账号',
						ok: function() {
							window.localStorage.setItem(pageName, JSON.stringify({pageurl: location.href}))
							window.location.href = "../../grzx/html/change-login.html?type=0&openid="+openid;
						},
						cancelValue: '我不看了',
						cancel: function () {
							wx.closeWindow();
			            }
					}).showModal();
				}else{
					relogin();
				}	
			})
		})
	}
	//重新登录操作
	function relogin(){
		dialog({
			content:'对不起，该消息是发给'+toName+'，如需查看，请切换'+toName+'账号登录',
			okValue:'切换账号',
			ok: function() {
				window.localStorage.setItem(pageName, JSON.stringify({pageurl: location.href}))
				window.location.href = "../../grzx/html/change-login.html?type=0&openid="+openid;
			},
			cancelValue: '我不看了',
			cancel: function () {
				wx.closeWindow();
            }
		}).showModal();
	}
	//保存信息
	function saveUserInfo(a,b){
		Promise.all([hasFamilyRelation(a,b)]).then(function () {
			newUaObj.represented = represented;
			window.localStorage.setItem(agentName,JSON.stringify(newUaObj));
			fun && fun.call(this);
		})
	}
	
	function initFamilyData () {
		if(!represented){
			fun && fun.call(this);
			return
		}
		//从微信模板消息进入
		if(represented && userAgent && toUser){
			if(toUser == represented){
				saveUserInfo(represented,newUaObj.uid)
			}else if(toUser != represented && represented == newUaObj.uid){
				//本人代理
				saveUserInfo(represented,newUaObj.uid)
			}else if(toUser != represented && toUser == newUaObj.uid){
				//需要判定关系 200有授权 100家人 1无关系	
				saveUserInfo(represented,newUaObj.uid)
			}else{
				//重新登录	
				relogin();
			}
		}
	}
	initFamilyData();
}