<#--  添加问题 集合-->
<@function>
	<#--  返回Map<String,Object>类型 -->
	<#assign returnValue = questionRelated_addQuestion_collection>
	
	<#--  全局允许提交问题 -->
	<#if returnValue['allowQuestion'] == true>
	
		<!-- 问题编辑器允许使用标签 -->
		<input type="hidden" id="questionAvailableTag"  value="${returnValue['availableTag']}">
		<!-- 文件系统 -->
		<input type="hidden" id="fileSystem"  value="${returnValue['fileSystem']}">
			
		<div class="question-form">
			<form id="addQuestionForm" action="user/control/question/add?page=${url_page}" method="post">
				<#-- 提交问题最多可选择标签数量 -->
				<input type="hidden" id="maxQuestionTagQuantity" value="${returnValue['maxQuestionTagQuantity']}">
				
				<#-- 提交后跳转参数  参数必须经过encodeBase64URL编码后才能提交 不填写时默认跳转回当前页 -->
				<input type="hidden" id="jumpUrl" name="jumpUrl" value="">
				
				<div class="item">
					<input type="text" id="title" name="title" class="formText" size="90" value="${question.title}" alt="标题" title="标题" placeholder="标题"/>
					<span id="${identificationNumber}_title_error" class="error" name="error">${(error['title'])!}</span>
				</div>
				<#if returnValue['questionRewardAmountMax'] == null ||  (returnValue['questionRewardAmountMax'] != null && returnValue['questionRewardAmountMax'] gt 0)>	
				<div class="item">
					<input type="text" id="amount" name="amount" class="formText" size="10" maxlength="8" value="${question.amount}" alt="悬赏金额" title="悬赏金额" placeholder="悬赏金额"/><span class="unit">元</span>
					<span id="${identificationNumber}_amount_error" class="error" name="error">${(error['amount'])!}</span>
					<span class="help" >
						<#if returnValue['questionRewardAmountMin'] gt 0>	
							悬赏金额不能少于 ${returnValue['questionRewardAmountMin']} 元；
						</#if>	
						<#if returnValue['questionRewardAmountMax'] != null && returnValue['questionRewardAmountMax'] gt 0>	
							悬赏金额不能超过${returnValue['questionRewardAmountMax']} 元；
						</#if>	
						用户共有金额 ${returnValue['maxDeposit']} 元
					</span>
				</div>
				</#if>
					
				<#if returnValue['questionRewardPointMax'] == null ||  (returnValue['questionRewardPointMax'] != null && returnValue['questionRewardPointMax'] gt 0)>		
				<div class="item">
					<input type="text" id="point" name="point" class="formText" size="10" maxlength="8" value="${question.amount}" alt="悬赏积分" title="悬赏积分" placeholder="悬赏积分"/><span class="unit">积分</span>
					<span id="${identificationNumber}_point_error" class="error" name="error">${(error['point'])!}</span>
					<span class="help" >
						<#if returnValue['questionRewardPointMin'] gt 0>	
							悬赏积分不能少于${returnValue['questionRewardPointMin']}；
						</#if>	
						<#if returnValue['questionRewardPointMax'] gt 0>	
							悬赏积分不能超过${returnValue['questionRewardPointMax']}；
						</#if>	
						用户共有积分 ${returnValue['maxPoint']}
					</span>
				</div>
				</#if>	
				<div class="questionTag">
					<#list question.questionTagAssociationList as questionTagAssociation>
		    			<a class="btn mr" data-tagId="${questionTagAssociation.questionTagId}" href="javascript:;">${questionTagAssociation.questionTagName}<span class="ml">×</span><input type="hidden" name="tagId" value="${questionTagAssociation.questionTagId}"></a>
		    	 	</#list>
					<div class="addTag">
						<button type="button" class="btn" id="add-questionTag-btn" onClick="javaScript:showTagDiv();">+ 添加标签(<span id="currentQuestionTagQuantity">0</span>/${returnValue['maxQuestionTagQuantity']})</button>
					</div>
					<span id="${identificationNumber}_tagId_error" class="error" name="error">${(error['tagId'])!}</span>
				</div>

				<p><textarea class="textarea" onfocus="" id="questionContent" name="content" >${question.content}</textarea></p>
				<span id="${identificationNumber}_content_error" class="error" name="error">${(error['content'])!}</span> 

				<span id="${identificationNumber}_question_error" class="error" name="error">${error['question']}</span>
				<div class="form-action">
					<#-- 获取令牌 -->
					<input type="hidden" id="token" name="token" value="${token!}">
					
					<div class="questionSubmit">
						<!-- 
			        	<span class="submitButton"><INPUT type="button" value="提交" onClick="javascript:sureSubmit(this.form)"></span> -->
			        	<span class="submitButton"><INPUT id="${identificationNumber}_submitForm"  type="button" value="提交" onClick="javascript:ajax_sureSubmit('${identificationNumber}')"></span>
			        </div>
					
					
					<#-- identificationNumber:识别号：用来区别每次请求 -->
					<#-- 验证码 -->
					<#if returnValue['captchaKey'] != "">	
					<div class="captchaModule">
						<div class="captcha_div">
							<div class="captchaBox">
								<input type="hidden" id="${identificationNumber}_captchaKey" name="captchaKey" value="${(returnValue['captchaKey'])!}">
								<input id="${identificationNumber}_captchaValue" name="captchaValue" type="text" maxLength="5" autocomplete="off" tabindex="3" onfocus="inputBoxTip_click(this);" onblur="inputBoxTip_leave('${identificationNumber}',this);"/>
								<label class="promptTag" id="${identificationNumber}_captchaValueBoxTip"  for="captchaValue">验证码</label>
							</div>
							<a class="captchaImage" href="javascript:void(0);" >
								<img id="${identificationNumber}_captcha" src="captcha/${(returnValue['captchaKey'])!}.jpg" onClick="replaceCaptcha('${identificationNumber}');" tabindex="4" />
							</a>
							<a  href="javascript:void(0);" onClick="replaceCaptcha('${identificationNumber}');" tabindex="5">换一幅</a>
						</div>
						<p class="tips">
							
							<#-- 验证码错误 -->
							<span id="${identificationNumber}_captchaValue_error" name="error">${(error['captchaValue'])!}</span>
							
						</p>
					</div>
					</#if>
				</div>
				
				
				
				
				<script language="javascript" type="text/javascript">
					//弹出查看标签层
					function showTagDiv(){	
						get_request(function(value){
							if(value != ""){
								var nav_html = "";
								var childTag_html = "";
					
								var returnValue = JSON.parse(value);//JSON转为对象
								for(var i =0; i<returnValue.length; i++){
									var questionTag = returnValue[i];
					
									nav_html += "<li class='nav-item'>";
									nav_html += 	"<a class='nav-link' id='tag-"+i+"' childTagQuantity='"+questionTag.childTag.length+"' tagId='"+questionTag.id+"' tagName = '"+_.escape(questionTag.name)+"' href='javascript:;'>"+_.escape(questionTag.name)+"</a>";
									nav_html += "</li>";
									
									childTag_html += "<div class='tab-pane' id='tag-"+i+"-panel' >";
									for(var j =0; j<questionTag.childTag.length; j++){
										var childQuestionTag = questionTag.childTag[j];
										childTag_html += "<a class='child-tag' href='javascript:;' tagId='"+childQuestionTag.id+"' tagName = '"+_.escape(childQuestionTag.name)+"'>"+_.escape(childQuestionTag.name)+"</a>";
									}
									childTag_html += "</div>";
								}
								
								var html = "";
								html += "<div class='questionTagNavigation'>";
								html += 	"<ul class='nav'>";
								html += 		nav_html;
								html += 	"</ul>";
								html += 	"<div class='tab-content'>";
								html += 		childTag_html;
								html += 	"</div>";
								html += "</div>";
					
								layer.open({
									type: 4,//4: tips层
									content: [html, '#add-questionTag-btn'], //数组第二项即吸附元素选择器或者DOM
								//	closeBtn:0,//0不显示关闭按钮
						    	 	area: '500px',//宽，高度自适应
									tips: [3, '#f6f6f6'],
									success: function(layero, index){//层弹出后的成功回调方法
										//选择标签
										selectTag(index);
					
										//点击空白关闭弹窗
										$(document).mouseup(function(e){
											var _con = $(layero.selector);   // 设置目标区域
											if(!_con.is(e.target) && _con.has(e.target).length === 0){
										    	//关闭窗口
										    	layer.close(index);
											}
										});
									}
								
								});
							}else{
								layer.msg('还没有问题标签', 
									{
									  time: 3000 //3秒关闭（如果不配置，默认是3秒）
									},function(){
										//关闭后的操作
										
									}
								);
							}
						},
							 "queryAllQuestionTag?timestamp=" + new Date().getTime(), true);
					}
					
					//最后一次选中的根节点
					var lastSelectedRootNode = "";
					//选择标签
					function selectTag(index){
						
						if(lastSelectedRootNode == ""){
							//第一次打开选第一项
							$(".nav-link:first").addClass("active");
							$(".tab-pane:first").addClass("active");
						}else{//选中上一次打开的节点
							$(".nav-link").removeClass("active"); 
							$(".tab-pane").removeClass("active"); 
							$("#"+lastSelectedRootNode).addClass("active");
							var panelId = lastSelectedRootNode + "-panel";
						 	$("#"+panelId).addClass("active");
						}
					
						
						
						
					
						//显示选中的标签
						$(".nav-link").on('click',function(e){
							e.preventDefault();
							
							
							var maxQuestionTagQuantity = document.getElementById("maxQuestionTagQuantity").value;
							var tagId = document.getElementsByName("tagId");
							if(tagId != null && tagId.length>=maxQuestionTagQuantity){//如果超过允许选择的最大标签数量
								//需引入layer
		        				layer.msg('已超过允许选择的最大标签数量', 
									{
									  time: 3000 //3秒关闭（如果不配置，默认是3秒）
									},function(){//关闭后的操作
										
									}
								);
								return;
							}
						
							
							
							
							$(".nav-link").removeClass("active"); 
							$(".tab-pane").removeClass("active"); 
						 	var panelId = $(this).attr("id") + "-panel";
						 	$(this).addClass("active");
						 	$("#"+panelId).addClass("active");
						 	
						 	lastSelectedRootNode = $(this).attr("id");//设置最后一次选中的根节点
						 	
						 	//子标签数量
						 	var childTagQuantity = $(this).attr("childTagQuantity");
						 	if(parseInt(childTagQuantity) ==0){//如果没有子标签则选择当前标签
						 		var tagId = $(this).attr("tagId");
								var tagName = $(this).attr("tagName");
								
								var html = "";
									html += "<a class='btn mr' data-tagId='"+tagId+"' href='javascript:;'>";
									html += 	_.escape(tagName);
									html += 	"<span class='ml'>×</span>";
									html += 	"<input type='hidden' name='tagId' value='"+tagId+"'>";
									html += "</a>";
									
								//如果标签已选中	
								var tag = $(".questionTag a[data-tagId='"+tagId+"']");
								
								if(tag.length > 0){//已选中	
									//重复选中时显示闪烁动画
									tag.addClass("warning");//显示动画
									setTimeout(function() {
										tag.removeClass("warning");//删除动画
									}, 2000);
									
									
								}else{//未选中	
									$(".addTag").before(html);
									//动态添加元素时设置删除标签事件
									$(".questionTag a[data-tagId='"+tagId+"']").find("span").on('click',function(e){
										e.preventDefault();
										$(this).parent().remove();
										
										var tagIdQuantity =$("input[name='tagId']").size();//tagId标签数量
										$("#currentQuestionTagQuantity").text(tagIdQuantity);
									});
									
									layer.close(index); //关闭tips层 
									var tagIdQuantity =$("input[name='tagId']").size();//tagId标签数量
									$("#currentQuestionTagQuantity").text(tagIdQuantity);
								}
						 	}
						 	
						});
						
						//选择标签
						$(".child-tag").on('click',function(e){
							e.preventDefault();
							
							var maxQuestionTagQuantity = document.getElementById("maxQuestionTagQuantity").value;
							var tagId = document.getElementsByName("tagId");
							if(tagId != null && tagId.length>=maxQuestionTagQuantity){//如果超过允许选择的最大标签数量
								//需引入layer
		        				layer.msg('已超过允许选择的最大标签数量', 
									{
									  time: 3000 //3秒关闭（如果不配置，默认是3秒）
									},function(){//关闭后的操作
										
									}
								);
								return;
							}
							
							
							var tagId = $(this).attr("tagId");
							var tagName = $(this).attr("tagName");
							
							var html = "";
								html += "<a class='btn mr' data-tagId='"+tagId+"' href='javascript:;'>";
								html += 	_.escape(tagName);
								html += 	"<span class='ml'>×</span>";
								html += 	"<input type='hidden' name='tagId' value='"+tagId+"'>";
								html += "</a>";
								
							//如果标签已选中	
							var tag = $(".questionTag a[data-tagId='"+tagId+"']");
							
							if(tag.length > 0){//已选中	
								//重复选中时显示闪烁动画
								tag.addClass("warning");//显示动画
								setTimeout(function() {
									tag.removeClass("warning");//删除动画
								}, 2000);
								
								
							}else{//未选中	
								$(".addTag").before(html);
								//动态添加元素时设置删除标签事件
								$(".questionTag a[data-tagId='"+tagId+"']").find("span").on('click',function(e){
									e.preventDefault();
									$(this).parent().remove();
									
									var tagIdQuantity =$("input[name='tagId']").size();//tagId标签数量
									$("#currentQuestionTagQuantity").text(tagIdQuantity);
								});
								
								layer.close(index); //关闭tips层 
								
								var tagIdQuantity =$("input[name='tagId']").size();//tagId标签数量
								$("#currentQuestionTagQuantity").text(tagIdQuantity);
								
							}
					
						});
					}
					
					
					
					
					
					
					$(document).ready(function(){
						//回显页面时设置删除标签事件
						$('.questionTag span').click(function(){
							//var tagId = $(this).parent().attr("data-tag-id");
							$(this).parent().remove();
						});
					});

				</script>
				
				
				
				<script language="javascript" type="text/javascript">
					//点击输入框提示 
					function inputBoxTip_click(obj){
						document.getElementById(obj.id+"BoxTip").style.display="none";
					
					}
					//离开输入框提示
					function inputBoxTip_leave(identificationNumber,obj){
						if(obj.value != ""){
							document.getElementById(obj.id+"BoxTip").style.display="none";
						}else{
							document.getElementById(obj.id+"BoxTip").style.display="";
						}
						verificationCode(identificationNumber);
					}
					
					//校对验证码 identificationNumber:识别号：用来区别每次请求
					function verificationCode(identificationNumber){
						var flag = true;
						if( document.getElementById(identificationNumber+"_"+"captchaKey") != null){
							
							var captchaKey = document.getElementById(identificationNumber+"_"+"captchaKey").value;
							var parameter = document.getElementById(identificationNumber+"_"+"captchaValue").value;
							if(parameter != ""){
							
								var parameter_trim = trim(parameter);
								if(parameter_trim != ""){
									//ajax使用同步提交
									get_request(function(value){
						            	if(value == "false"){
						            	
							            	document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "验证码错误";
							            	flag = false;
						            	}
						            },
								 		"checkCaptcha?captchaKey="+captchaKey+"&captchaValue="+parameter_trim+"&timestamp=" + new Date().getTime(), false);
						            	
								}else{
									document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "验证码不能为空";
									flag = false;
								}
							}else{
								document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "验证码不能为空";
								flag = false;
							}
							if(flag == true){
								document.getElementById(identificationNumber+"_"+"captchaValue_error").innerHTML = "";
							}
						}
					
						return flag;
					}
					//更换验证码
					function replaceCaptcha(identificationNumber){
						var captchaKey = document.getElementById(identificationNumber+"_"+"captchaKey").value;
						document.getElementById(identificationNumber+"_"+"captcha").src = "captcha/"+captchaKey+".jpg?" + Math.random(); 
					}
					//解决提交按钮的click和富文本的blur事件冲突
					$(document).ready(function(){
						$("input[type='button']").mousedown(function(e){
					    	e.preventDefault();
						});
					});
					//表单Form方式提交
					function sureSubmit(obj){
						obj.submit();
					} 
					//Ajax方式提交
					function ajax_sureSubmit(identificationNumber){
						//按钮设置 disabled="disabled"
						document.getElementById(identificationNumber+"_submitForm").disabled=true;
						//清空所有错误提示
						var error_tag = getElementsByName_pseudo("span", "error");
						for(var i = 0;i < error_tag.length; i++) {
							error_tag[i].innerHTML="";
						
						}
						
						
						if(verificationCode() == true){
						
							var parameter = "";

							//获取令牌
							parameter += "&token="+document.getElementById("token").value;
							
							//获取验证码key
							var captchaKey = document.getElementById(identificationNumber+"_"+"captchaKey");
							if(captchaKey != null){
								parameter += "&captchaKey="+captchaKey.value;
							}
							
							//获取验证码value
							var captchaValue = document.getElementById(identificationNumber+"_"+"captchaValue");
							if(captchaValue != null){
								parameter += "&captchaValue="+captchaValue.value;
							}
							
							
							//标签
							var tagId = document.getElementsByName("tagId");
							if(tagId != null){
								for(var i =0; i<tagId.length; i++){
									//获取标签
									parameter += "&tagId="+encodeURIComponent(tagId[i].value);
								}
							}
							
							//获取标题
							var title = document.getElementById("title").value;
							parameter += "&title="+encodeURIComponent(title);

							//获取内容
							var content = document.getElementById("questionContent").value;
							parameter += "&content="+encodeURIComponent(content);
						
							//悬赏金额
							var amount = document.getElementById("amount");
							if(amount != null){
								parameter += "&amount="+encodeURIComponent(amount.value);
							}
							
							
							//悬赏积分
							var point = document.getElementById("point");
							if(point != null){
								parameter += "&point="+encodeURIComponent(point.value);
							}
							
						
						   	//删除第一个&号,防止因为多了&号而出现警告: Parameters: Invalid chunk ignored.信息
							if(parameter.indexOf("&") == 0){
								parameter = parameter.substring(1,parameter.length);
							}
						   	post_request(function(value){
								var data = JSON.parse(value);
								for(var returnValue in data){
									
									if(returnValue == "success"){
					        			if(data[returnValue] == "true"){
					        				//需引入layer
					        				layer.msg('提交成功,3秒后自动跳转到问题列表页', 
												{
												  time: 3000 //3秒关闭（如果不配置，默认是3秒）
												},function(){//关闭后的操作
													//跳转页面
					        						window.location.href="askList"; 
												}
											);
					        				
					        			}
					        		}else if(returnValue == "error"){
					        			
					        			var error = data[returnValue];
					        			for(var key in error){
					        				var error_html = document.getElementById(identificationNumber+"_"+key+"_error");
					        	
					        				if(error_html != null){
					        					error_html.innerHTML = error[key];
					        				}
					        				
					        			}
					        			//按钮设置 disabled="disabled"
										document.getElementById(identificationNumber+"_submitForm").disabled=false;
					        		}else if(returnValue == "captchaKey"){//显示验证码
			        					
										var captchaKey = data[returnValue];
										if(captchaKey != ""){
											//设置验证码Key
											if(document.getElementById(identificationNumber+"_"+"captchaKey")!= null){
			
												document.getElementById(identificationNumber+"_"+"captchaKey").value = captchaKey;
												//设置验证码图片
												replaceCaptcha(identificationNumber);
											}	
										}
									}
					        	}
					        	
							},
								"user/control/question/add?timestamp=" + new Date().getTime(), true,parameter);
						}else{
							//按钮设置 disabled="disabled"
							document.getElementById(identificationNumber+"_submitForm").disabled=false;
						}
					} 
					
	
					// 指定编辑器iframe document的CSS数据，用于设置可视化区域的样式。 单冒号(:)用于CSS3伪类，双冒号(::)用于CSS3伪元素。伪元素由双冒号和伪元素名称组成。双冒号是在当前规范中引入的，用于区分伪类和伪元素。但是伪类兼容现存样式，浏览器需要同时支持旧的伪类，比如:first-line、:first-letter、:before、:after等
				    KindEditor.options.cssData = ".ke-content .prettyprint {"+
						"background:#f8f8f8;"+
						"border:1px solid #ddd;"+
						"padding:5px;"+
					"}"+//默认字体大小
					"body {"+
						"font-size: 14px;"+
					"}";
				
				   
					var questionAvailableTag = document.getElementById("questionAvailableTag").value;
					var questionAvailableTag_obj = null;//['source', '|','fontname','fontsize','emoticons'];
					if(questionAvailableTag != ""){
						questionAvailableTag_obj = JSON.parse(questionAvailableTag);//JSON转为对象
						if(questionAvailableTag_obj != null && questionAvailableTag_obj.length >0){
							for(var i=0; i< questionAvailableTag_obj.length; i++){
								var availableTag = questionAvailableTag_obj[i];
								if(availableTag == "image"){//增加批量插入图片按钮
									questionAvailableTag_obj.splice(i+1, 0, 'multiimage');//在指定索引处插入元素
									break;
								}
							}
						}
					}
					var editor;
					KindEditor.ready(function(K) {
						var fileSystem = document.getElementById("fileSystem").value;
						editor = K.create('textarea[id="questionContent"]', {
							themeType : 'style :minimalist customization',//极简主题 加冒号的是主题样式文件名称同时也是主题目录
							basePath : '${commonPath}js/kindeditor/',//指定编辑器的根目录路径
							formatUploadUrl :false,//false时不会自动格式化上传后的URL
							resizeType : 1,//2或1或0，2时可以拖动改变宽度和高度，1时只能改变高度，0时不能拖动。默认值: 2 
							autoHeightMode : true,//自适应高度
							allowPreviewEmoticons : true,//true或false，true时鼠标放在表情上可以预览表情
							allowImageUpload : true,//true时显示图片上传按钮
							uploadModule : parseInt(fileSystem),//上传模块 0.本地 10.SeaweedFS 20.MinIO 30.阿里云OSS
							uploadJson :'user/control/question/upload?method=upload',//指定浏览远程图片的服务器端程序
							filePostName:'file',//文件上传字段 默认imgFile
							fixToolBar : true,//浮动工具栏
							items : questionAvailableTag_obj,
							
							/**['source', 'fontsize', 'forecolor', 'hilitecolor', 'bold',
						        'italic', 'underline',  '|', 'image', 'multiimage',
						        'justifyleft','justifycenter','justifyright', 'insertorderedlist', 'insertunorderedlist',   'emoticons',
						         'link', 'unlink','removeformat','hide'],**/
							afterChange : function() {
								this.sync();
							},
							afterBlur:function(){
								this.sync();
							}
					
						});
					});
					</script>
				
			</form>
		</div>
	<#else>
		<div class="addQuestion-disableFormModule">
			<div class="disableForm-wrap">
				<div class="respond">
					<p>全站关闭提问</p>
				</div>
			</div>
		</div>
	</#if>
</@function>



