/*
 * 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.Combobox;

import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingTipoServicio;
import com.rjconsultores.ventaboletos.entidad.TipoServicio;
import com.rjconsultores.ventaboletos.service.PricingTipoServicioService;
import com.rjconsultores.ventaboletos.service.TipoServicioService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

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

	private static final long serialVersionUID = 1L;

	@Autowired
	private PricingTipoServicioService pricingTipoServicioService;
	@Autowired
	private TipoServicioService tipoServicioService;
	private List<TipoServicio> lsTipoServicio;
	private PricingTipoServicio pricingTipoServicio;
	private MyListbox pricingTipoServicioList;
	private Combobox cmbTipoServicio;
	private static Logger log = Logger.getLogger(EditarPricingTipoServicioController.class);

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

		lsTipoServicio = tipoServicioService.obtenerTodos();

		pricingTipoServicio = (PricingTipoServicio) Executions.getCurrent().getArg().get("pricingTipoServicio");
		pricingTipoServicioList = (MyListbox) Executions.getCurrent().getArg().get("pricingTipoServicioList");

		super.doAfterCompose(comp);

		cmbTipoServicio.focus();
	}

	public void onClick$btnAdicionarTipoServicio(Event ev) throws Exception {
		cmbTipoServicio.getValue();

		try {

			if (pricingTipoServicio.getTipoServicio() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowTipoServicio.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {

				Boolean existe = pricingTipoServicioService.obtenerPricingTipoServicio(pricingTipoServicio.getPricing(), (TipoServicio) cmbTipoServicio.getSelectedItem().getValue());
				if (existe) {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.windowTipoServicio.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				} else {
					if (pricingTipoServicio.getPricingtiposervicioId() != null) {
						pricingTipoServicioService.actualizacion(pricingTipoServicio);
						pricingTipoServicioList.updateItem(pricingTipoServicio);
					} else {
						pricingTipoServicioService.suscribir(pricingTipoServicio);
						pricingTipoServicioList.addItem(pricingTipoServicio);
					}
					closeWindow();
				}
			}
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public void onClick$btnRemoverTipoServicio(Event ev) {
		try {
			PricingTipoServicio pTipoServicio = (PricingTipoServicio) pricingTipoServicioList.getSelected();
			if (pTipoServicio != null) {
				int resp = Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.borrarPerguntaTipoServicio"),
						Labels.getLabel("editarPricingController.windowTipoServicio.title"),
						Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

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

					pricingTipoServicioList.removeItem(pricingTipoServicio);
					pricingTipoServicioService.borrar(pricingTipoServicio);
					closeWindow();
				}
			} else {
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.windowTipoServicio.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error(ex);
		}

	}

	public PricingTipoServicio getPricingTipoServicio() {
		return pricingTipoServicio;
	}

	public void setPricingTipoServicio(PricingTipoServicio pricingTipoServicio) {
		this.pricingTipoServicio = pricingTipoServicio;
	}

	public MyListbox getPricingTipoServicioList() {
		return pricingTipoServicioList;
	}

	public void setPricingTipoServicioList(MyListbox pricingTipoServicioList) {
		this.pricingTipoServicioList = pricingTipoServicioList;
	}

	public PricingTipoServicioService getPricingTipoServicioService() {
		return pricingTipoServicioService;
	}

	public void setPricingTipoServicioService(PricingTipoServicioService pricingTipoServicioService) {
		this.pricingTipoServicioService = pricingTipoServicioService;
	}

	public TipoServicioService getTipoServicioService() {
		return tipoServicioService;
	}

	public void setTipoServicioService(TipoServicioService tipoServicioService) {
		this.tipoServicioService = tipoServicioService;
	}

	public static Logger getLog() {
		return log;
	}

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

	public List<TipoServicio> getLsTipoServicio() {
		return lsTipoServicio;
	}

	public void setLsTipoServicio(List<TipoServicio> lsTipoServicio) {
		this.lsTipoServicio = lsTipoServicio;
	}
}
