/*
 * 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.Marca;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingMarca;
import com.rjconsultores.ventaboletos.service.MarcaService;
import com.rjconsultores.ventaboletos.service.PricingMarcaService;
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("editarPricingMarcaController")
@Scope("prototype")
public class EditarPricingMarcaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private PricingMarcaService pricingMarcaService;
	@Autowired
	private MarcaService marcaService;
	private List<Marca> lsMarca;
	private PricingMarca pricingMarca;
	private MyListbox pricingMarcaList;
	private Combobox cmbMarca;
	private static Logger log = Logger.getLogger(EditarPricingMarcaController.class);

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		lsMarca = marcaService.buscarMarcaPorEmpresa(UsuarioLogado.getUsuarioLogado().getEmpresa());

		PricingMarca pm = (PricingMarca) Executions.getCurrent().getArg().get("pricingMarca");
		pricingMarca = (pm.getUsuarioId() == null) ? pm : pricingMarcaService.obtenerID(pm.getUsuarioId());
		pricingMarcaList = (MyListbox) Executions.getCurrent().getArg().get("pricingMarcaList");

		super.doAfterCompose(comp);

		cmbMarca.focus();
	}

	public void onClick$btnAdicionarMarca(Event ev) throws Exception {
		cmbMarca.getValue();

		try {
			if (pricingMarca.getMarca() == null) {
				Messagebox.show(
						Labels.getLabel("MSG.Error.combobox"),
						Labels.getLabel("editarPricingController.windowMarca.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				Boolean existe = pricingMarcaService.obtenerPricingMarca(pricingMarca.getPricing(), (Marca) cmbMarca.getSelectedItem().getValue());
				if (existe) {
					Messagebox.show(
							Labels.getLabel("MSG.Registro.Existe"),
							Labels.getLabel("editarPricingController.windowMarca.title"),
							Messagebox.OK, Messagebox.EXCLAMATION);
				} else {
					if (pricingMarca.getPricingmarcaId() != null) {
						pricingMarcaService.actualizacion(pricingMarca);
						pricingMarcaList.updateItem(pricingMarca);
					} else {
						pricingMarcaService.suscribir(pricingMarca);
						pricingMarcaList.addItemNovo(pricingMarca);
					}
					closeWindow();
				}
			}
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public void onClick$btnRemoverMarca(Event ev) {
		try {

			int resp = Messagebox.show(
					Labels.getLabel("editarPricingController.MSG.borrarPerguntaMarca"),
					Labels.getLabel("editarPricingController.windowMarca.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

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

				pricingMarcaList.removeItem(pricingMarca);
				pricingMarcaService.borrar(pricingMarca);
				closeWindow();
			}
		} catch (Exception ex) {
			log.error(ex);
		}

	}

	public PricingMarca getPricingMarca() {
		return pricingMarca;
	}

	public void setPricingMarca(PricingMarca pricingMarca) {
		this.pricingMarca = pricingMarca;
	}

	public MyListbox getPricingMarcaList() {
		return pricingMarcaList;
	}

	public void setPricingMarcaList(MyListbox pricingMarcaList) {
		this.pricingMarcaList = pricingMarcaList;
	}

	public PricingMarcaService getPricingMarcaService() {
		return pricingMarcaService;
	}

	public void setPricingMarcaService(PricingMarcaService pricingMarcaService) {
		this.pricingMarcaService = pricingMarcaService;
	}

	public List<Marca> getLsMarca() {
		return lsMarca;
	}

	public void setLsMarca(List<Marca> lsMarca) {
		this.lsMarca = lsMarca;
	}

	public MarcaService getMarcaService() {
		return marcaService;
	}

	public void setMarcaService(MarcaService marcaService) {
		this.marcaService = marcaService;
	}
}
