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

import com.rjconsultores.ventaboletos.entidad.Secretaria;
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.RenderSecretaria;
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("busquedaSecretariaController")
@Scope("prototype")
public class BusquedaSecretariaController extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<Secretaria> plwSecretaria;
    private MyListbox secretariaList;
    private Paging pagingSecretaria;
    private Textbox txtDescSecretaria;
    private Textbox txtCveSecretaria;

    public Paging getPagingSecretaria() {
        return pagingSecretaria;
    }

    public void setPagingSecretaria(Paging pagingSecretaria) {
        this.pagingSecretaria = pagingSecretaria;
    }

    public MyListbox getSecretariaList() {
        return secretariaList;
    }

    public void setSecretariaList(MyListbox secretariaList) {
        this.secretariaList = secretariaList;
    }

    public Textbox getTxtCveSecretaria() {
        return txtCveSecretaria;
    }

    public void setTxtCveSecretaria(Textbox txtCveSecretaria) {
        this.txtCveSecretaria = txtCveSecretaria;
    }

    public Textbox getTxtDescSecretaria() {
        return txtDescSecretaria;
    }

    public void setTxtDescSecretaria(Textbox txtDescSecretaria) {
        this.txtDescSecretaria = txtDescSecretaria;
    }

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

        secretariaList.setItemRenderer(new RenderSecretaria());
        secretariaList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                Secretaria c = (Secretaria) secretariaList.getSelected();
                verSecretaria(c);
            }
        });

        refreshLista();

        txtDescSecretaria.focus();
    }

    private void verSecretaria(Secretaria s) {
        if (s == null) {
            return;
        }

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

        openWindow("/gui/configuraciones_comerciales/editarSecretaria.zul",
                Labels.getLabel("busquedaSecretariaController.window.title"),
                args, MODAL);
    }

    private void refreshLista() {
        HibernateSearchObject<Secretaria> secretariaBusqueda =
                new HibernateSearchObject<Secretaria>(Secretaria.class,
                pagingSecretaria.getPageSize());

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

        secretariaBusqueda.addFilterLike("descsecretaria",
                "%" + txtDescSecretaria.getText().trim().concat("%"));
        secretariaBusqueda.addFilterLike("cvesecretaria",
                "%" + txtCveSecretaria.getText().trim().concat("%"));

        secretariaBusqueda.addSortAsc("descsecretaria");

        plwSecretaria.init(secretariaBusqueda, secretariaList, pagingSecretaria);

        if (secretariaList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaSecretariaController.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) {
        verSecretaria(new Secretaria());
    }
}
