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.OrgaoConcedente;
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.RenderOrgaoConcedente;

@Controller("busquedaOrgaoConcedenteController")
@Scope("prototype")
public class BusquedaOrgaoConcedenteController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient PagedListWrapper<OrgaoConcedente> plwOrgaoConcedente;
	private MyListbox orgaoConcedenteList;
	private Paging pagingOrgaoConcedente;
	private Textbox txtNome;

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

		orgaoConcedenteList.setItemRenderer(new RenderOrgaoConcedente());
		orgaoConcedenteList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				OrgaoConcedente a = (OrgaoConcedente) orgaoConcedenteList.getSelected();
				verOrgaoConcedente(a);
			}
		});

		refreshLista();

		txtNome.focus();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verOrgaoConcedente(OrgaoConcedente o) {
		if (o == null) {
			return;
		}

		Map args = new HashMap();
		args.put("orgaoConcedente", o);
		args.put("orgaoConcedenteList", orgaoConcedenteList);

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

	private void refreshLista() {
		HibernateSearchObject<OrgaoConcedente> orgaoConcedenteBusqueda =
					new HibernateSearchObject<OrgaoConcedente>(OrgaoConcedente.class, pagingOrgaoConcedente.getPageSize());

		orgaoConcedenteBusqueda.addFilterILike("descOrgao", "%" + txtNome.getText().trim().concat("%"));

		orgaoConcedenteBusqueda.addSortAsc("descOrgao");

		orgaoConcedenteBusqueda.addFilterEqual("activo", Boolean.TRUE);

		plwOrgaoConcedente.init(orgaoConcedenteBusqueda, orgaoConcedenteList, pagingOrgaoConcedente);

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

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

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