//我的账号
mui.ready(function() {
	/*定义全局变量*/
	var loginYes = document.getElementById("loginYes");
	var loginNo = document.getElementById("loginNo");
	var goLogin = document.getElementById("gologin");
	var goReg = document.getElementById("goreg");
	var goSetup = document.getElementById("goSetup");
	var goZixun = document.getElementById("goZixun");
	var goZixunbox = document.getElementById("goZixunbox");
	var gonXuqiu=document.getElementById("demand");
	var oEdit = document.getElementById("editbox");
	var goFollow = document.getElementById("goFollow");
	var userImg = document.getElementById("userImg");
	var nameli = document.getElementById("nameli");
	var goBecomeExpert = document.getElementById("goBecomeExpert");
	var infobasic = document.getElementsByClassName("amend")[0];
	var oFlag;
	var oFlag1;
	var clickFlag = true;
	var professorName;
	mui.plusReady(function() {

		var userId = plus.storage.getItem('userid');
		console.log(userId);
		/*判断登录是否成功*/
		loginStatus();

		/*用户信息初始化*/
		userInformation();

		/*登录按钮*/
		loginNo.addEventListener('tap', function() {
			goLoginFun();
		})

		/*注册按钮*/
		/*goReg.addEventListener('tap', function() {
			goRegFun();
		})*/

		/*退出登录刷新页面*/
		window.addEventListener('closeUser', function(event) {
			userId = event.detail.id;
			console.log(userId);
			loginStatus();
			userInformation()
		});
		//在修改上传图片触发的事件
		window.addEventListener('photoUser', function(event) {
			nameli.classList.remove(nameli.classList[2])
			nameli.classList.remove(nameli.classList[2]);
			nameli.innerHTML = ""
			userInformation();
		});

		function loginStatus() {
			console.log(userId);
			if(userId && userId != "null" && userId != null) {
				loginNo.style.display = "none";
				loginYes.style.display = "block";
				//alert('dd')
				/*设置*/
				goSetup.addEventListener('tap', function() {
					mui.openWindow({
						url: '../html/setup.html',
						id: '../html/setup.html',
						show: {
							aniShow: "slide-in-right"
						}
					});
				})
				/*我的需求*/
				gonXuqiu.addEventListener('tap', function() {
					mui.openWindow({
						url: '../html/needList.html',
						id: '../html/needList.html',
						show: {
							autoShow: false,
							aniShow: "slide-in-right"
						}
					});
				})
				/*我的关注*/
				goFollow.addEventListener('tap', function() {
					mui.openWindow({
						url: '../html/attentions.html',
						id: '../html/attentions.html',
						show: {
							//autoShow: false,
							aniShow: "slide-in-right"
						}
					});
				})
				
				/*成为专家*/
				goBecomeExpert.addEventListener('tap', function() {
					mui.openWindow({
						url: '../html/expert-authentication.html',
						id: 'expert-authentication.html',
						show: {
							//autoShow: false,
							aniShow: "slide-in-right"
						}
					});
				})

				/*我的修改专家*/
				infobasic.addEventListener('tap', function() {
						if(oFlag1) {
							mui.openWindow({
								url: '../html/proinforupdate.html',
								id: 'html/proinforupdate.html',
								show: {
									autoShow: false,
									aniShow: "slide-in-left"
								},

							});
						} else if(!oFlag1 && oFlag == 1) {
							/*我的修改企业工作者*/
							mui.openWindow({
								url: '../html/researcher.html',
								id: 'html/researcher.html',
								show: {
									autoShow: false,
									aniShow: "slide-in-left"
								},

							});
						} else if(!oFlag1 && oFlag == 2) {
							/*我的修改企业工作者*/
							mui.openWindow({
								url: '../html/companyUpdata.html',
								id: 'html/companyUpdata.html',
								show: {
									autoShow: false,
									aniShow: "slide-in-left"
								},

							});
						} else if(!oFlag1 && oFlag == 3) {
							/*我的修改学生*/
							mui.openWindow({
								url: '../html/studentUpdata.html',
								id: 'html/studentUpdata.html',
								show: {
									autoShow: false,
									aniShow: "slide-in-left"
								},

							});
						}
					})
					/*我的历史和评价*/
				goZixun.addEventListener('tap', function() {
					if(!clickFlag) return;
					mui.openWindow({
						url: '../html/coophistory.html',
						id: 'html/coophistory.html',
						show: {
							autoShow: false,
							aniShow: "slide-in-left"
						},

					});
				})

			} else {
				loginNo.style.display = "block";
				loginYes.style.display = "none";
				mui("#loginNo").on("tap", "li", function() {
					goLoginFun();
				})
			}
		}

		function userInformation() {
			mui.ajax(baseUrl + "/ajax/professor/editBaseInfo/" + userId, {
				dataType: 'json', //数据格式类型
				type: 'GET', //http请求类型
				timeout: 10000, //超时设置
				//async: false,
				success: function(data) {
					var $info = data.data || {};
					oFlag = $info.authentication;
					oFlag1 = $info.authType
					console.log(oFlag)
					if(data.success && data.data) {
						document.getElementById("userName").innerText = $info.name || '';
						professorName = $info.name;
						var userTitle = document.getElementById("userTitle");
						var userPosition = document.getElementById("userPosition");
						var userDepartment = document.getElementById("userDepartment");
						var userMechanism = document.getElementById("userMechanism");
						var userCity = document.getElementById("userCity");
						var zixunOk = document.getElementById("zixunOk");
						if($info.title) {
							if($info.office) {
								userTitle.innerText = $info.title + "，"
							} else {
								userTitle.innerText = $info.title
							}

						}
						($info.office) ? userPosition.innerText = $info.office: userPosition.innerText = '';
						if($info.department) {
							if($info.orgName) {
								userDepartment.innerText = $info.department + "，"
							} else {
								if($info.address) {
									userDepartment.innerText = $info.department + " | "
								} else {
									userDepartment.innerText = $info.department;
								}
							}
						}
						if($info.orgName) {
							if($info.address) {
								userMechanism.innerText = $info.orgName + " | "
							} else {
								userMechanism.innerText = $info.orgName;
							}
						}
						($info.address) ? userCity.innerText = $info.address: userCity.innerText = '';
						($info.consultCount != '') ? zixunOk.innerText = $info.consultCount: zixunOk.innerText = '0';
						var startLeval = parseInt($info.starLevel);
						if($info.consultCount) {
							goZixunbox.style.display = "block";
							zixunOk.innerText = $info.consultCount;
							if(!startLeval) {
								clickFlag = false;
								document.getElementById("NoActive").classList.add("NoActive");
								document.getElementsByClassName("levelbox")[0].style.display = "none";
								document.getElementById("goZixun").classList.remove("mui-navigate-right");
							}
						} else {
							goZixunbox.style.display = "none";
						}
						if(!$info.authType && ($info.authentication == 2 || $info.authentication == 3)) {
							goZixunbox.style.display = "none";
						}
						var start = document.getElementsByClassName("star");
						for(var i = 0; i < startLeval; i++) {
							start[i].classList.add("icon-favorfill");
							start[i].classList.remove("icon-favor");
						}
						if($info.hasHeadImage == 1) {
							var mun = Math.round(Math.random() * 99 + 1);
							userImg.setAttribute("src", baseUrl + "/images/head/" + $info.id + "_l.jpg?" + mun);
						} else {
							userImg.setAttribute("src", baseUrl + "/images/default-photo.jpg");
						}
						if($info.authType) {
							nameli.classList.add('icon-vip');
							nameli.classList.add('authicon-cu');
							nameli.style.margin = "-4px 0 0 -2px";
						} else {
							if($info.authStatus) {
								if($info.authentication == 1) {
									nameli.classList.add('icon-renzheng');
									nameli.classList.add('authicon-mana');
								} else if($info.authentication == 2) {
									nameli.classList.add('icon-renzheng');
									nameli.classList.add('authicon-staff');
								} else {
									nameli.classList.add('icon-renzheng');
									nameli.classList.add('authicon-stu');
								}
							}
						}

					}
				},
				error: function() {
					plus.nativeUI.toast("服务器链接超时", toastStyle);
					return;
				}
			});
		}
		document.getElementById("goNewuser").addEventListener("tap", function() {
			mui.openWindow({
				url: '../html/invite_new.html',
				id: 'invite_new.html',
				show: {
					autoShow: false,
					aniShow: "slide-in-left"
				},
				extras: {
					proName: professorName
				}
			});
		})

	});

});
