<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head> 
		<%@ include file="../../include/lib.jsp" %>
	 	<%@ include file="../../include/editor.jsp"%>
	</head>
   
	<body>
	  	<div id="saveArticle"  title="添加文章" data-options="iconCls:'icon-save'" > 
	  		<form name="form"  id="contentEditSave" method="post" action="articleEdit">
	  			<div id="article_left">
			  		<div class="author">文章标题：&nbsp;&nbsp;<input class="easyui-validatebox articletitle" data-options="required:true,missingMessage:'标题不为空',invalidMessage:'标题长度为3-180个字符'" type="text" id="articleEdit_title" name="articleEdit_title" /></div>
					<div class="underContentSpace"></div>
					<div>  
	   				自定义属性：
		   				<input type="radio" id="rad1" style="width: 22px;" name="articleProperty" value="o">普通[默认]
						<input type="radio" id="rad2" style="width: 27px;" name="articleProperty" value="c">推荐[c]
						<input type="radio" id="rad3" style="width: 27px;" name="articleProperty" value="f">幻灯[f]
	   				</div>
					<div class="underContentSpace"></div>
					<!-- <div class="author">文章类型：<input class="articletitle" type="text" readonly id="artilce_type" name="artilce_type" data-options="field:'articleType',required:true"/></div> -->
		   			<div><input type="hidden" id="article_editId" name="article_editId"></div>
					<div><input type="hidden" id="article_edit_articleTypeId" name="article_edit_articleTypeId"></div>
					<div class="smallSpace"></div>
					<div class="demo-info">
		   				<div class="type">文章栏目：</div>
						<input id="articleType" style="width:252px; _width:270px;"  name="articleType" class="easyui-combotree select" data-options="url:'${base_url}/admin/treeArticle?type=json'"/>
					</div>
					
					<div class="smallSpace"></div>
					<div class="height">
						<ul style="height:23px;">
			    			<li class="name">缩略图：</li>
			    			<li class="select"><input id="fileupload" type="file" name="files[]" data-options="required:true" multiple/></li>
			    		</ul>
		    		</div>
	    		</div>
	  		
	  			<!-- 显示所上传的缩略图 -->
	    		<div id="article_right"><img class="img" id="imgUp" src=""/><input type="hidden" id="imgUploadArticle" name="article_img_save" value=""/></div>
	    		<div id="files" class="files"></div>
			    		
				<div class="smallSpace2"></div>
	  			<div class="content">内容：</div>
	   			<div class="underContentSpace"></div>
				<textarea name="articleEditContentSave" id="articleEditContentSave"  style="width:100%; _width:1109px; height:400px;"></textarea>
				<div class="space"></div>
				
				<div class="author">
					<ul>
						<li class="left">作者：<input class="easyui-validatebox title" type="text" name="articleEdit_author" id="articleEdit_author"/></li>
	   				</ul>
				</div>	
				<div class="space"></div>
				<div class="author">
					<ul>
						<li class="left">来源：<input class="title" type="text" style="width: 420px;" name="artilceEdit_source" id="artilceEdit_source" /></li>
					</ul>
				</div>	
				<div class="underContentSpace"></div>
				
		   		<div class="editFoot">
		   			<div class="button">
				 		<a href="javascript:void(0)" class="easyui-linkbutton l-btn left" onclick="submitEdit(this)">确定</a>
				 		<a href="javascript:void(0)" class="easyui-linkbutton l-btn right" onclick="clearForm('#contentEditSave')">重置</a>
					</div>
				</div>
	  		 </form>  
		</div> 
	</body>
</html>	
   <script type="text/javascript">
   var editor;
   /*点击上传缩略图时请求UploadImgServlet,并将文件保存路径返回*/
		$('#fileupload').fileupload({
			url:BASE_URL+"/uploadImg",
			dataType: 'json',
	    	done: function (e, data) {
	            $("#article_right img").attr("src",BASE_URL + data.result.files.url);
	            $("#article_right input").attr("value",data.result.files.url);
	        }
		});
		
		var radio;
	$(function(){
		var article_id = ${articleId};
		$.get("${base_url}admin/articleLoad?articleId="+article_id+"&t="+new Date().getTime(), function(data){
		 	$('#contentEditSave').form('load',{
					articleEdit_title:data.articleTitle,
					articleEdit_author:data.articleAuthor,
					artilceEdit_source:data.articleSource,
					artilce_type:data.articleTypes.text,
					article_editId:data.articleId,
					article_img:data.articleImg
					
			}); 
			
			$('#articleType').combotree('setValue',data.articleTypes.id);
			$("#article_right img").attr("src",BASE_URL + data.articleImg);
			$("#article_right input").attr("value",data.articleImg);
			$("#article_edit_articleTypeId").val(data.articleTypes.id);
			radio = data.articleFlag;
			checkRadio(radio);
					
			editor.html(data.articleContent);
		},"json");

	});
		
   //单选框选中
   	function checkRadio(data){
	   	var rad1 = document.getElementById("rad1");
	    var rad2 = document.getElementById("rad2");
	    var rad3 = document.getElementById("rad3");
   		if(data==0){
   			rad1.checked=true; 
   		}else if(data==1){
   			rad2.checked=true;
   		}else{
   			rad3.checked=true;
   		}
   	}

   
	
  	var temp = 0;//全局变量 0代表不为封面 1 为封面 2 频道栏目列表 
  		$('#articleType').combotree({
  			onClick: function(){
				//获取文章类型 
				var  art = $('#articleType').combotree('getValue');
				//利用隐藏输入框接收文章类型
				$('#article_edit_articleTypeId').val(art);
				var articTypeId = $('#article_edit_articleTypeId').val();
				//获取栏目名称 用于判断是否含有"(封面)","(频道列表)"等字段 
				var n = $('#articleType').combotree('tree');
				var b =n.tree('getSelected');
				var text = b.text;
				if(text.indexOf("(封面)")!=-1){
					temp=1;
					showFormMsg(SYSTEM_TITLE,"此类型为封面模版只存在一篇文章，可以通过栏目管理进行相应的操作!");
				}else if(text.indexOf("(频道栏目列表)")!=-1){
					temp=2;
					showFormMsg(SYSTEM_TITLE,"此类型为频道栏目列表不可以增加文章!");	
				}else{
					temp=0;
				}
			}
		});
		
		KindEditor.ready(function(K) {
				editor = K.create('#articleEditContentSave', {
					uploadJson : "${base_url}/editor/upload/upload_json.jsp",
					fileManagerJson : "${base_url}/upload/file_manager_json.jsp",
					allowFileManager : true
				});
		});
  
  		//提交数据form表单 
  		function submitEdit(obj){
  				//把编辑框里的内容赋值到 textarea进行赋值 
				$("#articleEditContentSave").val(editor.html());
				var artTitle = $('#articleEdit_title').val().replace(/[ ]/g,""); 
				var artType = $('#article_edit_articleTypeId').val().replace(/[ ]/g,"");
				var artContent = $("#articleEditContentSave").val().replace(/[ ]/g,"");
				if(temp == 0){
					if(artTitle==""||artTitle.length>180){
						showFormMsg(SYSTEM_TITLE,"文章标题不为空，且长度在180个字符以内!");
					}else if(artType==""||artType==null){
						showFormMsg(SYSTEM_TITLE,"请先选择文章的栏目!");
					}else if(artContent==""){
						showFormMsg(SYSTEM_TITLE,"文章内容不为空!");
					}else{
						$.post("${base_url}/articleEdit", $("#contentEditSave").serialize());
						showMessagerCenter(SYSTEM_TITLE,"文章修改成功，点击文章管理的刷新按钮可见 !");
					}
				}else if(temp==2){
					showFormMsg(SYSTEM_TITLE,"此类型为频道栏目列表不可以增加文章!");
				}else if(temp==1){
					showFormMsg(SYSTEM_TITLE,"此类型为封面模版只存在一篇文章，可以通过栏目管理进行相应的操作!");
				}	
				$('#articleList').datagrid('reload'); 
  		}
  		
	
	</script>
