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

import com.rjconsultores.ventaboletos.entidad.Articulo;
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.RenderArticulo;
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("busquedaArticuloController")
@Scope("prototype")
public class BusquedaArticuloController extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<Articulo> plwArticulo;
    private MyListbox articuloList;
    private Paging pagingArticulo;
    private Textbox txtNome;

    public MyListbox getArticuloList() {
        return articuloList;
    }

    public void setArticuloList(MyListbox articuloList) {
        this.articuloList = articuloList;
    }

    public Paging getPagingArticulo() {
        return pagingArticulo;
    }

    public void setPagingArticulo(Paging pagingArticulo) {
        this.pagingArticulo = pagingArticulo;
    }

    public Textbox getTxtNome() {
        return txtNome;
    }

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

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

        articuloList.setItemRenderer(new RenderArticulo());
        articuloList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                Articulo a = (Articulo) articuloList.getSelected();
                verArticulo(a);
            }
        });

        refreshLista();

        txtNome.focus();
    }

    private void verArticulo(Articulo a) {
        if (a == null) {
            return;
        }

        Map args = new HashMap();
        args.put("articulo", a);
        args.put("articuloList", articuloList);

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

    private void refreshLista() {
        HibernateSearchObject<Articulo> articuloBusqueda =
                new HibernateSearchObject<Articulo>(Articulo.class,
                pagingArticulo.getPageSize());

        articuloBusqueda.addFilterLike("descarticulo",
                "%" + txtNome.getText().trim().concat("%"));

        articuloBusqueda.addSortAsc("descarticulo");
        articuloBusqueda.addFilterNotEqual("articuloId", -1);
        
        articuloBusqueda.addFilterEqual("activo", Boolean.TRUE);

        plwArticulo.init(articuloBusqueda, articuloList, pagingArticulo);

        if (articuloList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaArticuloController.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) {
        verArticulo(new Articulo());
    }
}
