<%@ include file="/jsp/init.jsp" %>

<%@ include file="/jsp/admin/includes/menu-lateral-principal.jspf" %>

<liferay-portlet:renderURL var="pesquisaURL">
	<liferay-portlet:param name="mvcPath" value="/jsp/admin/destinatario/listagem.jsp"/>
	<liferay-portlet:param name="acao" value="pesquisar"/>
</liferay-portlet:renderURL>

<liferay-portlet:renderURL var="viewURL">
	<liferay-portlet:param name="admin" value="true"/>
	<liferay-portlet:param name="mvcPath" value="/jsp/admin/destinatario/listagem.jsp"/>
</liferay-portlet:renderURL>

<div class="span10 context-pane">
	<div id="selecao-template-wrapper">
		<liferay-ui:success key="destinatario.sucesso-gravar" message="destinatario.sucesso-gravar"/>
		<liferay-ui:success key="destinatario.sucesso-excluir" message="destinatario.sucesso-excluir"/>
		<liferay-ui:success key="destinatario.sucesso-atualizar" message="destinatario.sucesso-atualizar"/>
		<liferay-ui:header title="destinatario.header.listagem"/>
	
	<%
	String orderByCol = ParamUtil.getString(request, "orderByCol");
	String orderByType = ParamUtil.getString(request, "orderByType");
	String sortingOrder = orderByType;
	
	if(orderByType.equals("desc")){
	    orderByType = "asc";
	}else{
	    orderByType = "desc";
	}
	
	String nomeParam = "";
	String tipoListaParam = "";
	
//	String nomeParam = (!"".equals(ParamUtil.getString(request, "nomeListaDestinatarioSearch")) ? ParamUtil.getString(request, "nomeListaDestinatarioSearch") 
	//		: ParamUtil.getString(request, "nome"));
	
	
	if (!"".equals(ParamUtil.getString(request, "nomeListaDestinatarioSearch"))){
		nomeParam = ParamUtil.getString(request, "nomeListaDestinatarioSearch");
	} else if (!"".equals(ParamUtil.getString(request, "nome")) && "".equals(ParamUtil.getString(request, "operacao"))){
		nomeParam = ParamUtil.getString(request, "nome");
	}
	
	if (!"".equals(ParamUtil.getString(request, "tipoLista"))){
		tipoListaParam = ParamUtil.getString(request, "tipoLista");
	} else if (!"".equals(ParamUtil.getString(request, "tipoListaParam")) && "".equals(ParamUtil.getString(request, "operacao"))){
		tipoListaParam = ParamUtil.getString(request, "tipoListaParam");
	} else {
		tipoListaParam = "-1";
	}

	//String nomeParam = ParamUtil.getString(request, "nomeListaDestinatarioSearch");
	
	PortletURL portletURL = renderResponse.createRenderURL();
	portletURL.setParameter("nome", nomeParam);
	portletURL.setParameter("tipoListaParam", tipoListaParam);
	portletURL.setParameter("mvcPath", "/jsp/admin/destinatario/listagem.jsp");
	portletURL.setParameter("redirect", viewURL);
	
%>

<div class="formulario-lista-dados">

<liferay-ui:search-container
	emptyResultsMessage="destinatario.lista.vazia"
    deltaConfigurable="false"
    orderByType="<%=orderByType%>"
    iteratorURL="<%=portletURL%>">
    
    <aui:form name="fmSearchDestinatario" cssClass="search-terms" action="#">
            
                    <aui:input name="nomeListaDestinatarioSearch" 
                            cssClass="label-mensagem-dados"
                            value="${param.nome}" 
                            type="text" 
                            inlineField="false" 
                            inlineLabel="left"
                            label="destinatario.busca.campo.nome"/>
                            
                     <input type="hidden" name="operacao"  id="operacao" value="pesquisa"/>     
                    
                    <label class="control-label"><liferay-ui:message key="lista.tipos"/></label> 
                    
                    
                 		
                 		<% if ("0".equals(tipoListaParam)) {%>
                 		 <span id="radioDestinatario">
                 		   <aui:input checked="true" name="tipoLista" id="tipoDestinatario" value="0" label="destinatario.lista" type="radio" inlineField="true"  inlineLabel="left"/>
                 		 
                 		<% } else {%>
                 			 <span id="radioDestinatario">
                 			<aui:input name="tipoLista" id="tipoDestinatario" value="0" label="destinatario.lista" type="radio" inlineField="true"  inlineLabel="left"/>
                 			 </span>
                 		<% }%>
                 		
                 		<% if ("1".equals(tipoListaParam)) {%>
                 		
                 		
   						<span id="radioConta">
                        <aui:input  checked="true" name="tipoLista" id="contaSaida" value="1" label="destinatario.conta" type="radio" inlineField="true"  inlineLabel="left" >
                        </aui:input>
                    </span>                 		
                 		<% } else {%>
  					 <span id="radioConta">
                        <aui:input  name="tipoLista" id="contaSaida" value="1" label="destinatario.conta" type="radio" inlineField="true"  inlineLabel="left" >
                        </aui:input>
                    </span>                 			
                 		<% }%>
                 		
                      
                             
                 
            
                <div>
                    <br/>
                    <aui:button cssClass="pesquisa" type="submit" value="search" onClick="alert(document.get('_portletgestaomensagens_WAR_portletgestaomensagens_nomeListaDestinatarioSearch').value)"/>&nbsp;
                    <aui:button type="cancel" value="Limpar" onClick="limparFormulario();"/>
                </div>
                    
                        
		 <liferay-ui:search-container-results>
			<%      
				Map<Integer , List<GrupoDestinatario>> retorno = GrupoDestinatarioSearch.pesquisar(renderRequest , themeDisplay.getCompanyId(),
						searchContainer.getStart() , searchContainer.getEnd() , ParamUtil.getString(request, "orderByCol") , false);
				java.util.Map.Entry entry  = retorno.entrySet().iterator().next();	
				List<GrupoDestinatario> lista = (List<GrupoDestinatario>) 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.service.model.GrupoDestinatario"
        escapedModel="true"
        keyProperty="grupoDestinatarioId"
        modelVar="grupoDestinatario"
        rowIdProperty="grupoDestinatarioId">
                
            <liferay-portlet:renderURL varImpl="rowURL">
				<liferay-portlet:param name="mvcPath" value="/jsp/admin/destinatario/formulario.jsp"/>
            	<liferay-portlet:param name="grupoDestinatarioId" value="<%= String.valueOf(grupoDestinatario.getGrupoDestinatarioId()) %>" />
        	</liferay-portlet:renderURL>
        	
        	<liferay-ui:search-container-column-text
          		href="<%= rowURL.toString() %>"
           	 	name="nome_sortable"
           	 	orderableProperty="nome_sortable"
            	orderable="true"
            	property="nome"
            	cssClass="tabela-td-center"
    	    />
        
        	<liferay-ui:search-container-column-text
            	href="<%= rowURL.toString() %>"
            	name="Finalidade"
            	orderableProperty="Finalidade"
            	property="finalidade"
            	cssClass="tabela-td-center"
        	/>
        	
        	<liferay-ui:search-container-column-text
            	href="<%= rowURL.toString() %>"
            	name="Tipo da Lista"
            	orderable="false"
            	property="descricaoTipoLista"
            	cssClass="tabela-td-center"
        	/>
        	
            <liferay-ui:search-container-column-text name="table.header.acoes" align="center" cssClass="tabela-td-center">
	            <liferay-ui:icon-menu>
	            
	                <liferay-ui:icon image="edit" url="<%= rowURL.toString() %>" />
	                
		            <liferay-portlet:actionURL var="deleteURL">
		            
		                <liferay-portlet:param name="<%= ActionRequest.ACTION_NAME %>" value='GrupoDestinatarioExcluir'/>
		                <liferay-portlet:param name="redirect" value="${pesquisaURL}" />
		                <liferay-portlet:param name="grupoDestinatarioId" value="<%= String.valueOf(grupoDestinatario.getGrupoDestinatarioId()) %>" />
						<liferay-portlet:param name="mvcPath" value="/jsp/admin/destinatario/listagem.jsp"/>
		       
		           	</liferay-portlet:actionURL>
		            
					
					<liferay-ui:icon-delete url="${deleteURL}"/>
					
	            </liferay-ui:icon-menu>
            
            </liferay-ui:search-container-column-text>
        
       	</liferay-ui:search-container-row>
       	
       	 <liferay-ui:search-iterator />
       	 
       	  </aui:form>
       	 
       </liferay-ui:search-container>
	
	</div>
	
	<c:if test='<%=permissionChecker.hasPermission(scopeGroupId, "br.mp.mprj.portal.portlet.gestaomensagens.service.model", scopeGroupId, "ADICIONAR_MENSAGEM") %>'>
	
	
	<liferay-portlet:renderURL var="novaListaDestinatario">
			<liferay-portlet:param name="mvcPath" value="/jsp/admin/destinatario/formulario.jsp"/>
	</liferay-portlet:renderURL>
	
	<div class="botoes-acao-wizard-wrapper clearfix">
			<br/>
			<div class="botao-wizard-wrapper">
				<a href="${novaListaDestinatario}" id="<portlet:namespace/>btProximo" class="botao-wizard proximo">
					<liferay-ui:message key="destinatario.nova-lista"/>
					<!--  <i class="icon-circle-arrow-right"></i> -->
				</a>
			</div>
	</div>	
	
	</c:if>	
	
	<div>
	    
</div>
</div>


<aui:script use="node, liferay-search-container">

Liferay.provide(
		window,
		'limparFormulario',
		function() {
			 var tituloMensagemSearch = A.one('#<portlet:namespace />nomeListaDestinatarioSearch');
			 tituloMensagemSearch.val("");

			var tipoDestintario = A.one('#<portlet:namespace />tipoDestinatario');
			tipoDestintario.attr('checked',false);

			var contaSaida = A.one('#<portlet:namespace />contaSaida');
			contaSaida.attr('checked',false);
			 
		}
	);	

</aui:script>

<script type="text/javascript">
	$('.btn-group.lfr-icon-menu').width(120);	
	$('.lfr-pagination-delta-selector').hide();
	$('.table-sort-liner').children().attr('href', $('.table-sort-liner').children().attr('href').replace('_portletgestaomensagens_WAR_portletgestaomensagens_cur=' , '_portletgestaomensagens_WAR_portletgestaomensagens_cur=1&R=' ));
	document.getElementById("<portlet:namespace />nomeListaDestinatarioSearch").focus();
</script>