package com.rjconsultores.ventaboletos.web.gui.controladores.catalogos;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

import com.rjconsultores.ventaboletos.entidad.PacoteConvenio;
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.RenderPacoteConvenio;

@Controller("busquedaPacoteConvenioController")
@Scope("prototype")
public class BusquedaPacoteConvenioController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	
	private static final Logger log = LoggerFactory.getLogger(BusquedaPacoteConvenioController.class);

	@Autowired
    private transient PagedListWrapper<PacoteConvenio> plwPacoteConvenio;
    private MyListbox pacoteConvenioList;
    private Paging pagingPacoteConvenio;
    private Textbox txtNomconvenio;

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

        pacoteConvenioList.setItemRenderer(new RenderPacoteConvenio());
        pacoteConvenioList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                PacoteConvenio a = (PacoteConvenio) pacoteConvenioList.getSelected();
                verPacoteConvenio(a);
            }
        });

        refreshLista();

        txtNomconvenio.focus();
    }

    private void verPacoteConvenio(PacoteConvenio t) {
        if (t == null) {
            return;
        }

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("pacoteConvenio", t);
        args.put("pacoteConvenioList", pacoteConvenioList);

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

    private void refreshLista() {
        HibernateSearchObject<PacoteConvenio> pacoteConvenioBusqueda = new HibernateSearchObject<PacoteConvenio>(PacoteConvenio.class, pagingPacoteConvenio.getPageSize());

        if(StringUtils.isNotBlank(txtNomconvenio.getText())) {
        	pacoteConvenioBusqueda.addFilterLike("nomconvenio", "%" + txtNomconvenio.getText().trim().concat("%"));
        }

        pacoteConvenioBusqueda.addSortAsc("nomconvenio");
        pacoteConvenioBusqueda.addFilterEqual("activo", Boolean.TRUE);
        plwPacoteConvenio.init(pacoteConvenioBusqueda, pacoteConvenioList, pagingPacoteConvenio);

        if (pacoteConvenioList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        		Labels.getLabel("busquedaPacoteConvenioController.window.title"),
                        		Messagebox.OK, Messagebox.INFORMATION);
            } catch (InterruptedException e) {
            	log.error(e.getMessage(), e.getCause());
            }
        }
    }

    public void onClick$btnPesquisa(Event ev) {
        refreshLista();
    }

    public void onClick$btnRefresh(Event ev) {
        refreshLista();
    }

    public void onClick$btnNovo(Event ev) {
        verPacoteConvenio(new PacoteConvenio());
    }

	public MyListbox getPacoteConvenioList() {
		return pacoteConvenioList;
	}

	public void setPacoteConvenioList(MyListbox pacoteConvenioList) {
		this.pacoteConvenioList = pacoteConvenioList;
	}

	public Paging getPagingPacoteConvenio() {
		return pagingPacoteConvenio;
	}

	public void setPagingPacoteConvenio(Paging pagingPacoteConvenio) {
		this.pagingPacoteConvenio = pagingPacoteConvenio;
	}

	public Textbox getTxtNomconvenio() {
		return txtNomconvenio;
	}

	public void setTxtNomconvenio(Textbox txtNomconvenio) {
		this.txtNomconvenio = txtNomconvenio;
	}
}
