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

import java.util.Calendar;
import java.util.List;

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.Intbox;

import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingAsiento;
import com.rjconsultores.ventaboletos.service.PricingAsientoService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.ConstraintPorcentagem;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextboxDecimal;

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

	private static final long serialVersionUID = 1L;
	@Autowired
	private PricingAsientoService pricingAsientoService;
	private PricingAsiento pricingAsiento;
	private MyListbox pricingAsientoList;
	private MyTextboxDecimal porcentaje;
	private Intbox numeasiento;
	private ConstraintPorcentagem ct;
	private static Logger log = Logger.getLogger(EditarPricingAsientoController.class);

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

		ct = new ConstraintPorcentagem();
		pricingAsiento = (PricingAsiento) Executions.getCurrent().getArg().get("pricingAsiento");
		pricingAsientoList = (MyListbox) Executions.getCurrent().getArg().get("pricingAsientoList");

		super.doAfterCompose(comp);
	}

	public void onClick$btnAdicionarAsiento(Event ev) throws Exception {
		numeasiento.getValue();
		porcentaje.getValue();

		List<PricingAsiento> lsAsientos = pricingAsientoService.obtenerPricingCategoria(pricingAsiento.getPricing(), numeasiento.getValue());

		try {
			boolean exite = true;

			if (lsAsientos.isEmpty()) {
				exite = false;
			} else {
				for (PricingAsiento a : lsAsientos) {
					if (a.getPricingasientoId().equals(pricingAsiento.getPricingasientoId())) {
						exite = false;
					}
				}
			}

			if (!exite) {
				if (pricingAsiento.getPricingasientoId() != null) {
					pricingAsientoService.actualizacion(pricingAsiento);
					pricingAsientoList.updateItem(pricingAsiento);
				} else {
					pricingAsientoService.suscribir(pricingAsiento);
					pricingAsientoList.addItemNovo(pricingAsiento);
				}
				closeWindow();
			} else {
				Messagebox.show(
						Labels.getLabel("MSG.Registro.Existe"),
						Labels.getLabel("editarPricingController.windowCategoria.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public void onClick$btnRemoverAsiento(Event ev) {
		try {
			PricingAsiento pAsiento = (PricingAsiento) pricingAsientoList.getSelected();
			if (pAsiento != null) {
				int resp = Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.borrarPerguntaAsiento"),
						Labels.getLabel("editarPricingController.windowAsiento.title"),
						Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

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

					pricingAsientoList.removeItem(pricingAsiento);
					pricingAsientoService.borrar(pricingAsiento);
					closeWindow();
				}
			} else {
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.windowAsiento.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error(ex);
		}

	}

	public PricingAsiento getPricingAsiento() {
		return pricingAsiento;
	}

	public void setPricingAsiento(PricingAsiento pricingAsiento) {
		this.pricingAsiento = pricingAsiento;
	}

	public MyListbox getPricingAsientoList() {
		return pricingAsientoList;
	}

	public void setPricingAsientoList(MyListbox pricingAsientoList) {
		this.pricingAsientoList = pricingAsientoList;
	}

	public PricingAsientoService getPricingAsientoService() {
		return pricingAsientoService;
	}

	public void setPricingAsientoService(PricingAsientoService pricingAsientoService) {
		this.pricingAsientoService = pricingAsientoService;
	}

	/**
	 * @return the ct
	 */
	public ConstraintPorcentagem getCt() {
		return ct;
	}

	/**
	 * @param ct
	 *            the ct to set
	 */
	public void setCt(ConstraintPorcentagem ct) {
		this.ct = ct;
	}
}
