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

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.TarjetaCredito;
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.RenderTarjetaCredito;

@Controller("busquedaTarjetaCreditoController")
@Scope("prototype")
public class BusquedaTarjetaCreditoController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient PagedListWrapper<TarjetaCredito> plwTarjetaCredito;
	private MyListbox tarjetaCreditoList;
	private Paging pagingTarjetaCredito;
	private Textbox txtDescTarjetaCredito;

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

		tarjetaCreditoList.setItemRenderer(new RenderTarjetaCredito());
		tarjetaCreditoList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				TarjetaCredito emp = (TarjetaCredito) tarjetaCreditoList.getSelected();
				verTarjetaCredito(emp);
			}
		});

		refreshLista();

		txtDescTarjetaCredito.focus();
	}

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

		Map args = new HashMap();
		args.put("tarjetaCredito", emp);
		args.put("tarjetaCreditoList", tarjetaCreditoList);

		openWindow("/gui/configuraciones_comerciales/editarTarjetaCredito.zul",
				Labels.getLabel("editarTarjetaCreditoController.window.title"), args, MODAL);
	}

	private void refreshLista() {
		HibernateSearchObject<TarjetaCredito> tarjetaCreditoBusqueda =
				new HibernateSearchObject<TarjetaCredito>(TarjetaCredito.class, pagingTarjetaCredito.getPageSize());

		if (!txtDescTarjetaCredito.getText().isEmpty()) {
			tarjetaCreditoBusqueda.addFilterLike("descTarjetaCredito", txtDescTarjetaCredito.getText() + "%");
		}

		tarjetaCreditoBusqueda.addFilterEqual("activo", Boolean.TRUE);
		tarjetaCreditoBusqueda.addSortAsc("descTarjetaCredito");

		plwTarjetaCredito.init(tarjetaCreditoBusqueda, tarjetaCreditoList, pagingTarjetaCredito);

		if (tarjetaCreditoList.getData().length == 0) {
			try {
				Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
						Labels.getLabel("busquedaEmpresaController.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) {
		verTarjetaCredito(new TarjetaCredito());
	}
}
