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

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.event.Event;
import org.zkoss.zul.Messagebox;

import com.rjconsultores.ventaboletos.entidad.PricingCtrl;
import com.rjconsultores.ventaboletos.service.PricingCtrlService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;

/**
 * 
 * @author Rafius
 */
@Controller("editarPricingCtrlController")
@Scope("prototype")
public class EditarPricingCtrlController extends MyGenericForwardComposer {

	private static Logger log = LogManager.getLogger(EditarPricingCtrlController.class);

	private static final long serialVersionUID = 1L;

	@Autowired
	private PricingCtrlService pricingCtrlService;
	
	private PricingCtrl pricingCtrl;
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		pricingCtrl = pricingCtrlService.obtenerPricingCtrl();
		if(pricingCtrl == null) {
			pricingCtrl = new PricingCtrl();
		}
		
	}
	
	public void onClick$btnSalvar(Event ev) throws InterruptedException {
		try {
			
			pricingCtrl = pricingCtrlService.suscribirOrActualizacion(pricingCtrl);
			
			Messagebox.show(
					Labels.getLabel("editarPricingCtrlController.MSG.suscribirOK"),
					Labels.getLabel("editarPricingCtrlController.window.title"),
					Messagebox.OK, Messagebox.INFORMATION);
			
			closeWindow();
			
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			Messagebox.show(e.getLocalizedMessage(), Labels.getLabel("editarPricingCtrlController.window.title"), Messagebox.OK, Messagebox.ERROR);
		}
	}
	
	public void onClick$btnApagar(Event ev) throws InterruptedException {
		try {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingCtrlController.MSG.borrarPergunta"),
					Labels.getLabel("editarPricingCtrlController.window.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {
				
				pricingCtrlService.borrar(pricingCtrl);

				Messagebox.show(
						Labels.getLabel("editarPricingCtrlController.MSG.borrarOK"),
						Labels.getLabel("editarPricingCtrlController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

				closeWindow();
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			Messagebox.show(e.getLocalizedMessage(), Labels.getLabel("editarPricingCtrlController.window.title"), Messagebox.OK, Messagebox.ERROR);
		}
	}
	
	public String getStatusAtual() {
		return Labels.getLabel("editarPricingCtrlController.lbStatusAtual.value",
							    pricingCtrl != null && pricingCtrl.getPricingctrlId() != null ? new String[] {Labels.getLabel("MSG.ATIVADO")} : new String[] {Labels.getLabel("MSG.DESATIVADO")});
	}
	
	public Boolean getAtivado() {
		return pricingCtrl != null && pricingCtrl.getPricingctrlId() != null;
	}
	
	public Boolean getDesativado() {
		return !getAtivado();
	}

}
