package com.rjconsultores.ventaboletos.web.gui.controladores.ingreso;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
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.TipoEventoExtra;
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.RenderTipoEventoExtra;

/**
 * 
 * @author Rafius
 */
@Controller("busquedaTipoEventoExtraController")
@Scope("prototype")
public class BusquedaTipoEventoExtraController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient PagedListWrapper<TipoEventoExtra> plwTipoEventoExtra;
	private MyListbox tipoEventoExtraList;
	private Paging pagingTipoEventoExtra;
	private Textbox txtNombre;

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

		tipoEventoExtraList.setItemRenderer(new RenderTipoEventoExtra());
		tipoEventoExtraList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				TipoEventoExtra emp = (TipoEventoExtra) tipoEventoExtraList.getSelected();
				verEventoExtra(emp);
			}
		});

		refreshLista();

		txtNombre.focus();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verEventoExtra(TipoEventoExtra emp) {
		if (emp == null) {
			return;
		}

		Map args = new HashMap();
		args.put("tipoEventoExtra", emp);
		args.put("tipoEventoExtraList", tipoEventoExtraList);

		openWindow("/gui/ingreso/editarTipoEventoExtra.zul",
				Labels.getLabel("editarTipoEventoExtraController.window.title"),
				args, MODAL);
	}

	private void refreshLista() {
		HibernateSearchObject<TipoEventoExtra> tipoEventoExtraBusqueda =
				new HibernateSearchObject<TipoEventoExtra>(TipoEventoExtra.class,
						pagingTipoEventoExtra.getPageSize());

		tipoEventoExtraBusqueda.addFilterLike("descTipoEvento", "%" + txtNombre.getText().trim().concat("%"));

		tipoEventoExtraBusqueda.addFilterEqual("activo", Boolean.TRUE);
		tipoEventoExtraBusqueda.addSortAsc("descTipoEvento");

		plwTipoEventoExtra.init(tipoEventoExtraBusqueda, tipoEventoExtraList, pagingTipoEventoExtra);

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

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

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

	public void onClick$btnNovo(Event ev) {
		verEventoExtra(new TipoEventoExtra());
	}
}
