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

import com.rjconsultores.ventaboletos.entidad.CuponConvenio;
import com.rjconsultores.ventaboletos.entidad.TipoConvenio;
import com.rjconsultores.ventaboletos.service.CuponConvenioService;
import com.rjconsultores.ventaboletos.service.TipoConvenioService;
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.RenderCuponConvenio;
import java.util.HashMap;
import java.util.List;
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.Combobox;
import org.zkoss.zul.Paging;

/**
 *
 * @author Shiro
 */
@Controller("busquedaCuponConvenioController")
@Scope("prototype")
public class BusquedaCuponConvenioController extends MyGenericForwardComposer {

    @Autowired
    private transient PagedListWrapper<CuponConvenio> plwCuponConvenio;
    @Autowired
    private TipoConvenioService tipoConService;
    @Autowired
    private CuponConvenioService cuponConService;
    private MyListbox CuponConvenioList;
    private Paging pagingCuponC;
    private Combobox cmbTipoConvenio;
    private List<TipoConvenio> tipoConList;

    public CuponConvenioService getCuponConService() {
        return cuponConService;
    }

    public void setCuponConService(CuponConvenioService cuponConService) {
        this.cuponConService = cuponConService;
    }

    public List<TipoConvenio> getTipoConList() {
        return tipoConList;
    }

    public void setTipoConList(List<TipoConvenio> tipoConList) {
        this.tipoConList = tipoConList;
    }

    public TipoConvenioService getTipoConService() {
        return tipoConService;
    }

    public void setTipoConService(TipoConvenioService tipoConService) {
        this.tipoConService = tipoConService;
    }

    public MyListbox getCuponConvenioList() {
        return CuponConvenioList;
    }

    public void setCuponConvenioList(MyListbox CuponConvenioList) {
        this.CuponConvenioList = CuponConvenioList;
    }

    public Combobox getCmbTipoConvenio() {
        return cmbTipoConvenio;
    }

    public void setCmbTipoConvenio(Combobox cmbTipoConvenio) {
        this.cmbTipoConvenio = cmbTipoConvenio;
    }

    public Paging getPagingCuponC() {
        return pagingCuponC;
    }

    public void setPagingCuponC(Paging pagingCuponC) {
        this.pagingCuponC = pagingCuponC;
    }

    public PagedListWrapper<CuponConvenio> getPlwCuponConvenio() {
        return plwCuponConvenio;
    }

    public void setPlwCuponConvenio(PagedListWrapper<CuponConvenio> plwCuponConvenio) {
        this.plwCuponConvenio = plwCuponConvenio;
    }

    public void onClick$btnPesquisa(Event ev) throws InterruptedException {
        TipoConvenio tipoConvenio = (TipoConvenio) (cmbTipoConvenio.getSelectedItem() != null ? cmbTipoConvenio.getSelectedItem().getValue() : null);

        List<CuponConvenio> cuponConveniosList = cuponConService.pesquisar(tipoConvenio);
        CuponConvenioList.setData(cuponConveniosList);
    }

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

    public void onClick$btnNovo(Event ev) {
        verCuponConvenio(new CuponConvenio());

    }

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

        tipoConList = tipoConService.obtenerTodos();
        super.doAfterCompose(comp);

        CuponConvenioList.setItemRenderer(new RenderCuponConvenio());
        CuponConvenioList.addEventListener("onDoubleClick", new EventListener() {

            @Override
            public void onEvent(Event event) throws Exception {
                CuponConvenio cc = (CuponConvenio) CuponConvenioList.getSelected();
                verCuponConvenio(cc);
            }
        });
        refreshLista();
    }

    private void verCuponConvenio(CuponConvenio cc) {
        if (cc == null) {
            return;
        }

        Map args = new HashMap();
        args.put("cc", cc);
        args.put("CuponConvenioList", CuponConvenioList);

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

    private void refreshLista() {
        HibernateSearchObject<CuponConvenio> cupon =
                new HibernateSearchObject<CuponConvenio>(CuponConvenio.class, pagingCuponC.getPageSize());

        cupon.addFilterEqual("activo", Boolean.TRUE);
        cupon.addFilterNotEqual("cuponConvenioID", -1);

        cupon.addSortAsc("tipoConvenio.descTipo");

        plwCuponConvenio.init(cupon, CuponConvenioList, pagingCuponC);
    }
}
