/**
 * 服务列表
 *
 */

$(function () {
	var ERROR_OK = 200;
	var openId = $("#openId").val();
	var developId = $("#developId").val();
	var contextPath = $("#contextPath").val();
	var userStatus = $("#userStatus").val();
	var register = $("#register").val();
	var userId = $("#userId").val();
	var organId = $("#organId").val();
	var tpl = {

		serviceTpl: '\
			<% for(var i=0;i<data.length;i++){%>\
				<div class="service-list-con">\
				 <div class=service-list-name><span class="icon"></span><%=data[i].categoryName%></div>	\
				 <div class="service-list_s">\
					<% for(var j=0;j<data[i].serviceList.length;j++){%>\
						<% if(data[i].serviceList[j].showGray == 1){%>\
		                    <div class="service-item gray" data-isgray= "<%=data[i].serviceList[j].showGray%>"  data-grayurl= "<%=data[i].serviceList[j].grayUrl%>"  data-registerstatus = "<%=data[i].serviceList[j].registerStatus%>" data-skipaddress="<%=data[i].serviceList[j].skipAddress%>" data-servicecode="<%=data[i].serviceList[j].serviceCode%>" data-jumpaddress="<%=data[i].serviceList[j].jumpAddress%>" data-issigncontract = "<%=data[i].serviceList[j].isSignContract%>" data-serviceopeningstatus="<%=data[i].serviceList[j].serviceOpeningStatus%>">\
		                        <span class="icon-wp"><span class="icon" style="background-image: url(<%=data[i].serviceList[j].grayWechatServiceIcon%>)"></span></span>\
		                        <div class="name"><%=data[i].serviceList[j].serviceName%></div>\
		                    </div>\
		                <%}else{%>\
							<div class="service-item" data-isgray= "<%=data[i].serviceList[j].showGray%>"  data-registerstatus = "<%=data[i].serviceList[j].registerStatus%>" data-skipaddress="<%=data[i].serviceList[j].skipAddress%>" data-servicecode="<%=data[i].serviceList[j].serviceCode%>" data-jumpaddress="<%=data[i].serviceList[j].jumpAddress%>" data-issigncontract = "<%=data[i].serviceList[j].isSignContract%>" data-serviceopeningstatus="<%=data[i].serviceList[j].serviceOpeningStatus%>">\
					            <span class="icon-wp"><span class="icon" style="background-image: url(<%=data[i].serviceList[j].serviceIcon%>)"></span></span>\
					            <div class="name"><%=data[i].serviceList[j].serviceName%></div>\
					        </div>\
		                <%}%>    \
				  <%}%>\
                  </div>      \
				</div>\
                <%}%>',
                secondServiceTpl: '\
        			<% for(var i=0;i<data.length;i++){%>\
        				<div class="service-list-con">\
        				 <div class=service-list-name><span class="icon"></span><span class="name"><%=data[i].categoryName%></span><span class="line"></span></div>	\
        				 <div class="service-list_s">\
        					<% for(var j=0;j<data[i].serviceList.length;j++){%>\
        						<% if(data[i].serviceList[j].showGray == 1){%>\
        		                    <div class="service-item gray" data-isgray= "<%=data[i].serviceList[j].showGray%>"  data-grayurl= "<%=data[i].serviceList[j].grayUrl%>"  data-registerstatus = "<%=data[i].serviceList[j].registerStatus%>" data-skipaddress="<%=data[i].serviceList[j].skipAddress%>" data-servicecode="<%=data[i].serviceList[j].serviceCode%>" data-jumpaddress="<%=data[i].serviceList[j].jumpAddress%>" data-issigncontract = "<%=data[i].serviceList[j].isSignContract%>" data-serviceopeningstatus="<%=data[i].serviceList[j].serviceOpeningStatus%>">\
        		                        <span class="icon-wp"><span class="icon" style="background-image: url(<%=data[i].serviceList[j].grayWechatServiceIcon%>)"></span></span>\
        		                        <div class="name"><%=data[i].serviceList[j].serviceName%></div>\
        		                    </div>\
        		                <%}else{%>\
        							<div class="service-item" data-isgray= "<%=data[i].serviceList[j].showGray%>"  data-registerstatus = "<%=data[i].serviceList[j].registerStatus%>" data-skipaddress="<%=data[i].serviceList[j].skipAddress%>" data-servicecode="<%=data[i].serviceList[j].serviceCode%>" data-jumpaddress="<%=data[i].serviceList[j].jumpAddress%>" data-issigncontract = "<%=data[i].serviceList[j].isSignContract%>" data-serviceopeningstatus="<%=data[i].serviceList[j].serviceOpeningStatus%>">\
        					            <span class="icon-wp"><span class="icon" style="background-image: url(<%=data[i].serviceList[j].serviceIcon%>)"></span></span>\
        					            <div class="name"><%=data[i].serviceList[j].serviceName%></div>\
        					        </div>\
        		                <%}%>    \
        				  <%}%>\
                          </div>      \
        				</div>\
                        <%}%>',     
	}
	var Index = {
		init: function () {
			this.render();
			this.bindEvent();
		},
		render: function () {

			//获取企业服务列表
			this.getServiceList();
			//判断是否显示商圈/发现
			this.judgeShow();
			if(organId == 663){
				$(".wx-index-top .logo-icon").addClass("fude-logo-home")
			}else if(organId == 691){
        $(".wx-index-top .logo-icon").addClass("suoke-logo-home")
      }else{
        $(".wx-index-top .logo-icon").removeClass("fude-logo-home")
        $(".wx-index-top .logo-icon").removeClass("suoke-logo-home")
			}
		},
		bindEvent: function () {
			$(".wx-index-top .phone-icon").attr("href","tel:"+sessionStorage.getItem("telephone"))
			//phone-icon
			sessionStorage.setItem("serviceUrl",window.location.href);
			//tel:10086
			$(".wx-org-name").html(localStorage.getItem(developId+"orgName"))
			//跳转首页
			$(".nav-btn.home").on("click", function () {
				// window.location.href = $("#contextPath").val() + "/wechat/index/homePageView?openId=" + openId + "&developId=" + developId + "&organizationId=" + organId + "&userId=" + userId;
				window.location.href = $("#contextPath").val() + "/wechat/index/homePageView?openId=" + openId + "&developId=" + developId + "&organizationId=" + organId;
			});
			// 跳转商圈
			$(".nav-btn.trade_area").on("click", function () {
//				var findUrl = sessionStorage.getItem('findUrl')
//				window.location.href = findUrl ;
				window.location.href = $("#contextPath").val() + "/wechat/index/tradeArea?openId=" + openId + "&developId=" + developId + "&userStatus=" + userStatus + "&userId=" + $("#userId").val() + "&register=" + register + "&organId=" + organId;
			});
		},
		getServiceList: function () {
			$.ajax({
				url: contextPath + "/wechat/index/serviceListCategory",
				type: "POST",
				dataType: "json",
				data: {
					openId: openId,
					developId: developId,
					userStatus: userStatus
				},
				success: function (data) {
					if (data.code == ERROR_OK) {
						//灰度布尔值转换成0和1
						for(var i=0;i<data.data.length;i++){
							for(var j=0;j<data.data[i].serviceList.length;j++){
								if(data.data[i].serviceList[j].isGray){
									data.data[i].serviceList[j].showGray = 1
								}else{
									data.data[i].serviceList[j].showGray = 0
								}
							}
						}
						$(".service-list-wrapper").html(template.render(tpl.secondServiceTpl)(data));
						$(".service-list-wrapper .service-item").on("click", function () {
							var registerStatus = $(this).data("registerstatus");
							var skipAddress = $(this).data("skipaddress");
							var serviceCode = $(this).data("servicecode");
							var jumpAddress = $(this).data("jumpaddress");
							var isSignContract = $(this).data("issigncontract");
							var serviceOpeningStatus = $(this).data("serviceopeningstatus");
							var isgray = $(this).data("isgray");
							sessionStorage.setItem('firstEnter',1);//标志首次进入服务，为1代表首次进入
							//${obj.registerStatus}','${obj.skipAddress }','${obj.serviceCode }','${obj.jumpAddress }','${obj.isSignContract }','${obj.serviceOpeningStatus }
							//跳转各个模块
							if(isgray !=1){
								checkedPerm(serviceCode,function() {
									// console.log(userStatus, registerStatus, skipAddress, serviceCode, jumpAddress, isSignContract, serviceOpeningStatus)
									searchUrl(userStatus, registerStatus, skipAddress, serviceCode, jumpAddress, isSignContract, serviceOpeningStatus);
								})
							}else{
								window.location.href = $(this).data("grayurl");
							}
							
						})
					} else {
						alert(data.msg)
					}
				},
				error: function (data) {
//					alert("系统繁忙,菜单加载失败..");
				}
			});
		},
		judgeShow: function () {
			var isEnabled = sessionStorage.getItem("isEnabled");
			var name = sessionStorage.getItem("name");
			if(isEnabled == "true"){
				$(".trade_area").show();
				$(".trade_area .name").html(name);
			}	
		},
	}
	Index.init();
});
/**
 * 跳转服务链接
 * @param userStatus
 * @param status
 * @param url
 * @param serviceCode
 * @param jumpAddress
 * @param isSignContract
 * @param serviceOpeningStatus
 */
function searchUrl(userStatus, status, url, serviceCode, jumpAddress, isSignContract, serviceOpeningStatus) {
	var register = $("#register").val();
	var openId = $("#openId").val();
	var developId = $("#developId").val();
	var userId = $("#userId").val();
	var serviceUrl = "";
	sessionStorage.setItem("oldUrl", url);
	var urls = "";
	if (userStatus == 1) {
		urls = jumpAddress;
		console.log("1:" + urls);
		if (isSignContract == 1 && serviceOpeningStatus == 0) {
			layer.open({
				title: '温馨提示',
				style: 'font-size: 18px;',
				content: '您的企业暂未开通此服务,立即了解服务详情。',
				btn: ['确定', '取消'],
				yes: function (index) {
					layer.close(index);
					urls = url;
					var urlStr = urls.split("?");
					if (urlStr.length > 1) {
						serviceUrl = urls + "&openId=" + openId + "&developId=" + developId + "&serviceCode=" + serviceCode + "&userId=" + userId + "&resStatus=" + status;
					} else {
						serviceUrl = urls + "?openId=" + openId + "&developId=" + developId + "&serviceCode=" + serviceCode + "&userId=" + userId + "&resStatus=" + status;
					}
					console.log("2:" + serviceUrl);
					location.href = serviceUrl;
				},
				no: function (index) {
					layer.close(index);
				}
			});
		} else {
			var urlStr = urls.split("?");
			if (urlStr.length > 1) {
				serviceUrl = urls + "&openId=" + openId + "&developId=" + developId + "&serviceCode=" + serviceCode + "&userId=" + userId + "&resStatus=" + status;
			} else {
				serviceUrl = urls + "?openId=" + openId + "&developId=" + developId + "&serviceCode=" + serviceCode + "&userId=" + userId + "&resStatus=" + status;
			}
			console.log("3:" + serviceUrl);
			location.href = serviceUrl;

		}
	} else {
		urls = url;
		var urlStr = urls.split("?");
		if (urlStr.length > 1) {
			serviceUrl = urls + "&openId=" + openId + "&developId=" + developId + "&serviceCode=" + serviceCode + "&userId=" + userId + "&resStatus=" + status;
		} else {
			serviceUrl = urls + "?openId=" + openId + "&developId=" + developId + "&serviceCode=" + serviceCode + "&userId=" + userId + "&resStatus=" + status;
		}
		if (status == 1) {
			if (register == 1) {
				console.log("4:" + serviceUrl);
				location.href = serviceUrl;
			} else {
				console.log("5:" + urls + "&resStatus=" + status);
				doRegister(urls + "&resStatus=" + status);
			}
		} else {
			serviceUrl = serviceUrl + "&resStatus=" + status;
			console.log("6:" + serviceUrl);
			location.href = serviceUrl;
		}

	}
}
/**
 链接跳转地址
 */

function getUserInfo(orgId) {
	var openId = $("#openId").val();
	var developId = $("#developId").val();
	var userId = $("#userId").val();
	var register = $("#register").val();
	var userStatus = $("#userStatus").val();
	var organId = $("#organId").val();
	// if(orgId==""||orgId==null){
	// 	orgId=sessionStorage.getItem("orgId");
	// }
	//    if(userId!=""){
	//        location.href=$("#contextPath").val()+"/wechat/appUser/userInfo?openId="+openId+"&developId="+developId+"&userId="+userId+"&organizationId="+orgId+"&register="+register;
	//    }else{
	//        window.location.href=$("#contextPath").val()+"/wechat/appUser/bindphone?openId="+openId+"&developId="+developId+"&register="+register;
	//    }
	$.ajax({
		url: $("#contextPath").val() + "/wechat/appUser/getUserInfo",
		type: "POST",
		dataType: "json",
		data: {
			openId: openId,
			organizationId: orgId
		},
		success: function (data) {
			if (data.code == "200") {
				var dataObj = data.data;
				console.log(dataObj);
				if (dataObj.isLogin == 2) {
					window.location.href = $("#contextPath").val() + "/wechat/appUser/newmine?openId=" + openId + "&developId=" + developId + "&userStatus=" + userStatus + "&userId=" + userId + "&register=" + register + "&organizationId=" + organId;
				} else {
					// if(dataObj.accountType == 1){
						window.location.href = $("#contextPath").val() + "/wechat/appUser/newmine?openId=" + openId + "&developId=" + developId + "&userStatus=" + userStatus + "&userId=" + userId + "&register=" + register + "&organizationId=" + organId;
					// }
					// else{
					// 	location.href = $("#contextPath").val() + "/wechat/appUser/virAccount?openId=" + openId + "&developId=" + developId + "&userStatus=" + userStatus + "&userId=" + userId + "&register=" + register + "&organizationId=" + organId;
					// }
				}
			} else {
				alert(data.msg)
			}
		},
		error: function (data) {
//			alert("系统繁忙,菜单加载失败..");
		}
	});
	//	if(userId!=""){
	//		location.href=$("#contextPath").val()+"/wechat/release/mine?openId="+openId+"&developId="+developId+"&userId="+userId+"&organizationId="+orgId;
	//	}else{
	//		window.location.href=$("#contextPath").val()+"/wechat/appUser/bindphone?openId="+openId+"&developId="+developId;
	//	}
}
/**
 校验用户权限
 */

function checkedPerm(serviceCode,callback) {
	// var userId = $("#userId").val();
	var accountType = 1
	if(sessionStorage.getItem("accountType")) {
		accountType = sessionStorage.getItem("accountType")
	}
	var id = ""
	if(accountType == 1) {
		id = sessionStorage.getItem("userId")
	}else{
		id = sessionStorage.getItem("accId")
	}
    if (id){
        id = ""
    }
	$.ajax({
		url: $("#contextPath").val() + "/wechat/index/checkedHasFuncPerm",
		type: "POST",
		dataType: "json",
		data: {
			id: id,
			serviceCode: serviceCode,
			accountType: accountType
		},
		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("系统繁忙,菜单加载失败..");
		}
	});
}
//进入注册界面
function doRegister(serviceUrl) {
	window.location.href = serviceUrl;
}