/*
 * 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.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingTipoPtoVta;
import com.rjconsultores.ventaboletos.entidad.TipoPuntoVenta;
import com.rjconsultores.ventaboletos.service.PricingTipoPtoVtaService;
import com.rjconsultores.ventaboletos.service.TipoPuntoVentaService;
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("editarPricingTipoPtoVtaController")
@Scope("prototype")
public class EditarPricingTipoPtoVtaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private PricingTipoPtoVtaService pricingTipoPtoVtaService;
	@Autowired
	private TipoPuntoVentaService tipoPtoVtaService;
	private List<TipoPuntoVenta> lsTipoPtoVta;
	private PricingTipoPtoVta pricingTipoPtoVta;
	private MyListbox pricingTipoPtoVtaList;
	private Combobox cmbTipoPtoVta;
	private static Logger log = Logger.getLogger(EditarPricingTipoPtoVtaController.class);

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

		lsTipoPtoVta = tipoPtoVtaService.obtenerTodos();

		pricingTipoPtoVta = (PricingTipoPtoVta) Executions.getCurrent().getArg().get("pricingTipoPtoVta");
		pricingTipoPtoVtaList = (MyListbox) Executions.getCurrent().getArg().get("pricingTipoPtoVtaList");

		super.doAfterCompose(comp);

		cmbTipoPtoVta.focus();

	}

	public void onClick$btnAdicionarTipoPtoVta(Event ev) throws Exception {
		cmbTipoPtoVta.getValue();

		try {
			if (pricingTipoPtoVta.getTipoPtovta() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowTipoPtoVta.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				Boolean existe = pricingTipoPtoVtaService.obtenerPricingTipoPuntoVenta(pricingTipoPtoVta.getPricing(), (TipoPuntoVenta) cmbTipoPtoVta.getSelectedItem().getValue());

				if (existe) {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.windowTipoPtoVta.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				} else {
					if (pricingTipoPtoVta.getPricingtipoptovtaId() != null) {
						pricingTipoPtoVtaService.actualizacion(pricingTipoPtoVta);
						pricingTipoPtoVtaList.updateItem(pricingTipoPtoVta);
					} else {
						pricingTipoPtoVtaService.suscribir(pricingTipoPtoVta);
						pricingTipoPtoVtaList.addItemNovo(pricingTipoPtoVta);
					}
					closeWindow();
				}
			}
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public void onClick$btnRemoverTipoPtoVta(Event ev) {
		try {
			PricingTipoPtoVta pTipoPtoVta = (PricingTipoPtoVta) pricingTipoPtoVtaList.getSelected();
			if (pTipoPtoVta != null) {
				int resp = Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.borrarPerguntaTipoPtoVta"),
						Labels.getLabel("editarPricingController.windowTipoPtoVta.title"),
						Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

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

					pricingTipoPtoVtaList.removeItem(pricingTipoPtoVta);
					pricingTipoPtoVtaService.borrar(pricingTipoPtoVta);
					closeWindow();
				}
			} else {
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.windowTipoPtoVta.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error(ex);
		}

	}

	public PricingTipoPtoVta getPricingTipoPtoVta() {
		return pricingTipoPtoVta;
	}

	public void setPricingTipoPtoVta(PricingTipoPtoVta pricingTipoPtoVta) {
		this.pricingTipoPtoVta = pricingTipoPtoVta;
	}

	public MyListbox getPricingTipoPtoVtaList() {
		return pricingTipoPtoVtaList;
	}

	public void setPricingTipoPtoVtaList(MyListbox pricingTipoPtoVtaList) {
		this.pricingTipoPtoVtaList = pricingTipoPtoVtaList;
	}

	public PricingTipoPtoVtaService getPricingTipoPtoVtaService() {
		return pricingTipoPtoVtaService;
	}

	public void setPricingTipoPtoVtaService(PricingTipoPtoVtaService pricingTipoPtoVtaService) {
		this.pricingTipoPtoVtaService = pricingTipoPtoVtaService;
	}

	public List<TipoPuntoVenta> getLsTipoPtoVta() {
		return lsTipoPtoVta;
	}

	public void setLsTipoPtoVta(List<TipoPuntoVenta> lsTipoPtoVta) {
		this.lsTipoPtoVta = lsTipoPtoVta;
	}

	public TipoPuntoVentaService getTipoPtoVtaService() {
		return tipoPtoVtaService;
	}

	public void setTipoPtoVtaService(TipoPuntoVentaService tipoPtoVtaService) {
		this.tipoPtoVtaService = tipoPtoVtaService;
	}
}
