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

import com.rjconsultores.ventaboletos.entidad.Sistema;
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.RenderSistema;
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.Intbox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;

/**
 *
 * @author rodrigo
 */
@Controller("busquedaSistemaController")
@Scope("prototype")
public class BusquedaSistemaController extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<Sistema> plwFormaPago;
    private MyListbox sistemaList;
    private Paging pagingSistema;
    private Textbox txtNome;
    private Intbox txtID;

    public Paging getPagingSistema() {
        return pagingSistema;
    }

    public void setPagingSistema(Paging pagingSistema) {
        this.pagingSistema = pagingSistema;
    }

    public MyListbox getSistemaList() {
        return sistemaList;
    }

    public void setSistemaList(MyListbox sistemaList) {
        this.sistemaList = sistemaList;
    }

    public Intbox getTxtID() {
        return txtID;
    }

    public void setTxtID(Intbox txtID) {
        this.txtID = txtID;
    }

    public Textbox getTxtNome() {
        return txtNome;
    }

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

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

        sistemaList.setItemRenderer(new RenderSistema());
        sistemaList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                Sistema s = (Sistema) sistemaList.getSelected();
                verSistema(s);
            }
        });

        refreshLista();

        txtNome.focus();
    }

    private void verSistema(Sistema s) {
        if (s == null) {
            return;
        }

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

        openWindow("/gui/seguridad/editarSistema.zul",
                Labels.getLabel("editarSistemaController.window.title"), args, MODAL);
    }

    private void refreshLista() {
        HibernateSearchObject<Sistema> sistemaBusqueda =
                new HibernateSearchObject<Sistema>(Sistema.class,
                pagingSistema.getPageSize());

        sistemaBusqueda.addFilterLike("nombsistema", "%" + txtNome.getText().trim().concat("%"));
        sistemaBusqueda.addFilterEqual("sistemaId", txtID.getValue());

        sistemaBusqueda.addSortAsc("nombsistema");
        sistemaBusqueda.addFilterEqual("activo", Boolean.TRUE);

        plwFormaPago.init(sistemaBusqueda, sistemaList, pagingSistema);

        if (sistemaList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaSistemaController.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) {
        verSistema(new Sistema());
    }
}
