/*
 * 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.TipoConvenio;
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.MyTextbox;
import java.util.Calendar;
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.Messagebox;
import org.zkoss.zul.Textbox;

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

    @Autowired
    private TipoConvenioService tipoConvenioService;
    private TipoConvenio tc;
    private MyListbox tcList;
    private MyTextbox txtNome;
    private static Logger log = Logger.getLogger(EditarTipoConvenioController.class);

    public TipoConvenio getTc() {
        return tc;
    }

    public void setTc(TipoConvenio tc) {
        this.tc = tc;
    }

    public MyListbox getTcList() {
        return tcList;
    }

    public void setTcList(MyListbox tcList) {
        this.tcList = tcList;
    }

    public TipoConvenioService getTipoConvenioService() {
        return tipoConvenioService;
    }

    public void setTipoConvenioService(TipoConvenioService tipoConvenioService) {
        this.tipoConvenioService = tipoConvenioService;
    }

    public MyTextbox getTxtNome() {
        return txtNome;
    }

    public void setTxtNome(MyTextbox txtNome) {
        this.txtNome = txtNome;
    }

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

        tc = (TipoConvenio) Executions.getCurrent().getArg().get("tc");
        tcList = (MyListbox) Executions.getCurrent().getArg().get("tConvenioList");


        txtNome.focus();
    }

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

        txtNome.getValue();

        try {
            tc.setActivo(Boolean.TRUE);
            tc.setFecmodif(Calendar.getInstance().getTime());
            tc.setUsuarioId(0);
            ///tc.setDescTipo(txtNome.getValue());

            if (tc.getTipoConvenioID() == null) {
                tipoConvenioService.suscribir(tc);
                tcList.addItem(tc);
            } else {
                tipoConvenioService.actualizacion(tc);
                tcList.updateItem(tc);
            }

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

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

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

            if (resp == Messagebox.YES) {

                tipoConvenioService.borrar(tc);

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

                tcList.removeItem(tc);

                closeWindow();
            }
        } catch (Exception ex) {
            log.error(ex);
        }
    }
}
