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

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.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.PricingClasseTarifaria;
import com.rjconsultores.ventaboletos.service.PricingClasseTarifariaService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("editarPricingClasseTarifariaController")
@Scope("prototype")
public class EditarPricingClasseTarifariaController extends MyGenericForwardComposer {

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

	@Autowired
	private PricingClasseTarifariaService pricingClasseTarifariaService;
	private Textbox desc;
	private Textbox sigla;
	private String siglaVieja;

	private MyListbox pricingClasseTarifariaList;
	private PricingClasseTarifaria pricingClasseTarifaria;

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

		pricingClasseTarifaria = (PricingClasseTarifaria) Executions.getCurrent().getArg().get("pricingClasseTarifaria");
		pricingClasseTarifariaList = (MyListbox) Executions.getCurrent().getArg().get("pricingClasseTarifariaList");

		super.doAfterCompose(comp);

		siglaVieja = pricingClasseTarifaria.getPricingClasseTarifariaId() != null ? pricingClasseTarifaria.getSigla() : "";
		if (pricingClasseTarifaria.getPricingClasseTarifariaId() != null) {
			desc.setValue(pricingClasseTarifaria.getDescClasseTarifaria());
			sigla.setValue(pricingClasseTarifaria.getSigla());
		}
	}

	public void onClick$btnAdicionarClasseTarifaria(Event ev) throws Exception {
		try {
			boolean validacion;
			if (siglaVieja.equals(sigla.getValue()))
				validacion = true;
			else
				validacion = pricingClasseTarifariaService.validacionSigla(sigla.getValue());
			if (validacion) {
				pricingClasseTarifaria.setDescClasseTarifaria(desc.getValue());
				pricingClasseTarifaria.setSigla(sigla.getValue());

				if (pricingClasseTarifaria.getPricingClasseTarifariaId() == null) {
					pricingClasseTarifaria = pricingClasseTarifariaService.suscribir(pricingClasseTarifaria);
					pricingClasseTarifariaList.addItemNovo(pricingClasseTarifaria);

					if (pricingClasseTarifaria.getPricingClasseTarifariaId() != null) {
						Messagebox.show(Labels.getLabel("editarPricingClasseTarifariaController.btnAdd.resp"),
								Labels.getLabel("editarPricingClasseTarifariaController.window.title"),
								Messagebox.OK, Messagebox.EXCLAMATION);
					}
				} else {
					pricingClasseTarifaria = pricingClasseTarifariaService.actualizacion(pricingClasseTarifaria);
					pricingClasseTarifariaList.updateItem(pricingClasseTarifaria);
				}

				closeWindow();
			} else {
				Messagebox.show(Labels.getLabel("editarPricingClasseTarifariaController.MSG.Error.sigla"),
						Labels.getLabel("editarPricingClasseTarifariaController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}

		} catch (Exception e) {
			log.error("Erro ao salvar Pricing Classe Tarifaria:", e);
			Messagebox.show(e.getMessage(),
					Labels.getLabel("editarPricingClasseTarifariaController.window.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

}
