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

import com.rjconsultores.ventaboletos.entidad.Via;
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.RenderVia;
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("busquedaViaController")
@Scope("prototype")
public class BusquedaViaController extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<Via> plwVia;
    private MyListbox viaList;
    private Paging pagingVia;
    private Textbox txtNome;

    public Paging getPagingVia() {
        return pagingVia;
    }

    public void setPagingVia(Paging pagingVia) {
        this.pagingVia = pagingVia;
    }

    public PagedListWrapper<Via> getPlwVia() {
        return plwVia;
    }

    public void setPlwVia(PagedListWrapper<Via> plwVia) {
        this.plwVia = plwVia;
    }

    public MyListbox getViaList() {
        return viaList;
    }

    public void setViaList(MyListbox viaList) {
        this.viaList = viaList;
    }

    public Textbox getTxtNome() {
        return txtNome;
    }

    public void setTxtNome(Textbox txtNome) {
        this.txtNome = txtNome;
    }

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

        viaList.setItemRenderer(new RenderVia());
        viaList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                Via c = (Via) viaList.getSelected();
                verVia(c);
            }
        });

        refreshLista();

        txtNome.focus();
    }

    private void verVia(Via v) {
        if (v == null) {
            return;
        }

        Map args = new HashMap();
        args.put("via", v);
        args.put("viaList", viaList);

        openWindow("/gui/esquema_operacional/editarVia.zul",
                Labels.getLabel("editarViaController.window.title"), args, MODAL);
    }

    private void refreshLista() {
        HibernateSearchObject<Via> viaBusqueda =
                new HibernateSearchObject<Via>(Via.class,
                pagingVia.getPageSize());

        viaBusqueda.addFilterLike("nombvia",
                "%" + txtNome.getText().trim().concat("%"));

        viaBusqueda.addSortAsc("nombvia");
        viaBusqueda.addFilterEqual("activo", Boolean.TRUE);

        plwVia.init(viaBusqueda, viaList, pagingVia);

        if (viaList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaViaController.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) {
        verVia(new Via());
    }
}
