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

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.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Messagebox;

import com.rjconsultores.ventaboletos.entidad.TipoTarifaPacote;
import com.rjconsultores.ventaboletos.service.TipoTarifaPacoteService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextbox;

@Controller("editarTipoTarifaPacoteController")
@Scope("prototype")
public class EditarTipoTarifaPacoteController extends MyGenericForwardComposer {

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

	@Autowired
	private TipoTarifaPacoteService tipoTarifaPacoteService;

	private TipoTarifaPacote tarifa;
	private MyListbox tipoTarifaList;
	private MyTextbox txtDescTarifa;

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

		tarifa = (TipoTarifaPacote) Executions.getCurrent().getArg().get("tarifa");
		tipoTarifaList = (MyListbox) Executions.getCurrent().getArg().get("tipoTarifaList");

		if (tarifa.getTipotarifapacoteId() != null) {
			txtDescTarifa.setValue(tarifa.getDesctipotarifa());
		}
	}

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

		try {
			tarifa.setDesctipotarifa(txtDescTarifa.getValue());

			if (tarifa.getTipotarifapacoteId() == null) {
				tipoTarifaPacoteService.suscribir(tarifa);
				tipoTarifaList.addItemNovo(tarifa);
			} else {
				tipoTarifaPacoteService.actualizacion(tarifa);
			}

			closeWindow();

		} catch (WrongValueException ex) {
			throw (WrongValueException) ex;
		} catch (Exception ex) {
			log.error(ex);
			Messagebox.show(Labels.getLabel("MSG.Error").concat(" - ").concat(ex.toString()),
					Labels.getLabel("editarConfiguracionPacoteController.window.title"),
					Messagebox.OK, Messagebox.ERROR);
		}
	}

}
