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

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.ItemDesconto;
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.RenderItemDesconto;

@Scope("prototype")
@Controller("busquedaItemDescontoController")
public class BusquedaItemDescontoController extends MyGenericForwardComposer {

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

	@Autowired
    private transient PagedListWrapper<ItemDesconto> plwItemDesconto;
    private MyListbox itemDescontoList;
    private Paging pagingItemDesconto;
    private Textbox txtNomitemdesconto;
    
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);

        itemDescontoList.setItemRenderer(new RenderItemDesconto());
        itemDescontoList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                ItemDesconto a = (ItemDesconto) itemDescontoList.getSelected();
                verItemDesconto(a);
            }
        });

        refreshLista();

        txtNomitemdesconto.focus();
    }
    
    private void verItemDesconto(ItemDesconto t) {
        if (t == null) {
            return;
        }

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

        openWindow("/gui/comissao/editarItemDesconto.zul",
                Labels.getLabel("editarItemDescontoController.window.title"), args, MODAL);
    }

    private void refreshLista() {
    	try {
			HibernateSearchObject<ItemDesconto> itemDescontoBusqueda = new HibernateSearchObject<ItemDesconto>(ItemDesconto.class, pagingItemDesconto.getPageSize());
			
			if(StringUtils.isNotBlank(txtNomitemdesconto.getText())) {
				itemDescontoBusqueda.addFilterEqual("nomitemdesconto", txtNomitemdesconto.getText().trim());
			}
			
			itemDescontoBusqueda.addFilterEqual("activo", Boolean.TRUE);
			plwItemDesconto.init(itemDescontoBusqueda, itemDescontoList, pagingItemDesconto);
	
	        if (itemDescontoList.getData().length == 0) {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        		Labels.getLabel("busquedaItemDescontoController.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) {
        verItemDesconto(new ItemDesconto());
    }
	
}
