/*
 * 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.Plaza;
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.RenderPlaza;
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("busquedaPlazaController")
@Scope("prototype")
public class BusquedaPlazaController extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<Plaza> plwPlaza;
    private MyListbox plazaList;
    private Paging pagingPlaza;
    private Textbox txtNome;

    public MyListbox getPlazaList() {
        return plazaList;
    }

    public void setPlazaList(MyListbox plazaList) {
        this.plazaList = plazaList;
    }

    public Paging getPagingPlaza() {
        return pagingPlaza;
    }

    public void setPagingPlaza(Paging pagingPlaza) {
        this.pagingPlaza = pagingPlaza;
    }

    public Textbox getTxtNome() {
        return txtNome;
    }

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

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

        plazaList.setItemRenderer(new RenderPlaza());
        plazaList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                Plaza a = (Plaza) plazaList.getSelected();
                verPlaza(a);
            }
        });

        refreshLista();

        txtNome.focus();
    }

    private void verPlaza(Plaza a) {
        if (a == null) {
            return;
        }

        Map args = new HashMap();
        args.put("plaza", a);
        args.put("plazaList", plazaList);

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

    private void refreshLista() {
        HibernateSearchObject<Plaza> plazaBusqueda =
                new HibernateSearchObject<Plaza>(Plaza.class,
                pagingPlaza.getPageSize());

        plazaBusqueda.addFilterLike("nombplaza",
                "%" + txtNome.getText().trim().concat("%"));

        plazaBusqueda.addSortAsc("nombplaza");
        plazaBusqueda.addFilterNotEqual("plazaId", -1);

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

        plwPlaza.init(plazaBusqueda, plazaList, pagingPlaza);

        if (plazaList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaPlazaController.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) {
        verPlaza(new Plaza());
    }
}
