/*
 * 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.HashMap;
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.Paging;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.RegionMetropolitana;
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.RenderRegionMetropolitana;

@Controller("busquedaRegionMetropolitanaController")
@Scope("prototype")
public class BusquedaRegionMetropolitanaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;

	@Autowired
	private transient PagedListWrapper<RegionMetropolitana> plwRegionMetropolitana;
	private MyListbox regionMetropolitanaList;
	private Paging pagingRegionMetropolitana;
	private Textbox txtRegion;

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

		regionMetropolitanaList.setItemRenderer(new RenderRegionMetropolitana());
		regionMetropolitanaList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				RegionMetropolitana r = (RegionMetropolitana) regionMetropolitanaList.getSelected();
				verRegionMetropolitana(r);
			}
		});

		refreshLista();

		txtRegion.focus();
	
	}
	
	private void verRegionMetropolitana(RegionMetropolitana r) {
		if (r == null) {
			return;
		}

		Map args = new HashMap();
		args.put("regionMetropolitana", r);
		args.put("regionMetropolitanaList", regionMetropolitanaList);

		openWindow("/gui/catalogos/editarRegionMetropolitana.zul",
				Labels.getLabel("editarRegionMetropolitanaController.window.title"), args, MODAL);
	}

	private void refreshLista() {
		HibernateSearchObject<RegionMetropolitana> regionMetropolitanaBusqueda = new HibernateSearchObject<RegionMetropolitana>(RegionMetropolitana.class,
				pagingRegionMetropolitana.getPageSize());

		regionMetropolitanaBusqueda.addFilterLike("descRegion", "%" + txtRegion.getText().trim().concat("%"));
		regionMetropolitanaBusqueda.addFilterEqual("activo", Boolean.TRUE);
		regionMetropolitanaBusqueda.addSortAsc("descRegion");
		
		plwRegionMetropolitana.init(regionMetropolitanaBusqueda, regionMetropolitanaList, pagingRegionMetropolitana);

		if (regionMetropolitanaList.getData().length == 0) {
			try {
				Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
						Labels.getLabel("busquedaArticuloController.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) {
		verRegionMetropolitana(new RegionMetropolitana());
	}
}
