/*
 * 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 java.util.Calendar;
import java.util.List;
import org.apache.log4j.Logger;
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.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Messagebox;

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

    @Autowired
    private CuponConvenioService cuponConService;
    @Autowired
    private TipoConvenioService tipoConService;
    private CuponConvenio cc;
    private MyListbox CuponConvenioList;
    private Combobox cmbTipoConvenio;
    private Intbox folioInicio;
    private Intbox folioFin;
    private List<TipoConvenio> tipoConvenioList;
    private static Logger log = Logger.getLogger(EditarCuponConvenioController.class);

    public Combobox getCmbTipoConvenio() {
        return cmbTipoConvenio;
    }

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

    public MyListbox getCuponConvenioList() {
        return CuponConvenioList;
    }

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

    public CuponConvenio getCc() {
        return cc;
    }

    public void setCc(CuponConvenio cc) {
        this.cc = cc;
    }

    public CuponConvenioService getCuponConService() {
        return cuponConService;
    }

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

    public Intbox getFolioFin() {
        return folioFin;
    }

    public void setFolioFin(Intbox folioFin) {
        this.folioFin = folioFin;
    }

    public Intbox getFolioInicio() {
        return folioInicio;
    }

    public void setFolioInicio(Intbox folioInicio) {
        this.folioInicio = folioInicio;
    }

    public TipoConvenioService getTipoConService() {
        return tipoConService;
    }

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

    public List<TipoConvenio> getTipoConvenioList() {
        return tipoConvenioList;
    }

    public void setTipoConvenioList(List<TipoConvenio> tipoConvenioList) {
        this.tipoConvenioList = tipoConvenioList;
    }

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

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

        cc = (CuponConvenio) Executions.getCurrent().getArg().get("cc");
        CuponConvenioList = (MyListbox) Executions.getCurrent().getArg().get("CuponConvenioList");


    }

    public void onClick$btnSalvar(Event ev) throws InterruptedException {

        try {
            cc.setActivo(Boolean.TRUE);
            cc.setFecmodif(Calendar.getInstance().getTime());
            cc.setUsuarioId(0);
            cc.setTipoConvenio((TipoConvenio) cmbTipoConvenio.getSelectedItem().getValue());
            cc.setFolioInicio(folioInicio.getValue());
            cc.setFolioFinal(folioFin.getValue());

            if (cc.getCuponConvenioID() == null) {
                cuponConService.suscribir(cc);
                CuponConvenioList.addItem(cc);
            } else {
                cuponConService.actualizacion(cc);
                CuponConvenioList.updateItem(cc);
            }

            Messagebox.show(
                    Labels.getLabel("editarCuponConvenioController.MSG.suscribirOK"),
                    Labels.getLabel("editarCuponConvenioController.window.title"),
                    Messagebox.OK, Messagebox.INFORMATION);

            closeWindow();
        } catch (Exception ex) {
            log.error("editarCuponConvenioController: " + ex);
            Messagebox.show(
                    Labels.getLabel("MSG.Error"),
                    Labels.getLabel("editarCuponConvenioController.window.title"),
                    Messagebox.OK, Messagebox.ERROR);
        }
    }

    public void onClick$btnApagar(Event ev) throws InterruptedException {
        int resp = Messagebox.show(
                Labels.getLabel("editarCuponConvenioController.MSG.BorrarPergunta"),
                Labels.getLabel("editarCuponConvenioController.window.title"),
                Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

        if (resp == Messagebox.YES) {

            cuponConService.borrar(cc);

            Messagebox.show(
                    Labels.getLabel("editarCuponConvenioController.MSG.BorrarOK"),
                    Labels.getLabel("editarCuponConvenioController.window.title"),
                    Messagebox.OK, Messagebox.INFORMATION);

            CuponConvenioList.removeItem(cc);

            closeWindow();
        }
    }
}
