$(function() {
	//alert("sciresouser");
	//var ue;

	var userid = $.cookie("userid");
	var name = $.cookie("userName");
	var flag;

	////发布资源发布及修改
	var resourceNamePass = true;
	//var professorPass=true;
	var supportedServicesPass = true;

	function resourceName() {
		var code = $("#resourceName").val();
		if(code.length == 0 || code == " ") {
			$(".workmsg1").text("发布资源名称不能为空");
			resourceNamePass = false;
		} else {
			$(".workmsg1").text("");
			resourceNamePass = true;
		}
	}
	$("#resourceName").blur(function() {
		resourceName();
	});

	//验证可提供服务	
	function supportedServices() {
		var code = $("#supportedServices").val();
		if(code.length == 0 || code == " ") {
			$(".workmsg3").text("可提供服务不能为空");
			supportedServicesPass = false;
		} else {
			$(".workmsg3").text("");
			supportedServicesPass = true;
		}
	}
	$("#supportedServices").blur(function() {
		supportedServices();
	});

	function industryShow(data, industryList, deleteIndustry) {
		if(data != undefined && data.length != 0) {
			var subs = new Array();
			if(data.indexOf(',')) {
				subs = data.split(',');
			} else {
				subs[0] = data;
			}
			if(subs.length > 0) {
				for(var i = 0; i < subs.length; i++) {
					$("#" + industryList + "").append("<div class='" + deleteIndustry + "'><em>" + subs[i] + "</em><span class='removeNu'><img src='images/move.png'></span></div>");
				};
			}
		}
	}
	//添加或者删除应用行业及学术领域
	function addDele() {
		$("#industryList").on("click", ".removeNu", function() {
			$(this).parent().remove();
		});
		$("#industryAdd").click(function() {
			var val = $("#industry").val();
			if(!val.trim()) {
				$(".workmsg5").text("请填写应用行业");
				return;
			} else {
				$(".workmsg5").text("");
			}
			var vallist1 = $("#industryList .deleteIndustry em");
			for(var i = 0; i < vallist1.length; i++) {
				if(vallist1[i].innerText == val) {
					$(".workmsg5").text("不能添加重复内容");
					return;
				}
			}
			$("#industryList").append("<div class='deleteIndustry'><em>" + val + "</em><span class='removeNu'><img src='images/move.png'></span></div>")
			$("#industry").val("");
		});
		$("#SubjectList").on("click", ".removeNu", function() {
			$(this).parent().remove();
		});
		$("#subjectAdd").click(function() {
			var va = $("#subject").val();
			if(!va.trim()) {
				$(".workmsg4").text("请填写学术领域");
				return;
			} else {
				$(".workmsg4").text("");
			}
			var vallist2 = $("#SubjectList .deleteSubject em");
			for(var i = 0; i < vallist2.length; i++) {
				if(vallist2[i].innerText == va) {
					$(".workmsg4").text("不能添加重复内容");
					return;
				}
			}
			$("#SubjectList").append("<div class='deleteSubject'><em>" + va + "</em><span class='removeNu'><img src='images/move.png'></span></div>")
			$("#subject").val("");
		});
	}
	//我的资源的删除，修改
	$("#workmysrc").on("click", ".resoumag", function() {
		//初始引用编辑器
		ResourceInfo();
		addDele();
		$(".limitBox").removeClass("limitBox");
		$(".limitwords").removeClass("limitwords");
		$(".limitwordbox").removeClass("limitwordbox");
		$(".respublisher").css("display", "none");
		$(".stateinfor span").text("资源发布中");
		var resourceID = $(this).attr("resourceId");

		function delet() {
			$.ajax({
				"url": "/ajax/resource/" + resourceID,
				"type": "DELETE",
				"success": function($data) {
					if($data.success) {
						$(".blackcover").remove();
						$("body").css("position", "");
						getDataResource();
						UE.delEditor('editor');
					} else {
						$.MsgBox.Alert('message', $data.msg);
					}
				}
			})
		}
		if(resourceID) {
			$("#deletedResource").on("click", function() {
				$("body").css("position", "");
				$.MsgBox.Confirm("消息", "执行删除后资源信息将无法恢复，确定继续吗？", delet)
			})
			$.ajax({
				"url": "/ajax/resource/" + resourceID,
				"type": "get",
				"async": false,
				"success": function(info) {
					if(info.success) {
						//console.log(info);
						$("#resourceName").val(info.data.resourceName);
						$("#supportedServices").val(info.data.supportedServices);
						$("#hopePayMethod").val(info.data.hopePayMethod);
						$("#cooperationNotes").val(info.data.cooperationNotes);
						industryShow(info.data.subject, "SubjectList", "deleteSubject");
						industryShow(info.data.industry, "industryList", "deleteIndustry");
						ue.ready(function() {
							if(info.data.descp == undefined) {
								var datadescp = "";
							} else {
								var datadescp = info.data.descp;
							}
							ue.setContent(datadescp);
						});

						$("#professor").text(name);
						if(info.data.images.length) {
							var $liMages = $(
								'<div id="WU_FILE" class="file-item thumbnail ResImgBox" style="max-width:226px;width:226px;height: 226px;">' +
								'<img class="resImg headRadius resourceImg" style="display: inline;" src="/images/resource/' + resourceID + '.jpg">' +
								'</div>'
							)
						} else {
							var $liMages = $(
								'<div id="WU_FILE" class="file-item thumbnail ResImgBox" style="max-width:226px;width:226px;height: 226px;">' +
								'<img class="resImg headRadius resourceImg" style="display: inline;" src="/images/default-resource.jpg">' +
								'</div>'
							)
						}
						$("#fileList").append($liMages);
						$(".upbox").css({
							"marginTop": "0px"
						});
						$("#deletedResource").show();

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

		function captiureSubInd(subIndu) {
			var industrys = $("#" + subIndu + "");
			var industryAll = "";
			if(industrys.size() > 0) {
				for(var i = 0; i < industrys.size(); i++) {
					industryAll += industrys[i].innerText;
					industryAll += ',';
				};
				industryAll = industryAll.substring(0, industryAll.length - 1);
			}
			return industryAll;
		}
		$("#inputb").on("click", function() {
			var $data = {};
			$data.resourceName = $("#resourceName").val();
			$data.supportedServices = $("#supportedServices").val();
			$data.professorId = userid;
			$data.hopePayMethod = $("#hopePayMethod").val();
			$data.cooperationNotes = $("#cooperationNotes").val();
			$data.subject = captiureSubInd("SubjectList .deleteSubject");
			$data.industry = captiureSubInd("industryList .deleteIndustry");
			$data.descp = ue.getContent();
			if(cacheImageKey) {
				console.log(cacheImageKey);
				$data.fn = cacheImageKey;
			}
			if(resourceID) {

				$data.resourceId = resourceID;

			}
			console.log($data);

			function getDataput() {
				$.ajax({
					"url": "ajax/resource/updateResource",
					"type": "post",
					"success": function(rdata) {
						console.log(rdata);
						if(rdata.success) {
							getDataResource();
							$(".blackcover").remove();
							UE.delEditor('editor');
						} else {
							$.MsgBox.Alert("消息", "资源发布失败");
						}
					},
					"data": $data,
					"beforeSend": function() { /*console.log(this.data)*/ },
					"contentType": "application/x-www-form-urlencoded",
					dataType: "json"
				});
			}
			//console.log($data);
			resourceName();
			//							professor();
			supportedServices();
			if(resourceNamePass && supportedServicesPass) {
				if(resourceID) {
					//$(".blackcover").remove();
					$("body").css("position", "");
					$.MsgBox.Confirm("消息", "确定修改资源信息吗？", getDataput)
					$data.fn = cacheImageKey;
				} else {

					$.ajax({
						"url": "/ajax/resource",
						"type": "POST",
						"success": function(rdata) {
							if(rdata.success) {
								$(".blackcover").remove();
								$("body").css("position", "");
								getDataResource();
								$.MsgBox.Alert("消息", "资源发布成功");
								UE.delEditor('editor');
							} else {
								$.MsgBox.Alert("消息", "资源发布失败");
							}
						},
						"data": $data,
						"beforeSend": function() { /*console.log(this.data)*/ },
						"contentType": "application/x-www-form-urlencoded",
						dataType: "json"
					});
				}

			}
		});
	});

	getDataResource();

	function getDataResource() {
		$.ajax({
			"url": "/ajax/resource/qapro",
			"type": "get",
			"async": true,
			"data": {
				"professorId": userid
			},
			"beforeSend": function() {
				$("#consd").append('<img src="../images/loading.gif" class="loading" />');
			},
			"success": function(data) {

				if(data.success) {
					var add = '';
					var k = 0;
					$("#consd").html("");
					var addbut = ' <div class="addworkbtn resoumag" ></div>';
					$("#consd").append(addbut);
					for(var i = 0; i < data.data.length; i++) {

						var add = '<div class="addworkbtn-list"> <div class="resoumag">'
						add += '<div class="ResImgBox" style="max-width:226px;width:226px;height: 226px;"><img class="resImg headRadius resourceImg" src="" /></div>'
						add += '<h2>' + data.data[i].resourceName + '</h2>'
						add += '<p>' + data.data[i].supportedServices + '</p>'
						add += '</div></div>';
						$add = $(add);

						$("#consd").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", "/images/resource/" + data.data[i].resourceId + ".jpg");
						} else {
							$add.find(".resourceImg").attr("src", "/images/default-resource.jpg");
						}

						add = "";
						$(".loading").remove();
					}

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

});