$(function() {
	loginStatus();//判断个人是否登录
	var userid = $.cookie("userid");
	var num = 1;
	var keyt,subject,industry,address;
	var authType = 1;
	var searchContent = $.trim(GetQueryString("searchContent"));
	var subjectText = $.trim(GetQueryString("subject"));
	if(subjectText==""){
		subject="";
	}else{
		subject=subjectText;
	}
	if(searchContent==""){
		keyt="";
	}else{
		keyt=searchContent;
		$(".searchsome").val(searchContent);
	}
	expertListVal(keyt,subject,industry,address,authType,20,1,true);
	articalListVal(20, 1,true);
	resourceListVal(20, 1,true);
	academicFieldVal(subjectText);
	applicationIndustryVal();
	cityVal();
	//切换搜索范围
	jquerytab("col-w-12");
	//筛选条件的选择
	$(".filterUl").on("click","li",function(){
		this.parentNode.querySelector('li.filterCurrent').classList.remove("filterCurrent");
		this.classList.add("filterCurrent");
	})
	//筛选条件的展开关闭
	$(".filterListNew").on("click",".rightbtn",function(){
		if(this.querySelector("em").className == "unfoldtr") {
			this.querySelector("em").classList.remove("unfoldtr");
			this.querySelector("em").classList.add("foldtr");
			this.parentNode.querySelector(".filterUl").classList.remove("filterUlactive");
		} else {
			this.querySelector("em").classList.add("unfoldtr");
			this.querySelector("em").classList.remove("foldtr");
			this.parentNode.querySelector(".filterUl").classList.add("filterUlactive");
		}
	})
	
	$(".searchblock").hide();
	$(document).scroll(function() {
		var top = $(document).scrollTop();
		if (top >= 80) {
			$(".searchblock").stop(true,false).fadeIn();
		}else{
			$(".searchblock").stop(true,false).fadeOut();
		}
	})
	
	$(".searchsome").keydown(function(e) {
		if(e.which == 13) {
			num = $(".liactive").attr("data-id");
			if(num==1){
				expertBox();
			}else if(num==2){
				resourceListVal(20, 1,true);
			}else if(num==3){
				articalListVal(20, 1,true);
			}
		}
	})
	
	$(".searchgo").on("click",function(e) {
		num = $(".liactive").attr("data-id");
		if(num==1){
			expertBox();
		}else if(num==2){
			resourceListVal(20, 1,true);
		}else if(num==3){
			articalListVal(20, 1,true);
		}
	})
	
	$("#academicField,#applicationIndustry,#cityList").on("click","li",function(e) {
		expertBox();
	})
	
	
	function expertBox(){
		keyt = $(".searchsome").val();
		var  subjectVal= $("#academicField li.filterCurrent").text();
		if(subjectVal=="不限"){
			subject = "";
		}else{
			subject = subjectVal;
		}
		var  industryVal= $("#applicationIndustry li.filterCurrent").text();
		if(industryVal=="不限"){
			industry = "";
		}else{
			industry = industryVal;
		}
		var  addressVal= $("#cityList li.filterCurrent").text();
		if(addressVal=="不限"){
			address = "";
		}else{
			address = addressVal;
		}
		expertListVal(keyt,subject,industry,address,authType,20,1,true);
	}
		
});



function jquerytab(name){
   var oDome=$('.' + name);
   var oSpan=oDome.find('.filterdiv li');
   var oP=oDome.find('.listdiv .listbox');
   oSpan.click(function(){
		$(this).addClass('liactive').siblings().removeClass('liactive');
		var index =oSpan.index(this);
		oP.eq(index).show().siblings().hide();
		//$(".searchsome").val("");
		if(index==0){
			//expertListVal(keyt,subject,industry,address,authType,20,1,true);
			$(".searchsome").attr("placeholder","请输入专家姓名、机构、研究方向");
		}else if(index==1){
			//resourceListVal(20, 1,true);
			$(".searchsome").attr("placeholder","请输入资源名称、用途、机构");
		}else{
			//articalListVal(20, 1,true);
			$(".searchsome").attr("placeholder","请输入文章标题、作者");
		}
   });
}

//学术领域
function academicFieldVal(subjectText){
	$.ajax({
		"url": "/ajax/dataDict/qaDictCode",
		"type": "get",
		"async": false,
		"data": {
			"dictCode":"SUBJECT",
		},
		"success": function(data) {
			console.log(data);
			if(data.success) {
				$("#academicField").html("");
				$("#academicField").append('<li class="filterCurrent">不限</li>');
				for(var i = 0; i < data.data.length; i++) {
					var itemlist = '<li class="ititle">'+data.data[i].caption+'</li>';
					$("#academicField").append(itemlist);
					if(i > 20)
						break;
				}
				$("#academicField li").each(function(i){
				    var textt = $(this).text();
				    if(textt == subjectText){
				    	$(this).addClass("filterCurrent").siblings().removeClass("filterCurrent");
				    }
				});
			}
		},
		"error": function() {
			$.MsgBox.Alert('提示', '链接服务器超时')
		}
	});
}

//应用行业
function applicationIndustryVal(){
	$.ajax({
		"url": "/ajax/dataDict/qaDictCode",
		"type": "get",
		"data": {
			"dictCode":"INDUSTRY",
		},
		"success": function(data) {
			console.log(data);
			if(data.success) {
				$("#applicationIndustry").html("");
				$("#applicationIndustry").append('<li class="filterCurrent">不限</li>');
				for(var i = 0; i < data.data.length; i++) {
					var itemlist = '<li class="ititle">'+data.data[i].caption+'</li>';
					$("#applicationIndustry").append(itemlist);
					if(i > 20)
						break;
				}
			}
		},
		"error": function() {
			$.MsgBox.Alert('提示', '链接服务器超时')
		}
	});
}

//所在城市
function cityVal(){
	$.ajax({
		"url": "/ajax/dataDict/qaCity",
		"type": "get",
		"data": {
			"dictCode":"ADDRESS",
		},
		"success": function(data) {
			console.log(data);
			if(data.success) {
				$("#cityList").html("");
				$("#cityList").append('<li class="filterCurrent">不限</li>');
				for(var i = 0; i < data.data.length; i++) {
					var itemlist = '<li class="ititle">'+data.data[i].caption+'</li>';
					$("#cityList").append(itemlist);
					if(i > 20)
						break;
				}
			}
		},
		"error": function() {
			$.MsgBox.Alert('提示', '链接服务器超时')
		}
	});
}

//专家点击咨询
$('#expertList').on("click",".rightop",function(){
	if (userid && userid != "null" && userid != null) {
		var professorId = $(this).attr("id");
			ConsultApply();
			//点击的发送
			$("#sendConsultBtn").click(function() {
				sendConsultHandler($(this).attr("proId"))
			});
			concultProInfo(professorId);
	}else{
		$.MsgBox.Alert('提示',"请先登录再进行咨询");
		$("#mb_btn_ok").val("去登录");
		var aele = document.createElement('a');
		$("#mb_btnbox").append(aele);
		$("#mb_btnbox a").css({
			'display': "block",
			'width': '100%',
			'height': '40px',
			'position': 'absolute',
			'bottom': '-6px',
			'left': '0'
		});
		aele.setAttribute('href', '../login.html');
	}
})

//专家
function expertListVal(keyt,subject,industry,address,authType,pageSize,pageNo,isexpert) {
	$.ajax({
		"url": "/ajax/professor/pqBaseInfo",
		"type": "get",
		"data": {
			"key":keyt,
			"subject":subject,
			"industry":industry,
			"address":address,
			"authType":authType,
			"pageSize": pageSize,
			"pageNo": pageNo
		},
		"beforeSend": function() {
			$("#expertList").append('<img src="../images/loading.gif" class="loading"  style="position: absolute;left: 50%; top: 50%;" />');
		},
		"success": function(data) {
			console.log(data);
			if(data.success) {
				if(data.data.data.length > 0){
					$("#expertList,.expertPageCode").show();
					$(".expertPage").hide();
					$("#expertList").html("");
					var itemlist = '';
					for(var i = 0; i < data.data.data.length; i++) {
						var itemlist = '<li class="flexCenter">';
						itemlist += '<a href="" class="expertUrl linkhref"><div class="lefthead userheadt userRadius" id="expertImg"></div>';
						itemlist += '<div class="centercon">';
						itemlist += '<p class="h1font">';
						itemlist += '<span class="nameSpan" id="Name"></span>';
						itemlist += '<em class="authiconNew" title="科袖认证专家"></em></p>';
						itemlist += '<p class="h2font ellipsisSty" id="ellipsisSty">职称/职位，所在机构</p>';
						itemlist += '<p class="h2font ellipsisSty" id="researchAreas"></p>';
						itemlist += '</div></a><span class="rightop" style="display:none"  id="">咨询</span>';
						itemlist += '</li>';
						$itemlist = $(itemlist);
						$("#expertList").append($itemlist);
						var datalist = data.data.data[i];
						$itemlist.find(".expertUrl").attr("href", "information-brow.html?professorId=" + datalist.id );
						$itemlist.find("#Name").text(datalist.name);
						$itemlist.find(".rightop").attr("id",datalist.id);
						if(datalist.id != userid){
							$itemlist.find(".rightop").show();
						}
						var userType = autho(datalist.authType, datalist.orgAuth, datalist.authStatus);
						$itemlist.find(".authiconNew").attr("title", userType.title);
						$itemlist.find(".authiconNew").addClass(userType.sty);
						if(datalist.hasHeadImage) {
							$itemlist.find("#expertImg").attr("style", "background-image: url(/images/head/" + datalist.id + "_l.jpg);");
						}
						var title = datalist.title || "";
						var orgName = datalist.orgName || "";
						var office = datalist.office || "";
						if(title != "") {
							title = title + " / ";
						}
						if(office != "") {
							office =  office + "，" ;
						}
						if(orgName != "") {
							orgName = orgName;
						}
						$itemlist.find("#ellipsisSty").text(title+office+orgName);
						
						/*获取研究方向信息*/
						var researchAreas = datalist.researchAreas;
						if( researchAreas.length > 0){
							var rlist = '研究方向：';
							for(var n = 0; n < researchAreas.length; n++) {
								//console.log(researchAreas[n].caption);
								rlist += researchAreas[n].caption
								if(n < researchAreas.length - 1) {
									rlist += "；"
								}
							}
							$itemlist.find("#researchAreas").text(rlist);
						}
					}
					//分页
					if(isexpert==true){
						$(".expertPageCode").createPage({
							pageCount: Math.ceil(data.data.total / 20),
							current: data.data.pageNo,
							backFn: function(p) {
								$("#expertList").html("");
								expertListVal(keyt,subject,industry,address,authType,20,p,false);
								document.body.scrollTop = document.documentElement.scrollTop = 0;
							}
						});
					}
					
				}else{
					$("#expertList,.expertPageCode").hide();
					$(".expertPage").show();
				}
				$(".loading").remove();
			}

		},
		"error": function() {
			$.MsgBox.Alert('提示', '链接服务器超时')
		}
	});
}

//文章
function articalListVal(pageSize, pageNo,isbind) {
	$.ajax({
		"url": "/ajax/article/firstpq",
		"type": "get",
		"async": true,
		"data": {
			"key":$(".searchsome").val(),
			"pageSize": pageSize,
			"pageNo": pageNo
		},
		"beforeSend": function() {
			$("#articalList").append('<img src="../images/loading.gif" class="loading"  style="position: absolute;left: 50%; top: 50%;" />');
		},
		"success": function(data) {
			console.log(data);
			if(data.success) {
				if(data.data.data.length > 0){
					$("#articalList,.articalPageCode").show();
					$(".articalPage").hide();
					var itemlist = '';
					$("#articalList").html("");
					for(var i = 0; i < data.data.data.length; i++) {
						var itemlist = '<li class="flexCenter">';
						itemlist += '<a href="" class="artivleUrl linkhref"><div class="lefthead articalhead" id="articleImg"></div>';
						itemlist += '<div class="centercon centercon2">';
						itemlist += '<p class="h1font ellipsisSty" id="articalTitle"></p>';
						itemlist += '<p class="h2font">';
						itemlist += '<span class="nameSpan" id="userName"></span>';
						itemlist += '<em class="authiconNew" title="科袖认证专家"></em>';
						itemlist += '</p></div></a></li>';
						$itemlist = $(itemlist);
						$("#articalList").append($itemlist);
						var datalist = data.data.data[i];
						var articleType = datalist.articleType;
						$itemlist.find("#articalTitle").text(datalist.articleTitle);
						$itemlist.find(".artivleUrl").attr("href", "articalShow.html?articleId=" + datalist.articleId );
						if(datalist.articleImg) {
							$itemlist.find("#articleImg").attr("style", "background-image: url(/data/article/" + datalist.articleImg + ");");
						}
						if(articleType == 1) { //个人文章
							$itemlist.find("#userName").text(datalist.professor.name);
							var userType = autho(datalist.professor.authType, datalist.professor.orgAuth, datalist.professor.authStatus);
							$itemlist.find(".authiconNew").attr("title", userType.title);
							$itemlist.find(".authiconNew").addClass(userType.sty);
						} else if(articleType == 2) { //企业文章
							$itemlist.find("#userName").text(datalist.editOrganization.name);
							if(datalist.editOrganization.authStatus==3){
								$itemlist.find(".authiconNew").addClass("authicon-com-ok").attr("title", "认证企业");;	
							}
						}
					}
					//分页
					if(isbind==true){
						$(".articalPageCode").createPage({
							pageCount: Math.ceil(data.data.total / 20),
							current: data.data.pageNo,
							backFn: function(p) {
								$("#articalList").html("");
								articalListVal(20, p,false);
								document.body.scrollTop = document.documentElement.scrollTop = 0;
							}
						});
					}
					
				}else{
					$("#articalList,.articalPageCode").hide();
					$(".articalPage").show();
				}
				$(".loading").remove();
			}

		},
		"error": function() {
			$.MsgBox.Alert('提示', '链接服务器超时')
		}
	});
}

//资源
function resourceListVal(pageSize, pageNo,isresource) {
	$.ajax({
		"url": "/ajax/resource/firstpq",
		"type": "get",
		"async": true,
		"data": {
			"key":$(".searchsome").val(),
			"pageSize": pageSize,
			"pageNo": pageNo
		},
		"beforeSend": function() {
			$("#resourceList").append('<img src="../images/loading.gif" class="loading"  style="position: absolute;left: 50%; top: 50%;" />');
		},
		"success": function(data) {
			console.log(data);
			if(data.success) {
				if(data.data.data.length > 0){
					$("#resourceList,.resourcePageCode").show();
					$(".resourcePage").hide();
					var itemlist = '';
					$("#resourceList").html("");
					for(var i = 0; i < data.data.data.length; i++) {
						var itemlist = '<li class="flexCenter">';
						itemlist += '<a href="" class="resourceUrl linkhref"><div class="lefthead resouhead" id="resourceImg"></div>';
						itemlist += '<div class="centercon">';
						itemlist += '<p class="h1font" id="resourceName"></p>';
						itemlist += '<p class="h2font ellipsisSty" id="supportedServices"></p>';
						itemlist += '<p class="h2font">';
						itemlist += '<span class="nameSpan" id="username"></span>';
						itemlist += '<em class="authiconNew authicon-pro" title="科袖认证专家"></em>';
						itemlist += '</p></div></a></li>';
						$itemlist = $(itemlist);
						$("#resourceList").append($itemlist);
						var datalist = data.data.data[i];
						var resourceType = datalist.resourceType;
						$itemlist.find("#resourceName").text(datalist.resourceName);
						$itemlist.find("#supportedServices").text("用途：" + datalist.supportedServices);
						$itemlist.find(".resourceUrl").attr("href", "resourceShow.html?resourceId=" + datalist.resourceId );
						if(datalist.images.length > 0) {
							$itemlist.find("#resourceImg").attr("style", "background-image: url(/data/resource/" + datalist.images[0].imageSrc + ");");
						}
						if(resourceType == 1) { //个人资源
							$itemlist.find("#username").text(datalist.editProfessor.name);
							var userType = autho(datalist.editProfessor.authType, datalist.editProfessor.orgAuth, datalist.editProfessor.authStatus);
							$itemlist.find(".authiconNew").attr("title", userType.title);
							$itemlist.find(".authiconNew").addClass(userType.sty);
						} else if(resourceType == 2) { //企业资源
							$itemlist.find("#username").text(datalist.organization.name);
							if(datalist.organization.authStatus==3){
								$itemlist.find(".authiconNew").addClass("authicon-com-ok").attr("title", "认证企业");;	
							}
						}
					}
					//分页
					if(isresource==true){
						$(".resourcePageCode").createPage({
							pageCount: Math.ceil(data.data.total / 20),
							current: data.data.pageNo,
							backFn: function(p) {
								$("#resourceList").html("");
								resourceListVal(20, p,false);
								document.body.scrollTop = document.documentElement.scrollTop = 0;
							}
						});
					}
					
				}else{
					$("#resourceList,.resourcePageCode").hide();
					$(".resourcePage").show();
				}
				$(".loading").remove();
			}

		},
		"error": function() {
			$.MsgBox.Alert('提示', '链接服务器超时')
		}
	});
}
