/* 
* @Author: anchen
* @Date:   2017-07-06 14:48:06
* @Last Modified by: yubing
* @Last Modified time: 2017-07-07 16:30:31
*/
var contextPath="";
var visitPrefix="";
var currentPage=1;
var userId;			//用户ID
var virtualId;		//企业ID
var noticePower;	//接受通知开关
var pages=1;			//总的页数

$(document).ready(function(){
	contextPath=$("#contextPath").val();
	visitPrefix=$("#visitPrefix").val();
	userId=$("#userId").val();
	virtualId=$("#virtualId").val();
	noticePower=$("#noticePower").val();
	
	flag=true;
    //初始化接收通知状态
	if("1"==noticePower){
		initSwitchBox(true);
	}	
	else{
		initSwitchBox(false);
	}
	
	//分享处理
	var url=$("#webUrl").val()+"/wechat/appUser/execWechatAuth?developId="+$("#developId").val()+"&pageType=joinCompanyPage&manageUserId="+userId;
	var imageUrl=$("#webUrl").val()+"/images/visitor/log.png?v=1.0";
	var title=$("#userName").val()+"邀请您加入【"+$("#virtualName").val()+"】";
	shareAppMessage(title, "快来认证你所在的企业，超级前台专享福利享不停", url, imageUrl);
	
	function initSwitchBox(boxflag){
		var _this= $(".box");
		if(boxflag) {
			_this.parent().addClass("switchup");
			_this.parent().removeClass("switchoff");
			boxflag = !boxflag;
		} else {
			_this.parent().removeClass("switchup");
			_this.parent().addClass("switchoff");
			boxflag = !boxflag;
		}
		switchBox(boxflag);
	}
	
    /*
     * boxflag -- 布尔值  true 接收审核通知，false 不接收审核通知
     */
    function switchBox(boxflag){
        $(".box").click(function(){
            var _this = $(this);
            if(boxflag) {
                _this.parent().addClass("switchup");
                _this.parent().removeClass("switchoff");
                boxflag = !boxflag;
                noticePower="1";
            } else {
                _this.parent().removeClass("switchup");
                _this.parent().addClass("switchoff");
                boxflag = !boxflag;
                noticePower="0";
            }
            $.ajax({
        		url :contextPath+"/wechat/memberManage/updateNoticePower",
        		type : "POST",
        		data : {userId:userId,noticePower:noticePower},
        		success : function(data) {
        			if(noticePower=="0"){
        				showMessage("接收审核已关闭");
        			}
        			else{
        				showMessage("接收审核已开启");
        			}
        		},
        		error : function(data) {
        			// showMessage("系统繁忙，加载失败，请稍后再试.");
        		}
        	});
        })
    }

    function closeMask(){
        $(".examinemask").hide();
        flag = true;
    }
   
     //不通过 
    $(".outbtn").click(function(){
        closeMask();
    });
    
    //通过
    $(".passbtn").click(function(){
        closeMask();
        //$.showMessage("已通过", 2000);
    });
    
    // 查看大图
    $(".js_showimg").click(function(){
        var _this = $(this);
        var urlImg = _this.find("img")[0].src;
        if(urlImg.indexOf("noimg.png")==-1&&urlImg.indexOf("default_head1.png")==-1){
        	$(".showimg").find("img")[0].src = urlImg;
            $(".showimg").show();
        }
    });
    
    // 关闭查看大图
    $(".closeimg").click(function(){
        $(".showimg").hide();
    });
   
    // 点击空白处关闭选择公司弹窗
    $(".examinemask").click(function(e){
        if(!flag){
            var _con = $(".examinecon");   // 设置目标区域
            if(!_con.is(e.target) && _con.has(e.target).length === 0){
                closeMask();
            }
        }
    });
    
    //初始化第一页内容
//	query();
	
	$(window).scroll(function(){
		var scrollTop = $(this).scrollTop();
		var scrollHeight = $(document).height();
		var windowHeight = $(this).height();
		if(scrollTop + windowHeight == scrollHeight){
			if(currentPage<=pages){
//				query();
			}
			/*else{
				showMessage("已达到最后一页，暂无数据");
			}*/
	　　	}
	});
	
});

/**
 * 加载企业人员列表信息
 */
var haveLine=false;
var fengeStr="";
//function query(){
//	$.ajax({
//		url :contextPath+"/wechat/memberManage/getMemberList",
//		type : "POST",
//		data : {userId:userId,virtualId:virtualId,pageNo:currentPage},
//		dataType:"json",
//		success : function(data) {
//			var ulcontainer=$('#ulcontainer');
//			var totalCount=data.totalCount;
//			var items=data.items;
//			pages=data.pages;
//			if(currentPage==1){
//				ulcontainer.html("");
//				haveLine=false;
//				fengeStr="";
//			}
//			if(items.length>0){
//				currentPage=currentPage+1;
//			}
//			var strHTML="";
//			var fengeStr="";
//			$.each(items,function(index,item){
//				var uid=item.id;
//				var userName=item.userName;
//				var userMobile=item.userMobile;
//				var userPhotoUrl=item.headimgUrl;
//				var status=item.status;
//				var roleType=item.roleType;
//				var imageUrl=item.imageUrl;		//上传的头像信息
//				var imageUrl01=item.imageUrl01;
//				var imageUrl02=item.imageUrl02;
//				var imageUrl03=item.imageUrl03;
//				
//				var touxiang="";
//				if(userPhotoUrl==""||userPhotoUrl==null){
//					userPhotoUrl=contextPath+"/images/common/default_head1.png";
//				}
//				
//				if(imageUrl==""||imageUrl==null){
//					touxiang=userPhotoUrl;
//				}
//				else{
//					touxiang=visitPrefix+imageUrl;
//				}
//				
//				if(status=="0"){
//					strHTML+="<li class='info-item clearfix' onclick='showManageMember(\""+uid+"\",\""+userName+"\",\""+userMobile+"\",\""+status+"\",\""+touxiang+"\",\""+imageUrl01+"\",\""+imageUrl02+"\",\""+imageUrl03+"\",\""+roleType+"\")'>"
//						   +"<img src='"+userPhotoUrl+"' alt='' class='headimg'/>"
//						   +"<div class='infoname'> <span>"+userName+"</span><em class='pending'>待审核</em></div>"
//						   +"<span class='infotel'>"+userMobile+"</span>"
//						   +"<span class='arrowicon'><i></i></span></li>";
//				}
//				else{
//					var emHtml="<em class=''>管理员</em>";
//					if(roleType!="1"){
//						emHtml="<em class='staff'>员工</em>";
//					}
//					if(!haveLine&&fengeStr==""){
//						haveLine=true;
//						fengeStr="<li class='line'></li>";
//						strHTML+=fengeStr+"<li class='info-item clearfix' onclick='showManageMember(\""+uid+"\",\""+userName+"\",\""+userMobile+"\",\""+status+"\",\""+touxiang+"\",\""+imageUrl01+"\",\""+imageUrl02+"\",\""+imageUrl03+"\",\""+roleType+"\")'>"
//							   +"<img src='"+userPhotoUrl+"' alt='' class='headimg'/>"
//							   +"<div class='infoname'> <span>"+userName+"</span>"+emHtml+"</div>"
//							   +"<span class='infotel'>"+userMobile+"</span>"
//							   +"<span class='arrowicon'><i></i></span></li>";
//					}
//					else{
//						strHTML+="<li class='info-item clearfix' onclick='showManageMember(\""+uid+"\",\""+userName+"\",\""+userMobile+"\",\""+status+"\",\""+touxiang+"\",\""+imageUrl01+"\",\""+imageUrl02+"\",\""+imageUrl03+"\",\""+roleType+"\")'>"
//							   +"<img src='"+userPhotoUrl+"' alt='' class='headimg'/>"
//						       +"<div class='infoname'> <span>"+userName+"</span>"+emHtml+"</div>"
//						       +"<span class='infotel'>"+userMobile+"</span>"
//						       +"<span class='arrowicon'><i></i></span></li>";
//					}
//				}
//			});
//			ulcontainer.append(strHTML);
//			$(".bottomtext").html("共"+totalCount+"位同事")
//		},
//		error : function(data) {
//			showMessage("系统繁忙，加载失败，请稍后再试.");
//		}
//	});
//}
function testasd(idcode){
	console.log(idcode);
	query(idcode)
	let flag = true;
	if(flag){
		listloading.style.display="block";
		showicon.style.display="block";
		hiddenicon.style.display="none";
	 flag=false;
	}
	else{
		listloading.style.display="none";
		showicon.style.display="none";
		hiddenicon.style.display="block";
	 flag=true;
	}
}
function query(virtIdCode){
	let vid = virtIdCode
	$.ajax({
		url :contextPath+"/wechat/memberManage/getAppUserByVirId",
		type : "POST",
		data : {virtId:vid},
		dataType:"json",
		success : function(data) {
			console.log(data)
			if(data.code == 200){
				console.log("asdf"+data.msg)
				var ulcontainer=$(".company-id-"+ vid +" #ulcontainer");
				//var totalCount=data.totalCount;
				var totalCount = data.data.length
				var items=data.data;
//				pages=data.pages;
//				if(currentPage==1){
//					ulcontainer.html("");
//					haveLine=false;
//					fengeStr="";
//				}
//				if(items.length>0){
//					currentPage=currentPage+1;
//				}
				var strHTML="";
				var fengeStr="";
				$.each(items,function(index,item){
					var uid=item.id;
					var userName=item.userName;
//					var userMobile=item.userMobile;
					var userMobile=item.telephone;
//					var userPhotoUrl=item.headimgUrl;
					var userPhotoUrl=item.photo;
					var status=item.status;
					var roleType=item.isManager;
					var remark = item.remark
//					var imageUrl=item.imageUrl;		//上传的头像信息
//					var imageUrl01=item.imageUrl01;
//					var imageUrl02=item.imageUrl02;
//					var imageUrl03=item.imageUrl03;
					
					var touxiang="";
					if(userPhotoUrl==""||userPhotoUrl==null){
						userPhotoUrl=contextPath+"/images/common/default_head1.png";
					}
					
//					if(imageUrl==""||imageUrl==null){
//						touxiang=userPhotoUrl;
//					}
//					else{
//						touxiang=visitPrefix+imageUrl;
//					}
					
					if(status=="0"){
						strHTML+="<li class='info-item clearfix' onclick='showManageMember(\""+uid+"\",\""+userName+"\",\""+userMobile+"\",\""+status+"\",\""+userPhotoUrl+"\",\""+roleType+"\")'>"
							   +"<img src='"+userPhotoUrl+"' alt='' class='headimg'/>"
							   +"<div class='infoname'> <span>"+userName+"</span><em class='pending'>待审核</em></div>"
							   +"<span class='infotel'>"+userMobile+"</span>"
							   +"<span class='arrowicon'><i></i></span></li>";
					}
					else{
						var emHtml="<em class=''>管理员</em>";
						if(roleType!="1"){
							emHtml="<em class='staff'>员工</em>";
						}
						if(!haveLine&&fengeStr==""){
							haveLine=true;
							fengeStr="<li class='line'></li>";
							strHTML+=fengeStr+"<li class='info-item clearfix' onclick='showManageMember(\""+uid+"\",\""+userName+"\",\""+userMobile+"\",\""+status+"\",\""+touxiang+"\",\""+roleType+"\")'>"
								   +"<img src='"+userPhotoUrl+"' alt='' class='headimg'/>"
								   +"<div class='infoname'> <span>"+userName+"</span>"+emHtml+"</div>"
								   +"<span class='infotel'>"+userMobile+"</span>"
								   +"<span class='arrowicon'><i></i></span></li>";
						}
						else{
							strHTML+="<li class='info-item clearfix' onclick='showManageMember(\""+uid+"\",\""+userName+"\",\""+userMobile+"\",\""+status+"\",\""+touxiang+"\",\""+roleType+"\")'>"
								   +"<img src='"+userPhotoUrl+"' alt='' class='headimg'/>"
							       +"<div class='infoname'> <span>"+userName+"</span>"+emHtml+"</div>"
							       +"<span class='infotel'>"+userMobile+"</span>"
							       +"<span class='arrowicon'><i></i></span></li>";
						}
					}
				});
				ulcontainer.html(strHTML);
//				ulcontainer.append(strHTML);
				$(".company-id-"+ vid +" .bottomtext").html("共"+totalCount+"位同事")
			}else{
				console.log(msg)
			}
			
		},
		error : function(data) {
			// showMessage("系统繁忙，加载失败，请稍后再试.");
		}
	});
}

//显示员工操作面板
function showManageMember(currentUserId,userName,userMobile,status,userPhotoUrl,imageUrl01,imageUrl02,imageUrl03,roleType,remark){
	if(imageUrl01==null||imageUrl01==""||imageUrl01=="null"){
		imageUrl01=contextPath+"/images/userlogin/noimg.png";
		
	}
	else{
		imageUrl01=visitPrefix+imageUrl01;
	}
	
	if(imageUrl02==null||imageUrl02==""||imageUrl02=="null"){
		imageUrl02=contextPath+"/images/userlogin/noimg.png";
	}
	else{
		imageUrl02=visitPrefix+imageUrl02;
	}
	
	if(imageUrl03==null||imageUrl03==""||imageUrl03=="null"){
		imageUrl03=contextPath+"/images/userlogin/noimg.png";
	}
	else{
		imageUrl03=visitPrefix+imageUrl03;
	}

	$("#currentUserId").val(currentUserId);

	$(".personalinfo .personal-name-box").html(userName);

	$(".personalinfo .personal-phone-box").html(userMobile);
	$(".personalinfo .personal-remark-box").html(remark);

	$("#userPhotoUrl").attr("src",userPhotoUrl);

	$("#imageUrl01").attr("src",imageUrl01);

	$("#imageUrl02").attr("src",imageUrl02);

	$("#imageUrl03").attr("src",imageUrl03);

	if(flag) {
        flag = false;
        $(".examinemask").show();
        
        if(roleType=="1"){
        	$("#i_ckstatus").hide();
        	$("#i_ckRole").hide();
        }
        else{
			if("1"==status){
				$("#i_ckRole").show();
				$("#i_ckstatus").hide();
			}
			else{
			 	$("#i_ckstatus").show();
			 	$("#i_ckRole").hide();
			}
        }
    }
}

//管理员审核用户
function reviewAppUser(status){
	var acceptUserId=$("#currentUserId").val();
	$.ajax({
		url :contextPath+"/wechat/memberManage/reviewAppUser",
		type : "POST",
		data : {userId:userId,acceptUserId:acceptUserId,status:status},
		dataType:"json",
		success : function(data) {
			showMessage(data.resultDesc);
			if("success"==data.flag){
				currentPage=1;
				query();
			}
		},
		error : function(data) {
			// showMessage("系统繁忙，加载失败，请稍后再试.");
		}
	});
}

//设置管理员
function setRoleAppUser(){
	var acceptUserId=$("#currentUserId").val();
	$.ajax({
		url :contextPath+"/wechat/memberManage/setRoleAppUser",
		type : "POST",
		data : {userId:userId,acceptUserId:acceptUserId,status:status},
		dataType:"json",
		success : function(data) {
			showMessage(data.resultDesc);
			if("success"==data.flag){
				currentPage=1;
				query();
			}
		},
		error : function(data) {
			// showMessage("系统繁忙，加载失败，请稍后再试.");
		}
	});
}