#{param name="taskService"/}
#{param name="userService"/}
#{param name="filterLabel"/}
#{param name="status"/}
#{param name="filterStatus"/}
#{param name="filterAssignee"/}
#{param name="filterDueDate"/}
#{param name=bundle /}
#{param name=filterData /}

<%
	import org.exoplatform.task.util.TaskUtil;
   import org.exoplatform.task.util.ResourceUtil;
	import org.exoplatform.task.domain.Priority;
	import org.exoplatform.commons.utils.HTMLEntityEncoder;
	import org.json.JSONArray;
	import org.json.JSONObject;

	def encoder = HTMLEntityEncoder.getInstance();
	
	JSONArray buildJSON(def labels) {
    def encoder = HTMLEntityEncoder.getInstance();
    def jsons = new JSONArray();
    for (def lbl : labels) {
      def obj = new JSONObject();
      obj.put("id", lbl.getId());
      obj.put("name", encoder.encode(lbl.getName()));
      obj.put("color", lbl.getColor());
      jsons.put(obj);
    }
    return jsons;
  }
%>

<div class="taskFilter uiBox" data-projectid="0" data-canedit="0">
    <h5 class="title">
        <span>&{label.filter}</span>
        <a class="actionIcon hideFilter pull-right" href="#"><i class="uiIconClose"></i></a>
    </h5>
    <div class="uiContentBox">        
      <div class="form-horizontal">		   
		   <div class="form-horizontal">
		  	<div class="control-group">
		  		<label class="control-label" for="keyword">&{filter.task.contains}</label>
		  		<div class="controls">
				   <input type="text" id="keyword" name="keyword" value="${filterData.getKeyword()}" maxlength="100"/>
		  		</div>
		  	</div>
		  	<%if (filterLabel) { %>
		  	<div class="control-group">
		  		<label class="control-label" for="label">&{label.label}</label>
		  		<div class="controls">
		  		<%
			  		def selectedLabels = filterData.getLabel();
			      def labels = new LinkedList();
			      for (def id : selectedLabels) {
			        def label = taskService.getLabel(id);
			        if (label != null) {
			          labels.add(label);        
			        }
			      }
			      JSONArray lblArr = buildJSON(labels);
		  		%>
		  			<input type="text" id="label" name="label" value="" data-items="<%= new JSONArray(selectedLabels)%>" maxlength="100"
		  				data-options='${lblArr}'/>
		  		</div>
		  	</div>
		  	<%} %>
		  	<%if (filterStatus) { %>
		  	<div class="control-group">
		  		<label class="control-label" for="status">&{label.status}</label>
		  		<div class="controls">
		  			<select name="status">
		  				<option value="">&{filter.task.all}</option>
		  				<%
		  				for (def st : status) { 		  				  
		  				%>
		  				<option value="${st.id}" <%=filterData.getStatus() != null && filterData.getStatus() == st.id ? "selected" : ""%>>
		  					${encoder.encode(ResourceUtil.resolveStatus(bundle, st.name))}
		  				</option>
		  				<%} %>			
		  			</select>
		  		</div>
		  	</div>
		  	<%}%>
		  	<%if (filterAssignee) { %>
		  	<div class="control-group">
		  		<label class="control-label" for="assignee">&{label.assignee}</label>
		  		<div class="controls">
		  		<%		  		
		  			def selectedAssignees = filterData.getAssignee();
	  		      def userArr = new JSONArray();
		  		   for (def userId : selectedAssignees) {
		  		     def user = userService.loadUser(userId);
		  		     if (user != null && !user.getUsername().equals("guest")) {
			  		     def juser = new JSONObject();
			  		     juser.put("id", userId);
			  		     juser.put("text", user.getDisplayName());
			  		     userArr.put(juser);
		  		     }
		  		   }
		  		%>
		  			<input type="text" id="assignee" name="assignee" value="" data-options='${userArr}' data-items='<%=new JSONArray(selectedAssignees) %>' maxlength="100"/>
		  		</div>
		  	</div>
		  	<%} %>
		  	<%if (filterDueDate) { %>
		  	<div class="control-group">
		  		<label class="control-label" for="due">&{filter.task.due}</label>
		  		<div class="controls">
		  			<select name="due">
		  				<option value="">&{filter.task.all}</option>
		  				<%for (def due : TaskUtil.DUE.values()) {%>		  				
		  				<option value="<%=due.name()%>" <%=due.equals(filterData.getDue()) ? "selected" : ""%>><%=bundle.getString('label.' + due.name().toLowerCase())%></option>
		  				<%} %>
		  			</select>
		  		</div>
		  	</div>
		  	<%} %>
		  	<div class="control-group">
		  		<label class="control-label" for="priority">&{label.priority}</label>
		  		<div class="controls">
		  			<select name="priority">
		  				<option value="">&{filter.task.all}</option>
		  				<%for (def priority : Priority.values()) {%>										  					  				
		  				<option value="<%=priority.name()%>" <%=priority.equals(filterData.getPriority()) ? "selected" : ""%>><%=bundle.getString('label.priority.' + priority.name().toLowerCase())%></option>
		  				<%} %>
		  			</select>
		  		</div>
		  	</div>
		  	<div class="control-group">
		  		<label class="control-label" for="showCompleted"></label>
		  		<div class="controls">
		  			<div class="checkBoxRepeat" id="IsEmailRepeatEventReminderTab">
						<label class="uiCheckbox">
						<input type="checkbox" name="showCompleted" class="checkbox" id="showCompleted" <%=filterData.isShowCompleted() ? "checked" : ""%>/>
						<span>&{filter.task.showCompleted}.</span></label>
					</div>
		  		</div>
		  	</div>
		  </div>
  	  </div>
    </div>
</div>