<%@page import="br.mp.mprj.portal.portlet.gestaomensagens.search.UserSearch"%>
<%@ include file="/jsp/init.jsp" %>

<liferay-portlet:renderURL var="pesquisaURL" windowState="<%=LiferayWindowState.POP_UP.toString()%>">
	<liferay-portlet:param name="mvcPath" value="/jsp/admin/destinatario/user-popup.jsp"/>
</liferay-portlet:renderURL>

<aui:form action="${pesquisaURL}" method="post" name="fmSearch" cssClass="search-terms">
	<aui:input label="lista.usuario.nome" name="fullName" size="50" value="${param.fullName}" type="text" inlineField="true" />
	<aui:input label="lista.usuario.email" name="emailAddress" size="50" value="${param.emailAddress}" type="text" inlineField="true" />
	<aui:input type="hidden" name="tipoSelecao" value="${param.tipoSelecao}" />
	<aui:button type="submit" value="search"/>
</aui:form>

<div class="separator"></div>

<%
	PortletURL portletURL = renderResponse.createRenderURL();
	portletURL.setParameter("fullName", ParamUtil.getString(request, "fullName"));
	portletURL.setParameter("emailAddress", ParamUtil.getString(request, "emailAddress"));
	portletURL.setParameter("tipoSelecao", ParamUtil.getString(request, "tipoSelecao"));
	portletURL.setParameter("mvcPath", "/jsp/admin/destinatario/user-popup.jsp");
	portletURL.setWindowState(LiferayWindowState.POP_UP);
	
%>

<aui:form name="fmUser" cssClass="search-results">
	
	<liferay-ui:search-container
		emptyResultsMessage="search.empty.usuarios"
		delta="20" id="listaUsuarios"
	    deltaConfigurable="true"
	    iteratorURL="<%=portletURL%>"
	    rowChecker="<%= new RowChecker(renderResponse) %>">
	    
	     <liferay-ui:search-container-results>
			<%
				Map<Integer , List<UserVO>> retorno = UserSearch.pesquisar(renderRequest, themeDisplay.getCompanyId(),
						searchContainer.getStart() , searchContainer.getEnd() ,  ParamUtil.getString(request, "orderByCol"));
				java.util.Map.Entry entry  = retorno.entrySet().iterator().next();	
				List<UserVO> lista = (List<UserVO>) entry.getValue();
				pageContext.setAttribute("results", lista);
				pageContext.setAttribute("total", entry.getKey());
			%>        														
		</liferay-ui:search-container-results>
	  
	    <liferay-ui:search-container-row
	        className="br.mp.mprj.portal.portlet.gestaomensagens.search.vo.UserVO"
	        escapedModel="true"
	        keyProperty="userId"
	        modelVar="users"
	        rowIdProperty="userId"
	    >
	        <aui:input name="userSel_${users.userId}_fullName" type="hidden" value="${users.fullName}"/>
	        <aui:input name="userSel_${users.userId}_emailAddress" type="hidden" value="${users.emailAddress}"/>
	        
	        <liferay-ui:search-container-column-text
	            name="Nome"
	            property="fullName"
	        />
	        
	        <liferay-ui:search-container-column-text
	            name="Email"
	            property="emailAddress"
	        />
	        
	    </liferay-ui:search-container-row>
	    
	    <liferay-ui:search-iterator />
	    
	</liferay-ui:search-container>
	
	<aui:button cssClass="btn-primary"
	            type="button"
	            value="atualizar.usuario.associacao"
	            onClick="javascript:selecionarUsuarios()"/>

</aui:form>


<aui:script use="node">
	Liferay.provide(
	        window,
	        'selecionarUsuarios',
	        function() {
	        	var tipoSelecao = '<%=ParamUtil.getString(request, "tipoSelecao")%>';                                              
				var idsSel = Liferay.Util.listChecked(document.<portlet:namespace/>fmUser).split(',');
				var rowAdd = [];
				for (var i = 0; i < idsSel.length; i++) {
					var id = idsSel[i];
					if (id == 'on' || id == '') continue;
					var fullName = A.one("#<portlet:namespace/>userSel_" + id + "_fullName").val();
					var emailAddress = A.one("#<portlet:namespace/>userSel_" + id + "_emailAddress").val();
					var obj = {
						id: id,
						fullName: fullName,
						emailAddress: emailAddress
					}
					rowAdd.push(obj);
				}
				if (tipoSelecao == 'R'){
					Liferay.Util.getOpener().associarUsuariosResponsaveis(rowAdd);	
				} else if (tipoSelecao == 'M'){
					Liferay.Util.getOpener().associarUsuariosModeracao(rowAdd);
				} else if (tipoSelecao == 'N'){
					Liferay.Util.getOpener().associarUsuariosEmNome(rowAdd);
				}			
				
	        },
	        ['liferay-util-list-fields']
	);
</aui:script>