/*
 * 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.Ruta;
import com.rjconsultores.ventaboletos.entidad.PricingRuta;
import com.rjconsultores.ventaboletos.service.RutaService;
import com.rjconsultores.ventaboletos.service.PricingRutaService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import java.util.ArrayList;
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("editarPricingRutaController")
@Scope("prototype")
public class EditarPricingRutaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private PricingRutaService pricingRutaService;
	@Autowired
	private RutaService rutaService;
	private List<Ruta> lsRuta;
	private PricingRuta pricingRuta;
	private MyListbox pricingRutaList;
	private Combobox cmbRuta;
	private static Logger log = Logger.getLogger(EditarPricingRutaController.class);

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

		pricingRuta = (PricingRuta) Executions.getCurrent().getArg().get("pricingRuta");
		pricingRutaList = (MyListbox) Executions.getCurrent().getArg().get("pricingRutaList");

		lsRuta = new ArrayList<Ruta>();
		lsRuta.add(rutaService.obtenerID(-1));
		lsRuta.addAll(rutaService.obtenerPorEmpresa(pricingRuta.getPricing().getEmpresa())) ;

		super.doAfterCompose(comp);

		cmbRuta.focus();

	}

	public void onClick$btnAdicionarRuta(Event ev) throws Exception {
		cmbRuta.getValue();

		try {

			if (pricingRuta.getRuta() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowRuta.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				Boolean existe = pricingRutaService.obtenerPricingRuta(pricingRuta.getPricing(), (Ruta) cmbRuta.getSelectedItem().getValue());

				if (existe) {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.windowRuta.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				} else {
					if (pricingRuta.getPricingrutaId() != null) {
						pricingRutaService.actualizacion(pricingRuta);
						pricingRutaList.updateItem(pricingRuta);
					} else {
						pricingRutaService.suscribir(pricingRuta);
						pricingRutaList.addItemNovo(pricingRuta);
					}
					closeWindow();
				}
			}
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public void onClick$btnRemoverRuta(Event ev) {
		try {
			PricingRuta pRuta = (PricingRuta) pricingRutaList.getSelected();
			if (pRuta != null) {
				int resp = Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.borrarPerguntaRuta"),
						Labels.getLabel("editarPricingController.windowRuta.title"),
						Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

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

					pricingRutaList.removeItem(pricingRuta);
					pricingRutaService.borrar(pricingRuta);
					closeWindow();
				}
			} else {
				Messagebox.show(
						Labels.getLabel("editarPricingController.MSG.selectItem"),
						Labels.getLabel("editarPricingController.windowRuta.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error(ex);
		}

	}

	public PricingRuta getPricingRuta() {
		return pricingRuta;
	}

	public void setPricingRuta(PricingRuta pricingRuta) {
		this.pricingRuta = pricingRuta;
	}

	public MyListbox getPricingRutaList() {
		return pricingRutaList;
	}

	public void setPricingRutaList(MyListbox pricingRutaList) {
		this.pricingRutaList = pricingRutaList;
	}

	public PricingRutaService getPricingRutaService() {
		return pricingRutaService;
	}

	public void setPricingRutaService(PricingRutaService pricingRutaService) {
		this.pricingRutaService = pricingRutaService;
	}

	public List<Ruta> getLsRuta() {
		return lsRuta;
	}

	public void setLsRuta(List<Ruta> lsRuta) {
		this.lsRuta = lsRuta;
	}

	public RutaService getRutaService() {
		return rutaService;
	}

	public void setRutaService(RutaService rutaService) {
		this.rutaService = rutaService;
	}

	public Combobox getCmbRuta() {
		return cmbRuta;
	}

	public void setCmbRuta(Combobox cmbRuta) {
		this.cmbRuta = cmbRuta;
	}
}
