/*
 * 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.Moneda;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingImporte;
import com.rjconsultores.ventaboletos.service.MonedaService;
import com.rjconsultores.ventaboletos.service.PricingImporteService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextboxDecimal;
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.zk.ui.util.Clients;
import org.zkoss.zul.Combobox;

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

	private static final long serialVersionUID = 1L;

	@Autowired
	private PricingImporteService pricingImporteService;
	@Autowired
	private MonedaService monedaService;
	private List<Moneda> lsMoneda;
	private PricingImporte pricingImporte;
	private MyListbox pricingImporteList;
	private Combobox cmbMoneda;
	private MyTextboxDecimal importe;
	private static Logger log = Logger.getLogger(EditarPricingImporteController.class);

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

		lsMoneda = monedaService.obtenerTodos();

		pricingImporte = (PricingImporte) Executions.getCurrent().getArg().get("pricingImporte");
		pricingImporteList = (MyListbox) Executions.getCurrent().getArg().get("pricingImporteList");

		super.doAfterCompose(comp);
	}

	public void onClick$btnAdicionarImporte(Event ev) throws Exception {
		importe.getValue();
		cmbMoneda.getValue();

		try {
			if (pricingImporte.getMoneda() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowImporte.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				List<PricingImporte> lsPi = pricingImporteService.buscarMoneda(pricingImporte, pricingImporte.getMoneda());

				if (lsPi.isEmpty()) {
					if (pricingImporte.getPricingimporteId() != null) {
						pricingImporteService.actualizacion(pricingImporte);
						pricingImporteList.updateItem(pricingImporte);
					} else {
						pricingImporteService.suscribir(pricingImporte);
						pricingImporteList.addItemNovo(pricingImporte);
					}

					closeWindow();
				} else if (lsPi.size() == 1) {
					if (lsPi.get(0).getPricingimporteId().equals(pricingImporte.getPricingimporteId())) {
						pricingImporteService.actualizacion(pricingImporte);
						pricingImporteList.updateItem(pricingImporte);

						closeWindow();
					} else {
						Messagebox.show(
								Labels.getLabel("MSG.Registro.Existe"),
								Labels.getLabel("editarPricingController.windowImporte.title"),
								Messagebox.OK, Messagebox.EXCLAMATION);
					}
				} else {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.windowImporte.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				}
			}
		} catch (Exception ex) {
			log.error("",ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.windowImporte.title"),
					Messagebox.OK, Messagebox.EXCLAMATION);
		}
	}

	public void onClick$btnRemoverImporte(Event ev) {
		try {
			PricingImporte pImporte = (PricingImporte) pricingImporteList.getSelected();
			if (pImporte != null) {
				int resp = Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.borrarPerguntaImporte"),
						Labels.getLabel("editarPricingController.windowImporte.title"),
						Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

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

					pricingImporteList.removeItem(pricingImporte);
					pricingImporteService.borrar(pricingImporte);
					closeWindow();
				}
			} else {
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.windowImporte.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error(ex);
			Clients.alert(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.windowImporte.title"),
					Messagebox.EXCLAMATION);
		}

	}

	public PricingImporte getPricingImporte() {
		return pricingImporte;
	}

	public void setPricingImporte(PricingImporte pricingImporte) {
		this.pricingImporte = pricingImporte;
	}

	public MyListbox getPricingImporteList() {
		return pricingImporteList;
	}

	public void setPricingImporteList(MyListbox pricingImporteList) {
		this.pricingImporteList = pricingImporteList;
	}

	public PricingImporteService getPricingImporteService() {
		return pricingImporteService;
	}

	public void setPricingImporteService(PricingImporteService pricingImporteService) {
		this.pricingImporteService = pricingImporteService;
	}

	public List<Moneda> getLsMoneda() {
		return lsMoneda;
	}

	public void setLsMoneda(List<Moneda> lsMoneda) {
		this.lsMoneda = lsMoneda;
	}

	public MonedaService getMonedaService() {
		return monedaService;
	}

	public void setMonedaService(MonedaService monedaService) {
		this.monedaService = monedaService;
	}
}
