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

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.Datebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.EnderecoApanhe;
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.RenderEnderecoApanhe;

@Controller("busquedaManutencaoPacoteController")
@Scope("prototype")
public class BusquedaManutencaoPacoteController extends MyGenericForwardComposer {

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

	@Autowired
    private transient PagedListWrapper<EnderecoApanhe> plwEnderecoApanhe;
    private MyListbox enderecoApanheList;
    private Paging pagingEnderecoApanhe;
    private Textbox txtNumoperacion;
    private Datebox dtDataPacote;

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

        enderecoApanheList.setItemRenderer(new RenderEnderecoApanhe());
        enderecoApanheList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                EnderecoApanhe a = (EnderecoApanhe) enderecoApanheList.getSelected();
                verEnderecoApanhe(a);
            }
        });

        refreshLista();

        txtNumoperacion.focus();
    }

    private void verEnderecoApanhe(EnderecoApanhe t) {
        if (t == null) {
            return;
        }

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

        openWindow("/gui/senda_amigo/editarManutencaoPacote.zul",
                Labels.getLabel("editarManutencaoPacoteController.window.title"), args, MODAL);
    }

    private void refreshLista() {
		HibernateSearchObject<EnderecoApanhe> enderecoApanheBusqueda = new HibernateSearchObject<EnderecoApanhe>(EnderecoApanhe.class, pagingEnderecoApanhe.getPageSize());
		
		if(StringUtils.isNotBlank(txtNumoperacion.getText())) {
			enderecoApanheBusqueda.addFilterEqual("vendaPacote.numoperacion", txtNumoperacion.getText().trim());
		}
		
		enderecoApanheBusqueda.addFilterEqual("activo", Boolean.TRUE);
		plwEnderecoApanhe.init(enderecoApanheBusqueda, enderecoApanheList, pagingEnderecoApanhe);

        if (enderecoApanheList.getData().length == 0) {
            try {
                Messagebox.show(Labels.getLabel("MSG.ningunRegistro"),
                        		Labels.getLabel("busquedaManutencaoPacoteController.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) {
        verEnderecoApanhe(new EnderecoApanhe());
    }

	public MyListbox getHotelList() {
		return enderecoApanheList;
	}

	public void setHotelList(MyListbox hotelList) {
		this.enderecoApanheList = hotelList;
	}

	public Paging getPagingHotel() {
		return pagingEnderecoApanhe;
	}

	public void setPagingHotel(Paging pagingHotel) {
		this.pagingEnderecoApanhe = pagingHotel;
	}

	public Textbox getTxtNumoperacion() {
		return txtNumoperacion;
	}

	public void setTxtNumoperacion(Textbox txtNumoperacion) {
		this.txtNumoperacion = txtNumoperacion;
	}

	public Datebox getDtDataPacote() {
		return dtDataPacote;
	}

	public void setDtDataPacote(Datebox dtDataPacote) {
		this.dtDataPacote = dtDataPacote;
	}
}
