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

import com.rjconsultores.ventaboletos.entidad.ClaseServicio;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingClase;
import com.rjconsultores.ventaboletos.service.ClaseServicioService;
import com.rjconsultores.ventaboletos.service.PricingClaseService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
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 java.util.List;
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.Combobox;

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

	@Autowired
	private PricingClaseService pricingClaseService;
	@Autowired
	private ClaseServicioService claseServicioService;
	private List<ClaseServicio> lsClase;
	private PricingClase pricingClase;
	private MyListbox pricingClaseList;
	private Combobox cmbClase;
	private static Logger log = Logger.getLogger(EditarPricingClaseController.class);

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

		lsClase = claseServicioService.obtenerTodos();

		pricingClase = (PricingClase) Executions.getCurrent().getArg().get("pricingClase");
		pricingClaseList = (MyListbox) Executions.getCurrent().getArg().get("pricingClaseList");

		super.doAfterCompose(comp);

		cmbClase.focus();
	}

	public void onClick$btnAdicionarClase(Event ev) throws Exception {
		cmbClase.getValue();

		if (pricingClase.getClaseServicio() == null) {
			Messagebox.show(
					Labels.getLabel("MSG.Error.combobox"),
					Labels.getLabel("editarPricingController.windowClase.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		} else {

			Boolean existe = pricingClaseService.obtenerPricingClase(pricingClase.getPricing(), (ClaseServicio) cmbClase.getSelectedItem().getValue());
			if (existe) {
				Messagebox.show(
						Labels.getLabel("MSG.Registro.Existe"),
						Labels.getLabel("editarPricingController.windowClase.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				if (pricingClase.getPricingclaseId() != null) {
					pricingClaseService.actualizacion(pricingClase);
					pricingClaseList.updateItem(pricingClase);
				} else {
					pricingClaseService.suscribir(pricingClase);
					pricingClaseList.addItem(pricingClase);
				}
				closeWindow();
			}
		}
	}

	public void onClick$btnRemoverClase(Event ev) throws InterruptedException {
		PricingClase pClase = (PricingClase) pricingClaseList.getSelected();
		if (pClase != null) {
			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaClase"),
					Labels.getLabel("editarPricingController.windowClase.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {
				pricingClase.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pricingClase.setFecmodif(Calendar.getInstance().getTime());
				pricingClase.setActivo(Pricing.INATIVO);

				pricingClaseList.removeItem(pricingClase);
				pricingClaseService.borrar(pricingClase);
				closeWindow();
			}
		} else {
			Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.selectItem"),
					Labels.getLabel("editarPricingController.windowClase.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public PricingClase getPricingClase() {
		return pricingClase;
	}

	public void setPricingClase(PricingClase pricingClase) {
		this.pricingClase = pricingClase;
	}

	public MyListbox getPricingClaseList() {
		return pricingClaseList;
	}

	public void setPricingClaseList(MyListbox pricingClaseList) {
		this.pricingClaseList = pricingClaseList;
	}

	public PricingClaseService getPricingClaseService() {
		return pricingClaseService;
	}

	public void setPricingClaseService(PricingClaseService pricingClaseService) {
		this.pricingClaseService = pricingClaseService;
	}

	public ClaseServicioService getClaseServicioService() {
		return claseServicioService;
	}

	public void setClaseServicioService(ClaseServicioService claseServicioService) {
		this.claseServicioService = claseServicioService;
	}

	public static Logger getLog() {
		return log;
	}

	public static void setLog(Logger log) {
		EditarPricingClaseController.log = log;
	}

	public List<ClaseServicio> getLsClase() {
		return lsClase;
	}

	public void setLsClase(List<ClaseServicio> lsClase) {
		this.lsClase = lsClase;
	}
}
