$(function() {

	var userid = $.cookie("userid");
	var articleId;
	newslist();//科研新闻列表
	
	$("#workmysrc").on("click", ".newsadd", function() {
		newsBombBox();//插入弹框
		addDele();//编辑学术领域和应用行业	
		$("#inputb").on("click", function() {
			if($("#newsName").val()!=""){
				newsAdd();
			}else{
				$(".workmsg1").text("发布文章名称不能为空");
			}
			
		});
	});
	
	$("#workmysrc").on("click", ".resoumag", function() {
		articleId = $(this).attr("data-title");
		newsBombBox();//插入弹框
		addDele();//编辑学术领域和应用行业
		modifyDisplay();
		$("#inputb").on("click", function() {
			if($("#newsName").val()!=""){
				$.MsgBox.Confirm("消息", "确定修改文章信息吗？", newsModify)
			}else{
				$(".workmsg1").text("发布文章名称不能为空");
			}
		});
	});
	
    /*科研新闻列表*/
	function newslist() {
		$.ajax({
			"url": "/ajax/article/qaPro",
			"type": "get",
			"async": true,
			"data": {
				"professorId": userid
			},
			"beforeSend": function() {
				$("#newsbox").append('<img src="../images/loading.gif" class="loading" />');
			},
			"success": function(data) {
				console.log(data);
				if(data.success) {
					var add = '';
					$("#newsbox").html("");
					var addbut = ' <div class="addworkbtn newsadd" ></div>';
					$("#newsbox").append(addbut);
					for(var i = 0; i < data.data.length; i++) {
						var add = '<div class="addworkbtn-list addworkbtn-list2" ><div class="resoumag" data-title="">'
						add += '<div class="art_topicBox art_topicBox2"><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 += '</div></div>';
						$add = $(add);
						$("#newsbox").append($add);
						$add.find('.resoumag').attr("data-title", data.data[i].articleId);
						$add.find('.tagsBox span').text(data.data[i].industry);
						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;");
						}

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

				} else {
					$.MsgBox.Alert('message', "系统异常!");
				}
			},
			"error": function() {
				$.MsgBox.Alert('message', 'failed')
			}
		});
	}
	
    /*科研新闻添加*/
	function newsAdd(){
	    var $data = {};
		$data.professorId = userid;
		$data.articleTitle = $("#newsName").val();
		$data.subject = captiureSubInd("SubjectList .deleteSubject");
		$data.industry = captiureSubInd("industryList .deleteIndustry");
		$data.articleContent = ue.getContent();
		if(cacheImageKey) {
			$data.articleImg = cacheImageKey;
		}
		$.ajax({
			"url" : "/ajax/article",
			"type" :  "post" ,
			"dataType" : "json",
			"data" :$data,
			"success" : function(data) {
				console.log(data);
				if (data.success){
					newsname = data.data;
					console.log(newsname);
					$.MsgBox.Alert("消息","文章发表成功！");	
					newslist();
					$(".blackcover").remove();
					UE.delEditor('editor');
				} else {
					$.MsgBox.Alert("消息","文章发表失败！");
				}
			},
			"error":function(){
				$.MsgBox.Alert('消息','链接服务器超时')
			}
		});
	}
	
	/*科研新闻修改回显内容*/
	function modifyDisplay(){
	    $.ajax({
			"url" : "/ajax/article/query",
			"type" : "GET",
			"dataType" : "json",
			"data": {
				"articleId": articleId
			},
			"success" : function($data) {	
				console.log($data);
				if ($data.success) {
					$("#newsName").val($data.data.articleTitle);	
					industryShow($data.data.subject,"SubjectList","deleteSubject");
		            industryShow($data.data.industry,"industryList","deleteIndustry");
		            ue.ready(function() {
				    	if($data.data.articleContent==undefined){
				    		var datadescp ="";
				    	}else{
				    		var datadescp = $data.data.articleContent;
				    	}
				        ue.setContent(datadescp);
				    });
				    if($data.data.articleImg) {
						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="data/article/'+$data.data.articleImg+'">'+								 			
						 		'</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-artical.jpg">'+								 			
						 			'</div>'
						            )
					}
					
					$("#fileList").append($liMages);   
				} 
			},
			"error":function(){
				$.MsgBox.Alert('消息','链接服务器超时')
			}
		})
	    
		$("#deletedResource").show();
		//$.MsgBox.Confirm("消息", "确定修改文章信息吗？", newsAdd())
		$("#deletedResource").on("click", function() {
			
			$.MsgBox.Confirm("消息", "执行删除后文章信息将无法恢复，确定继续吗？",newsDelet)	
			
		});
	}

	/*科研新闻修改*/
	function newsModify() {
		var $data = {};
		$data.articleId = articleId;
		$data.articleTitle = $("#newsName").val();
		$data.subject = captiureSubInd("SubjectList .deleteSubject");
		$data.industry = captiureSubInd("industryList .deleteIndustry");
		$data.articleContent = ue.getContent();
		if(cacheImageKey) {
			$data.articleImg = cacheImageKey;
		}
		console.log($data);
	    $.ajax({
			"url" : "/ajax/article/updateArt",
			"type" : "POST",
			"dataType" : "json",
			"data" :$data,
			"success" : function($data) {	
				console.log($data);
				if ($data.success) {
					newslist();
					$(".blackcover").remove();
					UE.delEditor('editor');
				} 
			},
			"error":function(){
				$.MsgBox.Alert('消息','链接服务器超时')
			}
		})
	    
		$("#deletedResource").show();
		//$.MsgBox.Confirm("消息", "确定修改文章信息吗？", newsAdd())
		$("#deletedResource").on("click", function() {
			$.MsgBox.Confirm("消息", "执行删除后文章信息将无法恢复，确定继续吗？",newsDelet)		
		});
	}
	
	/*科研新闻删除*/
    function newsDelet() {
    	$.ajax({
			"url" : "/ajax/article/delete",
			"type" : "POST",
			"dataType" : "json",
			"data": {
				"articleId": articleId
			},
			"success" : function($data) {							
				if ($data.success) {
					$(".blackcover").remove();
					UE.delEditor('editor');
					newslist();	
				} 
			},
			"error":function(){
				$.MsgBox.Alert('消息','链接服务器超时')
			}
		})
	}
    
	//组合应用行业及学术领域
	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;
	}
	
	//拆解应用行业及学术领域
	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+"'>"+ subs[i] +"<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==""){
				$(".workmsg4").text("请填写应用行业");
				return;
			}
			var vallist = $("#industryList .deleteIndustry em");
			for(var i=0;i<vallist.length;i++){
				if(vallist[i].innerText==val){
					$(".workmsg4").text("不能添加重复内容");
					return;
				}
			}
			$("#industryList").append("<div class='deleteIndustry'><em>" + val + "</em><span class='removeNu'><img src='images/move.png'></span></div>")
			$("#industry").val("");
			$(".workmsg4").text("");
		});
		
		$("#SubjectList").on("click", ".removeNu", function() {
			$(this).parent().remove();
		});
		
		$("#subjectAdd").click(function() {
			var val = $("#subject").val();
			if(val==""){
				$(".workmsg3").text("请填写学术领域");
				return;
			}
			var vallist = $("#SubjectList .deleteSubject em");
			for(var i=0;i<vallist.length;i++){
				if(vallist[i].innerText==val){
					$(".workmsg3").text("不能添加重复内容");
					return;
				}
			}
			$("#SubjectList").append("<div class='deleteSubject'><em>" + val + "</em><span class='removeNu'><img src='images/move.png'></span></div>")
			$("#subject").val("");
			$(".workmsg3").text("");
		});
	}
});