$(document).ready(function() {
	var yesNo = "",
		oSortType = 0;
	/*我的需求列表*/
	function myDemandList(isbind, page, pageNum, dema, sortType) {
		$.ajax({
			url: "/ajax/demand/pqDemander",
			type: "GET",
			timeout: 10000,
			dataType: "json",
			data: {
				"demander": userid,
				"pageNo": page,
				"demandStatus": dema,
				"sortType": sortType,
				"pageSize": pageNum
			},
			beforeSend: function() {},
			success: function(data, textState) {
				if(data.success) {
//					if(data.data.data.length==0){
//						return;
//					}
					$(".workselectitem").html(" ");
					demandHtml(data.data.data);
					if(isbind == true) {
						$(".tcdPageCode").createPage({
							pageCount: Math.ceil(data.data.total / pageNum),
							current: data.data.data.pageNo,
							backFn: function(p) {
								myDemandList(false, p, 5, yesNo, oSortType);
							}
						});
					}
				}
			},
			error: function(XMLHttpRequest, textStats, errorThrown) {

			}
		})
	}

	function demandHtml($data) {
		for(var i = 0; i < $data.length; i++) {
			var oTime, oDemandType, oDemandAim, oDemandStatus;
			if($data[i].demandType == 1) {
				oDemandType = "个人需求"
			} else if($data[i].demandType == 2) {
				oDemandType = "企业需求"
			}
			if($data[i].demandAim == 1) {
				oDemandAim = "技术咨询"
			} else if($data[i].demandAim == 2) {
				oDemandAim = "寻找资源"
			} else if($data[i].demandAim == 3) {
				oDemandAim = "其他需求"
			}
			if($data[i].demandStatus == 0) {
				oDemandStatus = "已关闭"
			} else if($data[i].demandStatus == 1) {
				oDemandStatus = "发布中"
			}
			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 oString = "<table width='100%'><tbody><tr>"
			oString += "<td style='position:relative;top:20px;'>"
			oString += "<div class='workinfor worksitcon'>"
			oString += "<h4><div class='titList h4Font' style='width:620px'>" + $data[i].demandTitle + "</div></h4>"
			oString += "<h6 style='position:relative;'>"
			oString += "<div class='clearfix'>"
			oString += "<div class='floatL'>发布时间：<span class='lasttime'>" + oTime + "</span></div>"
			if($data[i].closeTime) {
				var oCloseTime = $data[i]["closeTime"].substr(0, 4) + "年" + $data[i].closeTime.substr(4, 2) + "月" + $data[i].closeTime.substr(6, 2) + "日" +
					$data[i].closeTime.substr(8, 2) + ":" + $data[i].closeTime.substr(10, 2);
				oString += "<div class='floatL' style='margin-left:40px;'>关闭时间：<span class='lasttime'>" + oCloseTime + "</span></div></div>"
			}
			oString += "<p class='rebackcon lastReplyCon' style='width: 860px;' >" + $data[i].demandContent + "</p></h6></div>"
			oString += "<div class='workhandle'>"
			oString += "<div class='rightopert floatR'>"
			oString += "<span class='replybtn'demanid='" + $data[i].demandId + "'>查看</span></div>"
			oString += "<div class='leftstate floatR'>"
			oString += "<span class='coultstate status-1'><i>" + oDemandStatus + "</i></span></div>"
			oString += "<div class='leftstate floatR'>"
			oString += "<span class='coultstate status-4'><i>" + oDemandAim + "</i></span></div>"
			oString += "<div class='leftstate floatR'>"
			oString += "<span class='coultstate coulstAim status-4'><i>" + oDemandType + "</i></span></div></div></td></tr></tbody></table>"
			$(".workselectitem").append(oString)
		}
	}
	myDemandList(true, 1, 5, yesNo, 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() == "全部") {
					yesNo = "";
					$(".tcdPageCode").remove();
					$("#workContainer2").append('<div class="tcdPageCode"></div>');
					myDemandList(true, 1, 5, yesNo, oSortType);
				} else if($(this).text() == "发布中") {
					yesNo = 1;
					$(".tcdPageCode").remove();
					$("#workContainer2").append('<div class="tcdPageCode"></div>');
					myDemandList(true, 1, 5, yesNo, oSortType);
				} else if($(this).text() == "已关闭") {
					yesNo = 0;
					$(".tcdPageCode").remove();
					$("#workContainer2").append('<div class="tcdPageCode"></div>');
					myDemandList(true, 1, 5, yesNo, oSortType);
				} else if($(this).text() == "按最早发布时间排序") {
					oSortType = 1;
					$(".tcdPageCode").remove();
					$("#workContainer2").append('<div class="tcdPageCode"></div>');
					myDemandList(true, 1, 5, yesNo, oSortType);
				} else if($(this).text() == "按最新发布时间排序") {
					oSortType = 0;
					$(".tcdPageCode").remove();
					$("#workContainer2").append('<div class="tcdPageCode"></div>');
					myDemandList(true, 1, 5, yesNo, oSortType);
				}
				return false;
			});
			$("body").click(function(e) {
				if($(".option4")[0].style.display == "block" || $(".option1")[0].style.display == "block") {
					$(".option4")[0].style.display = "none";
					$(".option1")[0].style.display = "none";
				}
			});
			return false;
		})
	}
	selcet(1);
	selcet(4);
	$(".workselectitem").on("click", ".replybtn", function() {
		var de = $(this).attr("demanid");
		location.href = "needShow.html?demandId=" + de
	});
	/*发布新需求*/
	$("#needIssueBtn").click(function() {
		$.ajax({
			url: "/ajax/professor/auth",
			type: "GET",
			timeout: 10000,
			dataType: "json",
			data: {
				"id": userid,
			},
			success: function(data, textState) {
				if(data.success) {
					var $rta=data.data;
					if($rta.authStatus!=3){
						location.href="realname-authentication.html";
					}else{
						$(".blackcover ").show();
						$("#induSub").val("");
						$("textarea").val("");
						$("#demandContent").val("");
						$("#navsub").find("li").removeClass("aimedLi");
						$("#mutChecked").find("li").removeClass("checkedLi");
						
					}
					
				}

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

			}
		})
		
	});
	$("#workclose2").click(function() {
			$(".blackcover ").hide();
		})
		/*发布新需求目的*/
	var consun;
	tab("navsub"); //身份切换
	function tab(name) {
		var oDome = document.getElementById(name);
		var oSpan = oDome.getElementsByTagName('ul')[0].childNodes;
		for(var i = 0; i < oSpan.length; i++) {
			oSpan[i].onclick = function() {
				for(var i = 0; i < oSpan.length; i++) {
					oSpan[i].className = '';
				}
				this.className = 'aimedLi';
				if(this.getElementsByTagName("span")[0].innerText == "咨询技术难题") {
					consun = 1;
				} else if(this.getElementsByTagName("span")[0].innerText == "寻求研发资源") {
					consun = 2;
				} else {
					consun = 3;
				}
			}
		}
	}

	/*需求题目限制30个字*/
	$("#demandContent").on('keyup', function() {
		if(this.value.length > 30) {
			this.value = this.value.substring(0, 30);
		}
	});
	/*需求内容限制300个字*/
	$("textarea").on('keyup', function() {
		var maxChars = 300;
		if(this.value.length > maxChars) {
			this.value = this.value.substring(0, maxChars);
		}
		var oCanInput = maxChars - this.value.length;
		$("#countNum").text(oCanInput);
	});
	/*查询应用行业及学术领域*/
	function industry(insu) {
		$.ajax({
			url: "/ajax/dataDict/qaDictCode",
			dataType: 'json', //数据格式类型
			type: 'GET', //http请求类型
			timeout: 10000, //超时设置
			data: {
				"dictCode": insu
			},
			success: function(data) {
				if(data.success) {
					var $data = data.data;
					var n;
					($data.length > 5) ? n = 5:
						n = $data.length
					for(var i = 0; i < n; i++) {
						var oString = '<li><span>' + $data[i].caption + '</span></li>';
						$("#mutChecked").append(oString);
					}
				}
			},
			error: function() {
				return;
			}
		});
	}
	industry("INDUSTRY");
	industry("SUBJECT");
	/*行业及领域选择*/
	$('#mutChecked').on('click', 'li', function() {
			if(this.className == "checkedLi") {
				this.className = ""
			} else {
				this.className = "checkedLi"
			}
		})
		/*行业或者领域添加及删除*/
	$("#labelshow").on("click", ".removeNu", function() {
		$(this).parent().remove();
	});
	$("#addIndu").click(function() {
			var oinduSub = $("#induSub").val();
			var oAddNum = $("#labelshow").find("em");
			if(!oinduSub.trim()) {
				$.MsgBox.Alert("消息", "请先添加内容");
				return;
			}
			if(oinduSub.trim().length>10) {
				$.MsgBox.Alert("消息", "不能多于10个字");
				return;
			}
			if(oAddNum.length == 5) {
				$.MsgBox.Alert("消息", "最多添加五条");
				return;
			}
			for(var i = 0; i < oAddNum.length; i++) {
				if(oAddNum[i].innerHTML == oinduSub) {
					$.MsgBox.Alert("消息", "不能重复添加");
					return;
				}
			}
			$("#labelshow").append('<li class="deleteSubject"><em>' + oinduSub + '</em><span class="removeNu"><img src="images/move.png"></span></li>')
			$("#induSub").val("");
		})
		/*发布新需求*/
	$("#postDemand").click(function() {
		var dd = $("#navsub").find(".aimedLi")
		if(dd.length == 0) {
			$.MsgBox.Alert("消息", "请选择您发布需求的目的");
			return;
		}
		if(!$("#demandContent").val().trim()) {
			$.MsgBox.Alert("消息", "请填写需求主题");
			return;
		}
		if(!$("textarea").val().trim()) {
			$.MsgBox.Alert("消息", "请填写需求内容");
			return;
		}
		publish()
	});

	function publish() {
		var arr = [];
		var oSuin = $(".checkedLi");
		var oin = $("#labelshow").find("li");
		for(var i = 0; i < oSuin.length; i++) {
			arr[i] = oSuin[i].innerText;
		}
		for(var j = 0; j < oin.length; j++, i++) {
			console.log(oin[j].innerText);
			arr[i] = oin[j].innerText;
		}
		$.ajax({
			url: '/ajax/demand',
			dataType: 'json', //数据格式类型
			type: 'post', //http请求类型
			timeout: 10000, //超时设置
			traditional: true,
			data: {
				"demander": userid,
				"demandAim": consun,
				"demandType": 1,
				"demandTitle": $("#demandContent").val(),
				"demandContent":$("textarea").val(),
				"args": arr
			},
			success: function(data) {
				if(data.success) {
					myDemandList(true, 1, 5, yesNo, oSortType);
					$(".blackcover ").hide();
					$.MsgBox.Alert("消息", "需求发布成功！很快会有专家与您联系，您可以在咨询列表中查看专家回复的信息");
				}
			},
			error: function() {
				return;
			}
		});
	}
	/*判断是否是专家*/
	$.ajax({
			url: "/ajax/professor/auth",
			type: "GET",
			timeout: 10000,
			dataType: "json",
			data: {
				"id": userid,
			},
			success: function(data, textState) {
				if(data.success) {
					var $rta=data.data;
					if($rta.authType==0){
						$("#searchDemand").hide();
					}
					
				}

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

			}
		})
})