/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.gui.controladores.catalogos;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.util.resource.Labels;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.constantes.ConstantesFuncionSistema;
import com.rjconsultores.ventaboletos.entidad.Custom;
import com.rjconsultores.ventaboletos.enums.SistemaEnum;
import com.rjconsultores.ventaboletos.service.FuncionSistemaService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.paginacion.HibernateSearchObject;
import com.rjconsultores.ventaboletos.web.utilerias.paginacion.PagedListWrapper;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderCustom;
import com.trg.search.Search;

/**
 *
 * @author Lucas
 */
@Controller("busquedaCustomController")
@Scope("prototype")
public class BusquedaCustomController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;

	@Autowired
	private transient PagedListWrapper<Custom> plwCustom;
	
	@Autowired
	private FuncionSistemaService funcionSistemaService;
	
	private MyListbox customList;
	private Paging pagingCustom;
	private Textbox txtChave;
	private Combobox cmbSistema;
	private List<SistemaEnum> lsSistemaEnum;
	private Search customBusqueda;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		lsSistemaEnum = Arrays.asList(SistemaEnum.values());

		customList.setItemRenderer(new RenderCustom());
		customList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				Custom a = (Custom) customList.getSelected();
				verCustom(a);
			}
		});

		refreshLista();

		txtChave.focus();
	}

	private void verCustom(Custom a) {
		if (a == null) {
			return;
		}

		Map args = new HashMap();
		args.put("custom", a);
		args.put("customList", customList);

		openWindow("/gui/catalogos/editarCustom.zul",
				Labels.getLabel("customController.window.title"), args, MODAL);
	}
	
	private void verCustomUploadArquivo() {
		Map args = new HashMap();
		args.put("customList", customList);
		openWindow("/gui/catalogos/editarCustomUploadArquivo.zul",
		Labels.getLabel("customController.uploadArquivo.titulo"), args, MODAL);
	}

	private void refreshLista() {
		HibernateSearchObject<Custom> customBusqueda = new HibernateSearchObject<Custom>(Custom.class,
				pagingCustom.getPageSize());

		customBusqueda.addFilterLike("chave",
				"%" + txtChave.getText().trim().concat("%"));
		
		Comboitem cbiSistema = cmbSistema.getSelectedItem();
        if (cbiSistema != null) {
        	SistemaEnum dab = (SistemaEnum) cbiSistema.getValue();
            customBusqueda.addFilterEqual("sistema", dab.getValor());
        }

		customBusqueda.addFilterEqual("activo", Boolean.TRUE);
		
		customBusqueda.addSortAsc("valor");

		plwCustom.init(customBusqueda, customList, pagingCustom);

		if (customList.getData().length == 0) {
			try {
				Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
						Labels.getLabel("customController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);
			} catch (InterruptedException ex) {
			}
		}
	}

	public void onClick$btnPesquisa(Event ev) {
		refreshLista();
	}

	public void onClick$btnRefresh(Event ev) {
		refreshLista();
	}

	public void onClick$btnNovo(Event ev) {
		verCustom(new Custom());
	}
	
	public void onClick$btnUpLoad(Event ev) {
		verCustomUploadArquivo();
	}

	public PagedListWrapper<Custom> getPlwCustom() {
		return plwCustom;
	}

	public void setPlwCustom(PagedListWrapper<Custom> plwCustom) {
		this.plwCustom = plwCustom;
	}

	public MyListbox getCustomList() {
		return customList;
	}

	public void setCustomList(MyListbox customList) {
		this.customList = customList;
	}

	public Paging getPagingCustom() {
		return pagingCustom;
	}

	public void setPagingCustom(Paging pagingCustom) {
		this.pagingCustom = pagingCustom;
	}

	public Textbox getTxtChave() {
		return txtChave;
	}

	public void setTxtChave(Textbox txtChave) {
		this.txtChave = txtChave;
	}

	public Combobox getCmbSistema() {
		return cmbSistema;
	}

	public void setCmbSistema(Combobox cmbSistema) {
		this.cmbSistema = cmbSistema;
	}

	public List<SistemaEnum> getLsSistemaEnum() {
		return lsSistemaEnum;
	}

	public void setLsSistemaEnum(List<SistemaEnum> lsSistemaEnum) {
		this.lsSistemaEnum = lsSistemaEnum;
	}

	public Search getCustomBusqueda() {
		return customBusqueda;
	}

	public void setCustomBusqueda(Search customBusqueda) {
		this.customBusqueda = customBusqueda;
	}
	
	public Boolean getExibirBtnUpload() {
		return UsuarioLogado.getUsuarioLogado().isPermisoClave(ConstantesFuncionSistema.CLAVE_UPLOAD_ARQUIVO_CONFIGURACAO);
	}

}
