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

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.entidad.TaxaEmbarqueKm;
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.RenderTaxaEmbarqueOrgao;

/**
 * 
 * @author Administrador
 */
@Controller("busquedaTaxaEmbarqueKmController")
@Scope("prototype")
public class BusquedaTaxaEmbarqueKmController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient PagedListWrapper<OrgaoConcedente> plwOrgao;
	private MyListbox taxaEmbarqueKmList;
	private Paging pagingTaxaEmbarqueKm;
	private Textbox txtOrgao;

	public Paging getPagingTaxaEmbarqueKm() {
		return pagingTaxaEmbarqueKm;
	}

	public void setPagingTaxaEmbarqueKm(Paging pagingTaxaEmbarqueKm) {
		this.pagingTaxaEmbarqueKm = pagingTaxaEmbarqueKm;
	}

	public PagedListWrapper<OrgaoConcedente> getPlwOrgao() {
		return plwOrgao;
	}

	public void setPlwOrgao(PagedListWrapper<OrgaoConcedente> plwOrgao) {
		this.plwOrgao = plwOrgao;
	}

	public MyListbox getTaxaEmbarqueKmList() {
		return taxaEmbarqueKmList;
	}

	public void setTaxaEmbarqueKmList(MyListbox taxaEmbarqueKmList) {
		this.taxaEmbarqueKmList = taxaEmbarqueKmList;
	}

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

		taxaEmbarqueKmList.setItemRenderer(new RenderTaxaEmbarqueOrgao());
		taxaEmbarqueKmList.addEventListener("onDoubleClick", new EventListener() {

			@Override
			public void onEvent(Event event) throws Exception {
				OrgaoConcedente c = (OrgaoConcedente) taxaEmbarqueKmList.getSelected();
				verTaxaEmbarqueKm(c);
			}
		});

		refreshLista();

		txtOrgao.focus();
	}

	private void verTaxaEmbarqueKm(OrgaoConcedente o) {
		if (o == null) {
			return;
		}

		Map args = new HashMap();
		args.put("orgao", o);
		args.put("taxaEmbarqueKmList", taxaEmbarqueKmList);

		openWindow("/gui/tarifas/editarTaxaEmbarqueKm.zul",
				Labels.getLabel("busquedaTaxaEmbarqueKmController.window.title"),
				args, MODAL);
	}

	private void refreshLista() {

		HibernateSearchObject<OrgaoConcedente> taxaEmbarqueKmBusqueda = new HibernateSearchObject<OrgaoConcedente>(OrgaoConcedente.class, pagingTaxaEmbarqueKm.getPageSize());

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

		if (!txtOrgao.getValue().equals("")) {
			taxaEmbarqueKmBusqueda.addFilterLike("descOrgao", "%" + txtOrgao.getText().trim().concat("%"));
		}

		taxaEmbarqueKmBusqueda.addSortAsc("descOrgao");

		plwOrgao.init(taxaEmbarqueKmBusqueda, taxaEmbarqueKmList, pagingTaxaEmbarqueKm);

		if (taxaEmbarqueKmList.getData().length == 0) {
			try {
				Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
						Labels.getLabel("busquedaTaxaEmbarqueKmController.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) {
		verTaxaEmbarqueKm(new TaxaEmbarqueKm());
	}

	private void verTaxaEmbarqueKm(TaxaEmbarqueKm tx) {
		if (tx == null) {
			return;
		}

		Map args = new HashMap();
		args.put("orgao", null);
		args.put("taxaEmbarqueKm", tx);
		args.put("taxaEmbarqueKmList", taxaEmbarqueKmList);

		openWindow("/gui/tarifas/editarTaxaEmbarqueKm.zul",
				Labels.getLabel("busquedaTaxaEmbarqueKmController.window.title"),
				args, MODAL);
	}
}
