$(function() {
	var userid = $.cookie("userid");
	if(userid=="null"){
		location.href="login.html"
	}
	$(".onlogin .headnavbtn li").eq(0).addClass("navcurrent");
	loginStatus();//判断个人是否登录
	var oAuthType=0;
	/*工作台专家的数据*/
	getProfessorData(userid);

	/*工作台未读的咨询消息*/
	queryUnreadConsultFn(userid);

	/*工作台最新咨询*/
	getConsultData(userid);

	/*工作台最新回复*/

	getReplyData(userid);

	/*工作台我的资源*/
	getDataResource();

	/*工作台我的文章*/
	myNews();

	/*最新回复中的发送*/
	$("#myFlatSendReplyBtn").bind("click", newReplySendFn);

	/*点击未读咨询消息查看*/
	$("#lookMyConsult").on("click", function() {
		if(ifuser.authType2 == 0) {
			window.location.href = "myConsult.html";
		} else {
			window.location.href = "consult.html";
		}
	})
	
	/*点击未读咨询消息查看*/
	$("#lookconsu").on("click", function() {
		var consultid = $("#myFlatConsultTime").attr("consultid");
		window.location.href = "consultSure.html?consultid="+consultid;
	})
	
	function getProfessorData(professorId) {
		$.ajax({
			"url": "/ajax/professor/editBaseInfo/" + professorId,
			"type": "get",
			"async": true,
			"success": function(data) {
				if(data.success) {
					oAuthType=data.data.authType;
					if(oAuthType==0){
						$("#ponUm").text("个我正在发布的需求");
						$("#searchDemand").text(">>点击查看");
						demandNumMy(); 
					}else if(oAuthType==1){
						demandNum();
					}
					$("#nameS").text(data.data.name);
					$("#allConsultCount").text(data.data.consultCount);
					
					var oSty=autho(data.data.authType,data.data.orgAuth,data.data.authStatus);
					$("#nameIcon").addClass(oSty.sty);
					$("#nameIcon").attr("title",oSty.title);
					
					if(data.data.office) {
						if(data.data.title) {
							$("#office").text(data.data.office + ",");
						} else {
							$("#office").text(data.data.office);
						}

					}
					if(data.data.title) {
						$("#title").text(data.data.title);
					}
					if(data.data.department) {
						if(data.data.orgName) {
							$("#department").text(data.data.department + ",");
						} else {
							$("#department").text(data.data.department);
						}

					}
					if(data.data.orgName) {
						$("#organization").text(data.data.orgName);
					}
					if(data.data.address) {
						$("#address").text(data.data.address);
					}
					if(data.data.hasHeadImage) {
						$(".imgProfess").attr("src", "/images/head/" + userid + "_l.jpg");
					} else {
						$(".imgProfess").attr("src", "/images/default-photo.jpg");
					}
					var startLeval = parseInt(data.data.starLevel);
					for(var i = 0; i < startLeval; i++) {
						$(".evastar2").eq(i).addClass("startStyle");
					}
				} else {
					$.MsgBox.Alert("消息", "获取专家数据失败!");
				}
			},
			"error": function() {
				$.MsgBox.Alert('message', '获取专家数据失败')
			}
		})
	}

	function queryUnreadConsultFn(professorId) {
		var params = {
			"professorId": professorId
		};
		$.ajax({
			"url": "/ajax/consult/qaReadStatus",
			"type": "get",
			"async": false,
			"data": params,
			"success": function(response) {
				$("#unreadConsult").html(response["data"]);
			},
			"error": function() {
				$.MsgBox.Alert('message', '总的未读咨询条数请求失败');
			}
		});
	};

	function getConsultData(professorId) {
		var params = {
			"professorId": professorId
		};
		$.ajax({
			"url": "/ajax/consult/qaNewConsult",
			"type": "get",
			"async": false,
			"data": params,
			"success": function(response) {
				if(response.success) {
					if(response.data != null) {
						var newConsultData = response["data"];
						$("#myFlatConsultTime").attr("consultId", newConsultData["consultId"]);
						$("#myFlatProName").text(newConsultData["professor"]["name"]);
						if(newConsultData["professor"]["office"]) {
							$("#myReplyProOffice").html(newConsultData["professor"]["office"] + ",");
						}
						if(newConsultData["professor"]["title"]) {
							$("#myReplyProTitle").html(newConsultData["professor"]["title"]);
						}
						if(newConsultData["professor"]["department"]) {
							$("#myReplyProDepartment").html(newConsultData["professor"]["department"] + ",");
						}
						if(newConsultData["professor"]["orgName"]) {
							$("#myReplyProOrgName").html(newConsultData["professor"]["orgName"]);
						}
						if(newConsultData["professor"]["address"]) {
							$("#myReplyAddress").html(newConsultData["professor"]["address"]);
						}
						$("#myFlatConsultTime").html(newConsultData["createTime"].substr(0, 4) + "-" + newConsultData["createTime"].substr(4, 2) + "-" + newConsultData["createTime"].substr(6, 2) + " " +
							newConsultData["createTime"].substr(8, 2) + ":" + newConsultData["createTime"].substr(10, 2));
						$("#myFlatConsultTitle").html(newConsultData["consultTitle"]);
						$("#myFlatConsultContent").html(newConsultData["consultContant"]);
						if(response.data.professor.hasHeadImage) {
							$("#proImage").attr("src", "images/head/" + response.data.professor.id + "_m.jpg");
						} else {
							$("#proImage").attr("src", "images/default-photo.jpg");
						}
						$("#imgusr").attr("href", "information-brow.html?professorId="+response.data.professor.id);
					} else {
						$("#wbcon1").empty("");
						var height = $("#repalyf").outerHeight(true);
						$("#wbcon1").append('<span id="wbcen">暂无最新咨询消息</span>').css("height", height);
						var oHeight = ($("#wbcon1").height() - $("#wbcen").height()) / 2;
						$("#wbcen").css({
							"color": "#999",
							"display": "block",
							"margin-top": oHeight,
							"text-align": "center"
						});
					}

				} else {
					$.MsgBox.Alert("消息", "最新咨询数据请求失败");
				}

			},
			"error": function() {
				$.MsgBox.Alert('message', '最新咨询数据请求失败');
			}

		})
	}

	function getReplyData(consultantId) {
		var params = {
			"consultantId": consultantId
		};
		$.ajax({
			"url": "/ajax/tidings/qaNewReply",
			"type": "get",
			"async": true,
			"data": params,
			"success": function(response) {
				if(response.success) {
					if(response["data"] != null) {
						var newReplyData = response["data"];
						$("#myFlatRelpyTime").attr("consultId", newReplyData["consult"]["consultId"]);
						$("#myFlatReplyProName").html(newReplyData["professor"]["name"]);
						if(newReplyData["professor"]["office"]) {
							$("#myProOffice").html(newReplyData["professor"]["office"] + ",");
						}
						if(newReplyData["professor"]["title"]) {
							$("#myProTitle").html(newReplyData["professor"]["title"]);
						}
						if(newReplyData["professor"]["department"]) {
							$("#myProDepartment").html(newReplyData["professor"]["department"] + ",");
						}
						if(newReplyData["professor"]["orgName"]) {
							$("#myProOrgName").html(newReplyData["professor"]["orgName"]);
						}
						if(newReplyData["professor"]["address"]) {
							$("#myAddress").html(newReplyData["professor"]["address"]);
						}
						$("#myFlatRelpyTime").html(newReplyData["createTime"].substr(0, 4) + "-" + newReplyData["createTime"].substr(4, 2) + "-" + newReplyData["createTime"].substr(6, 2) + " " +
							newReplyData["createTime"].substr(8, 2) + ":" + newReplyData["createTime"].substr(10, 2));
						$("#myFlatRelpyTitle").html(newReplyData["consult"]["consultTitle"]);

						$("#myFlatRelpyContent").html(newReplyData["tidingsContant"]);
						if(response.data.professor.hasHeadImage) {
							$("#proreplayImage").attr("src", "images/head/" + response.data.professor.id + "_m.jpg");
						} else {
							$("#proreplayImage").attr("src", "images/default-photo.jpg");
						}
						$("#imgusr2").attr("href", "information-brow.html?professorId="+response.data.professor.id);
					} else {
						$("#wbcon2").empty("")
						var height = $("#wbcon1").outerHeight(true);
						$("#wbcon2").append('<span id="wbcent">暂无最新回复消息</span>').css("height", height);
						var oHeight = ($("#wbcon2").height() + 16 - $("#wbcent").height()) / 2;
						$("#wbcent").css({
							"color": "#999",
							"display": "block",
							"margin-top": oHeight,
							"text-align": "center"
						});

					}

				} else {
					$.MsgBox.Alert('message', '最新回复数据请求失败');
				}

			},
			"error": function() {
				$.MsgBox.Alert('message', '最新回复数据请求失败');
			}

		})
	}

	function newReplySendFn() {
		var consultId = $("#myFlatRelpyTime").attr("consultId");
		var consultCon = $("#myFlatReplyCon").val();
		var params = {
			"tidingsContant": consultCon,
			"consultId": consultId,
			"senderId": userid
		}
		$.ajax({
			url: "/ajax/tidings",
			type: "post",
			async: false,
			"contentType": "application/x-www-form-urlencoded",
			data: params,
			success: function(response) {
				getReplyData(userid);
			},
			error: function(error) {

			}

		});
		//		$.MsgBox.Alert("消息","回复成功!");
		$("#myFlatReplyCon").val("");
	};

	function getDataResource() {
		$.ajax({
			"url": "/ajax/resource/qaForDesk",
			"type": "get",
			"async": true,
			"data": {
				"professorId": userid
			},
			"success": function(data) {
				if(data.success) {
					var add = '';
					$("#consdcen").html("");
					for(var i = 0; i < data.data.length; i++) {
						var add = '<div class="addworkbtn-list"><a href="resourceShow.html?resourceId=' + data.data[i].resourceId + '"><div class="resoumag">'
						add += '<div class="ResImgBox" style="max-width:200px;width:200px;height: 200px;"><img class="resImg headRadius resourceImg" src="" /></div>'
						add += '<h2>' + data.data[i].resourceName + '</h2>'
						add += '<p>' + data.data[i].supportedServices + '</p>'
						add += '</div></a></div>';
						$add = $(add);

						$("#consdcen").append($add);

						if(data.data[i].subject == undefined) {
							$(".li1").eq(i).css({
								"display": "none"
							});
						}

						$add.find('.resoumag').attr("resourceId", data.data[i].resourceId);

						//$('.resoumag').eq(i+1).attr("resourceId",data.data[i].resourceId);
						if(data.data[i].images.length) {
							$add.find(".resourceImg").attr("src", "/data/resource/" + data.data[i].images[0].imageSrc);
						} else {
							$add.find(".resourceImg").attr("src", "/images/default-resource.jpg");
						}
					}

					$(".addworkbtn-list:gt(2)").css({
						"display": "none"
					});

				} else {
					$.MsgBox.Alert('message', "系统异常!");
				}
			},
			"error": function() {
				$.MsgBox.Alert('message', 'failed')
			}
		});
	}

	function myNews() {
		$.ajax({
			"url": "/ajax/article/qaForDesk",
			"type": "get",
			"async": true,
			"data": {
				"professorId": userid
			},
			"success": function(data) {
				if(data.success) {

					var add = '';
					$("#myNews").html("");
					for(var i = 0; i < data.data.length; i++) {
						var add = '<li><a href="articalShow.html?articleId=' + data.data[i].articleId +'">'
						add += '<div class="art_topicBox"><div class="art_img" style=""></div>'
						add += '<div class="art_tbox"><h6 id="artical_topic" >' + data.data[i].articleTitle + '</h6></div>'
						add += '</div><div class="tagsBox"><span></span></div>'
						add += '</a></li>';
						$add = $(add);
						$("#myNews").append($add);
						$add.find('.tagsBox span').text(data.data[i].subject);
						if(data.data[i].articleImg) {
							$add.find(".art_img").attr("style", "background: url(/data/article/" + data.data[i].articleImg + ") 0 0 no-repeat;background-size:cover;");
						}
					}
					$("#myNews li:gt(2)").css({
						"display": "none"
					});
				} else {
					$.MsgBox.Alert('消息', "链接服务器超时!");
				}
			},
			"error": function() {
				$.MsgBox.Alert('消息', '链接服务器超时!')
			}
		});
	}
	$("#dett,#searchDemand").click(function(){
		
		if(oAuthType==0){
			location.href="myDemand.html"
		}else if(oAuthType==1){
			location.href="needList.html"
		}
	})
	/*登录者需求的数量*/
	function demandNumMy(){
		$.ajax({
			"url":"/ajax/demand/queryMyPublish" ,
			"type": "get",
			"async": true,
			"data":{
				"demander": userid
			},
			"success": function(data) {
				if(data.success) {
					$("#demandNumber").text(data.data)
				} else {
					$.MsgBox.Alert('消息', data.msg);
				}
			},
			"error": function(x) {
				$.MsgBox.Alert('消息', '链接服务器超时!')
			}
		});
	}
	function demandNum(){
		$.ajax({
			"url":"/ajax/demand/queryPublish" ,
			"type": "get",
			"async": true,
			"success": function(data) {
				
				if(data.success) {
					$("#demandNumber").text(data.data)
				} else {
					$.MsgBox.Alert('消息', data.msg);
				}
			},
			"error": function() {
				$.MsgBox.Alert('消息', '链接服务器超时!')
			}
		});
	}
});