/*
 * 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.CompaniaBancaria;
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.RenderCompaniaBancaria;
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.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;

/**
 *
 * @author Administrador
 */
@Controller("busquedaCompaniaBancaria")
@Scope("prototype")
public class BusquedaCompaniaBancaria extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<CompaniaBancaria> plwCompaniaBancaria;
    private MyListbox companiaBancariaList;
    private Paging pagingCompaniaBancaria;
    private Textbox txtCVECompania;
    private Textbox txtCVESucursal;

    public MyListbox getCompaniaBancariaList() {
        return companiaBancariaList;
    }

    public void setCompaniaBancariaList(MyListbox companiaBancariaList) {
        this.companiaBancariaList = companiaBancariaList;
    }

    public Paging getPagingCompaniaBancaria() {
        return pagingCompaniaBancaria;
    }

    public void setPagingCompaniaBancaria(Paging pagingCompaniaBancaria) {
        this.pagingCompaniaBancaria = pagingCompaniaBancaria;
    }

    public Textbox getTxtCVECompania() {
        return txtCVECompania;
    }

    public void setTxtCVECompania(Textbox txtCVECompania) {
        this.txtCVECompania = txtCVECompania;
    }

    public Textbox getTxtCVESucursal() {
        return txtCVESucursal;
    }

    public void setTxtCVESucursal(Textbox txtCVESucursal) {
        this.txtCVESucursal = txtCVESucursal;
    }

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

        companiaBancariaList.setItemRenderer(new RenderCompaniaBancaria());
        companiaBancariaList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                CompaniaBancaria s = (CompaniaBancaria) companiaBancariaList.getSelected();
                verCompaniaBancaria(s);
            }
        });

        refreshLista();

        txtCVECompania.focus();
    }

    private void verCompaniaBancaria(CompaniaBancaria c) {
        if (c == null) {
            return;
        }

        Map args = new HashMap();
        args.put("companiaBancaria", c);
        args.put("companiaBancariaList", companiaBancariaList);

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

    private void refreshLista() {
        HibernateSearchObject<CompaniaBancaria> companiaBancariaBusqueda =
                new HibernateSearchObject<CompaniaBancaria>(CompaniaBancaria.class,
                pagingCompaniaBancaria.getPageSize());

        if (!txtCVECompania.getValue().isEmpty()) {
            companiaBancariaBusqueda.addFilterEqual("cvecompania", txtCVECompania.getValue());
        }

        if (!txtCVESucursal.getValue().isEmpty()) {
            companiaBancariaBusqueda.addFilterEqual("cvesucursal", txtCVESucursal.getValue());
        }

        companiaBancariaBusqueda.addSortAsc("cvecompania");
        companiaBancariaBusqueda.addFilterEqual("activo", Boolean.TRUE);

        plwCompaniaBancaria.init(companiaBancariaBusqueda, companiaBancariaList, pagingCompaniaBancaria);

        if (companiaBancariaList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        Labels.getLabel("busquedaCompaniaBancaria.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) {
        verCompaniaBancaria(new CompaniaBancaria());
    }
}
