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

import com.rjconsultores.ventaboletos.entidad.Parada;
import com.rjconsultores.ventaboletos.entidad.TaxaEmbarqueParada;
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.RenderTaxaEmbarqueParadaLocalidade;
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;

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

    @Autowired
    private transient PagedListWrapper<Parada> plwParada;
    private MyListbox paradaList;
    private Paging pagingTaxaEmbarqueParada;
    private Textbox txtParada;

    public Paging getPagingTaxaEmbarqueParada() {
        return pagingTaxaEmbarqueParada;
    }

    public void setPagingTaxaEmbarqueParada(Paging pagingTaxaEmbarqueParada) {
        this.pagingTaxaEmbarqueParada = pagingTaxaEmbarqueParada;
    }

    public PagedListWrapper<Parada> getPlwParada() {
        return plwParada;
    }

    public void setPlwParada(PagedListWrapper<Parada> plwParada) {
        this.plwParada = plwParada;
    }

    public MyListbox getParadaList() {
        return paradaList;
    }

    public void setParadaList(MyListbox paradaList) {
        this.paradaList = paradaList;
    }

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

        paradaList.setItemRenderer(new RenderTaxaEmbarqueParadaLocalidade());
        paradaList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                Parada c = (Parada) paradaList.getSelected();
                verTaxaEmbarqueParada(c);
            }
        });

        refreshLista();

        txtParada.focus();
    }

    private void verTaxaEmbarqueParada(Parada s) {
        if (s == null) {
            return;
        }

        Map args = new HashMap();
        args.put("parada", s);

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

    private void refreshLista() {
        HibernateSearchObject<Parada> taxaEmbarqueParadaBusqueda =
                new HibernateSearchObject<Parada>(Parada.class,
                pagingTaxaEmbarqueParada.getPageSize());

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

        taxaEmbarqueParadaBusqueda.addFilterLike("descparada",
                "%" + txtParada.getText().trim().concat("%"));

        taxaEmbarqueParadaBusqueda.addSortAsc("descparada");

        plwParada.init(taxaEmbarqueParadaBusqueda, paradaList, pagingTaxaEmbarqueParada);

        if (paradaList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaTaxaEmbarqueParadaController.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) {
        verTaxaEmbarqueParada(new Parada());
    }
}
