/**
 * 企业账号登录
 * created by liufang 2018/8/17
 */
$(function () {
	var ERROR_OK = 200;
	var contextPath = $("#contextPath").val();
	var account = {
		init: function () {
			this.render();
			this.bindEvent();
		},
		render: function () {

		},
		bindEvent: function () {

			var _this = this;
			var openId = this.GetQueryString("openId");
			var organizationId = sessionStorage.getItem("orgId");
			// if(organizationId==""||organizationId==null){
			// organizationId=sessionStorage.getItem("orgId");
			// }
			var userStatus = this.GetQueryString("userStatus");
			var register = this.GetQueryString("register");
			var developId = this.GetQueryString("developId");
			var userId = this.GetQueryString("userId");
			var compName = this.GetQueryString("compName");
			var accountId = this.GetQueryString("accountId");
			var moduleCode = this.GetQueryString("moduleCode");
			var oldUrl = this.GetQueryString("oldUrl");
			var type = this.GetQueryString("type");
			var reUrl = this.GetQueryString("reUrl");
			var serviceCode = this.GetQueryString("serviceCode");
			console.log(moduleCode, 99999999999);
			console.log(oldUrl, 11111);
			//企业账号说明
			$(".account-explain").on("click",function(){
				window.location.href = contextPath + "/wechat/appUser/accountExplain";
			})
			//登录
			$(".login-btn").on("click", function () {
				_this.ajaxLogin(reUrl,type,openId, organizationId, developId, userId, compName, accountId, userStatus, register,serviceCode)
			})
			if(oldUrl.indexOf("emig")!=-1 || oldUrl.indexOf("release")!=-1){
				console.log(oldUrl, 22222);
				$('.explain').addClass('active')
				if(oldUrl.indexOf("emig")!=-1){
					$(".expain_txt").text("企业迁出");
				}
			}else{
				$('.explain').removeClass('active')
			}
			// 跳转说明页面
			$("#goExplain").on("click", function () {
				// console.log($("#contextPath").val());
				if (oldUrl.indexOf("emig")!=-1) { //迁出
					// http://wx.palmnest.com/avicfile/super_wechat/emig/index.html?#/explain
					window.location.href = $("#wechatUrl").val() + "/emig/index.html?#/explain";

				} else if (oldUrl.indexOf("release")!=-1) { //电子放行条
					//$(".expain_txt").text("电子放行条");
					// http://wx.palmnest.com/avicfile/super_wechat/release/index.html?#/ReleaseUsing
					window.location.href = $("#wechatUrl").val() + "/release/index.html?#/ReleaseUsing";

				}
			})
		},
		/**
		 * 获取url的参数
		 * @param name 参数名
		 */
		GetQueryString: function (name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]);
			return null;
		},
		/**
		 * 登录
		 * @param openId
		 * @param organizationId
		 * @param developId
		 * @param userId
		 * @param compName 公司名称
		 */
		ajaxLogin: function (reUrl,type,openId, organizationId, developId, userId, compName, accountId, userStatus, register,serviceCode) {
			var _this = this;
			// var oldUrl=sessionStorage.getItem("oldUrl");
			// console.log(oldUrl);
			//登录账号
			var account = $(".account-user").val();
			
			var oldUrl = this.GetQueryString("oldUrl");
			if (!account) {
				layer.open({
					content: '请输入登录账号',
					skin: 'msg',
					time: 2 //2秒后自动关闭
				});
				return false;
			};
			//密码
			var password = $(".account-psd").val();
			if (!password) {
				layer.open({
					content: '请输入密码',
					skin: 'msg',
					time: 2 //2秒后自动关闭
				});
				return false;
			};
			$.ajax({
				url: contextPath + "/wechat/appUser/accountLogin",
				type: "POST",
				dataType: "json",
				data: {
					openId: openId,
					account: account,
					password: password,
					orgId: organizationId
				},
				success: function (data) {
					if (data.code == ERROR_OK) {
						var result = data.data;
						sessionStorage.setItem("accId", result.accId);
						sessionStorage.setItem("account", result.account);
						sessionStorage.setItem("virId", result.virId);
						sessionStorage.setItem("virName", result.virName);
						sessionStorage.setItem('firstEnter',1);//标志首次进入服务，为1代表首次进入
						_this.checkedPerm(result.accId,serviceCode,function() {
							if(type==1 && !!reUrl){//会议室
		  					window.location.href =reUrl+"#/boardroom";//如果是会议室就拼这个，跳到对应预约的界面
                //window.location.href =$("#contextPath").val()+"/wechat/neigou/meetingDo?openId="+openId+"&organizationId="+organizationId+"&developId="+developId+"&userId="+userId;
							}else if(type==2 && !!reUrl){//空调加时
								window.location.href =reUrl;
							}else if (!!oldUrl) {
								window.location.href = oldUrl;
							} else {
								window.location.href = $("#contextPath").val() + "/wechat/appUser/newmine?openId=" + openId + "&developId=" + developId + "&userStatus=" + userStatus + "&userId=" + userId + "&register=" + register + "&organizationId=" + organizationId;
							}
						})
						//			  				window.location.href=$("#contextPath").val()+"/wechat/release/mine?openId="+openId+"&organizationId="+organizationId+"&developId="+developId+"&userId="+userId+"&compName="+compName+"&accountId="+accountId+"&virAccount="+result.virAccount;
					} else {
						layer.open({
							content: data.msg,
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
						//			  				alert(data.msg)
					}
				},
				error: function (data) {
					// alert("系统繁忙..");
				}
			});
		},
		/**
		 校验用户权限
		*/

		checkedPerm(id,serviceCode,callback) {
            if (id){
                id = ""
            }
			$.ajax({
				url: $("#contextPath").val() + "/wechat/index/checkedHasFuncPerm",
				type: "POST",
				dataType: "json",
				data: {
					id: id,
					serviceCode: sessionStorage.getItem('serviceCode'),
					accountType: 0
				},
				success: function (data) {
					if (data.code == "200") {
						// alert(data.data)
						if(data.data){
							callback()
						}else{
							layer.open({
								content: '抱歉，你暂无该功能的使用权限，请联系企业管理员或客服人员！',
								skin: 'msg',
								time: 2
							});
						}
					} else {
						alert(data.msg)
					}
				},
				error: function (data) {
					alert("系统繁忙,菜单加载失败..");
				}
			});
		}
	}
	account.init();
})