$(document).ready(function() {
	var oDemandAim = "",
		oSortType = "";
	/*需求列表*/
	function demandList(isbind, pageSize, pageNo, oDemand, oSort) {
		$.ajax({
			url: "/ajax/demand/pq",
			type: "GET",
			timeout: 10000,
			dataType: "json",
			data: {
				"pageNo": pageNo,
				"demandAim": oDemand,
				"sortType": oSort
			},
			beforeSend: function() {},
			success: function(data, textState) {
				if(data.success) {
					console.log(data);
					$("#demandList").html(" ");
					var $info = data.data.data;
					demandHtml($info);
					if(isbind == true) {
						$(".tcdPageCode").createPage({
							pageCount: Math.ceil(data.data.total / pageSize),
							current: data.data.data.pageNo,
							backFn: function(p) {
								demandList(false, 5, p, oDemandAim, oSortType);
							}
						});
					}
				}

			},
			error: function(XMLHttpRequest, textStats, errorThrown) {

			}
		})
	}

	function demandHtml($data) {
		for(var i = 0; i < $data.length; i++) {
			var $obje = $data[i].professor;
			var personStr = personalInformation($obje);
			var demandAim = "技术咨询",
				demandType = "企业需求";
			var oImg = "",
				oTime = "";
			if($data[i].demandAim == 2) {
				demandAim = "寻找资源"
			} else if($data[i].demandAim == 3) {
				demandAim = "其他需求"
			}
			if($data[i].demandType == 1) {
				demandType = "个人需求"
			}
			if($obje.hasHeadImage) {
				oImg = "/images/head/" + $obje.id + "_l.jpg";
			} else {
				oImg = "images/default-photo.jpg"
			}
			if($obje.authType) {
				var typeTname = " authicon authicon-cu"
			} else {
				if($obje.authStatus == 3) {
					if($obje.authentication == 1) {
						var typeTname = " authicon2 authicon-mana";
					} else if($obje.authentication == 2) {
						var typeTname = " authicon2 authicon-staff";
					} else {
						var typeTname = " authicon2 authicon-stu"
					}
				}
			}
			oTime = $data[i]["createTime"].substr(0, 4) + "年" + $data[i].createTime.substr(4, 2) + "月" + $data[i].createTime.substr(6, 2) + "日" +
				$data[i].createTime.substr(8, 2) + ":" + $data[i].createTime.substr(10, 2);
			var dataString = "<table width='100%'>"
			dataString += "<tbody><tr><td width='14%' class='messagebox'>"
			dataString += "<a class='workhead workitimg headRadius'>"
			dataString += "<img class='headPhoto' src='" + oImg + "'  width='100%' height='100%'></a></td>"
			dataString += "<td style='position:relative;top:20px;' width='86%'>"
			dataString += "<div class='workinfor worksitcon'><h4><a class='named'>" + $obje.name + "</a>"
			dataString += "<a class='modifyicon" + typeTname + "'></a><input type='text' class='assessStar' style='display: none;'></h4><h6 class='h3Font'>"
			dataString += "<span>" + personStr + "</span>"
			dataString += "<h6 style='position:relative;'>"
			dataString += "<div class='titList'>需求主题：<em class='h4Font'>" + $data[i].demandTitle + "</em></div>"
			dataString += "<span class='lasttime rightTime'>" + oTime + "</span>"
			dataString += "<p class='rebackcon' >" + $data[i].demandContent + "</p></div>"
			dataString += "<div class='workhandle'><div class='rightopert floatR'>"
			dataString += "<span class='replybtn' demanid='" + $data[i].demandId + "'>查看</span></div>"
			dataString += "<div class='leftstate floatR'>"
			dataString += "<span class='coultstate status-4'><i>" + demandAim + "</i></span></div>"
			dataString += "<div class='leftstate floatR'>"
			dataString += "<span class='coultstate coulstAim status-4'><i>" + demandType + "</i></span>"
			dataString += "</div></div></td></tr></tbody></table>"
			$("#demandList").append(dataString);
		}
	}
	/*职位职称所在机构，部门，地址等等*/
	function personalInformation($person) {
		var arr1 = [$person.title, $person.office, $person.department, $person.orgName, $person.address];
		var arr = new Array();
		var arr2 = new Array();
		var n = 0;
		for(var i = 0; i < arr1.length; i++) {
			if(arr1[i]) {
				arr.push(arr1[i]);
				if(arr1[i] == arr1[arr1.length - 1]) {
					n = 1;
				}
			}
		}
		if(n == 1) {
			for(var i = 0; i < arr.length-1; i++) {
				arr2.push(arr[i]);
			}
			return arr2.join()+ " | " + arr[arr.length - 1]; 
		} else {
			return arr.join();
		}

	}
	demandList(true, 5, 1, oDemandAim, oSortType);

	/*检索排序*/
	function selcet(i) {
		$(".selcet" + i).on('click', function(e) {
			var option = $(this).find(".option" + i);
			option.css("display", "block")
			var o_this = $(this);
			o_this.find("li").click(function() {
				o_this.find("li").removeClass("workcurrent");
				$(this).addClass("workcurrent");
				o_this.find("span").text($(this).text());
				option.css("display", "none");
				if($(this).text() == "技术咨询") {
					oDemandAim = 1;
					$(".tcdPageCode").remove();
					$("#shoudao").append('<div class="tcdPageCode"></div>');
					demandList(true, 5, 1, oDemandAim, oSortType);
				} else if($(this).text() == "资源合作") {
					oDemandAim = 2;
					$(".tcdPageCode").remove();
					$("#shoudao").append('<div class="tcdPageCode"></div>');
					demandList(true, 5, 1, oDemandAim, oSortType);
				} else if($(this).text() == "其他需求") {
					oDemandAim = 3;
					$(".tcdPageCode").remove();
					$("#shoudao").append('<div class="tcdPageCode"></div>');
					demandList(true, 5, 1, oDemandAim, oSortType);
				} else if($(this).text() == "不限") {
					oDemandAim = "";
					$(".tcdPageCode").remove();
					$("#shoudao").append('<div class="tcdPageCode"></div>');
					demandList(true, 5, 1, oDemandAim, oSortType);
				} else if($(this).text() == "按最早发布时间排序") {
					oSortType = 1;
					$(".tcdPageCode").remove();
					$("#shoudao").append('<div class="tcdPageCode"></div>');
					demandList(true, 5, 1, oDemandAim, oSortType);
				} else if($(this).text() == "按最新发布时间排序") {
					oSortType = 0;
					$(".tcdPageCode").remove();
					$("#shoudao").append('<div class="tcdPageCode"></div>');
					demandList(true, 5, 1, oDemandAim, oSortType);
				}
				return false;
			});
			$("body").click(function(e) {
				if($(".option4")[0].style.display == "block" || $(".option3")[0].style.display == "block") {
					$(".option4")[0].style.display = "none";
					$(".option3")[0].style.display = "none";
				}
			});
			return false;
		})
	}
	selcet(3);
	selcet(4);
	/*进入needSure.html*/
	$("#demandList").on("click", ".replybtn", function() {
		var demand = $(this).attr("demanid");
		location.href = "needSure.html?demandId=" + demand;
	})
})