/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.gui.controladores.catalogos;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.api.Comboitem;

import com.rjconsultores.ventaboletos.entidad.ConfTotem;
import com.rjconsultores.ventaboletos.enums.SistemaEnum;
import com.rjconsultores.ventaboletos.service.ConfTotemService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

/**
 * 
 * @author Lucas
 */
@Controller("editarConfTotemController")
@Scope("prototype")
public class EditarConfTotemController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	@Autowired
	private ConfTotemService confTotemService;
	private ConfTotem confTotem;
	private MyListbox confTotemList;
	private Button btnApagar;
	private Textbox txtChave;
	private Textbox txtValor;
	private Combobox cmbSistema;
	private List<SistemaEnum> lsSistemaEnum;

	private static Logger log = LogManager.getLogger(EditarCustomController.class);

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

		confTotem = (ConfTotem) Executions.getCurrent().getArg().get("confTotem");
		confTotemList = (MyListbox) Executions.getCurrent().getArg().get("confTotemList");
		
	

		lsSistemaEnum = Arrays.asList(SistemaEnum.values());
		
		if (confTotem.getConfTotemId() == null) {
			btnApagar.setVisible(Boolean.FALSE);
		}else{
			txtChave.setDisabled(Boolean.TRUE);
			cmbSistema.setDisabled(Boolean.TRUE);
			
		}

		txtValor.focus();
	}

	public void onClick$btnSalvar(Event ev) throws InterruptedException {
		txtValor.getValue();

		try {
			
			Comboitem cbSistema = cmbSistema.getSelectedItem();

			confTotem.setActivo(Boolean.TRUE);
			confTotem.setFecmodif(Calendar.getInstance().getTime());
			confTotem.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());

			ConfTotem tempConfTotem = confTotemService.buscar(confTotem.getChave());

			if (confTotem.getConfTotemId() == null && tempConfTotem != null) {
				Messagebox.show(
						Labels.getLabel("MSG.Registro.Existe"),
						Labels.getLabel("customController.window.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			} else {
				if (confTotem.getConfTotemId() == null) {
					confTotemService.suscribir(confTotem);
					confTotemList.addItem(confTotem);
				} else {
					confTotemService.actualizacion(confTotem);
					confTotemList.updateItem(confTotem);
				}

				Messagebox.show(
						Labels.getLabel("customController.MSG.suscribirOK"),
						Labels.getLabel("customController.window.title"),
						Messagebox.OK, Messagebox.INFORMATION);

				closeWindow();
			}
		} catch (Exception ex) {
			log.error("", ex);
			Messagebox.show(
					Labels.getLabel("MSG.Error"),
					Labels.getLabel("customController.window.title"),
					Messagebox.OK, Messagebox.ERROR);
		}
	}

	public void onClick$btnApagar(Event ev) throws InterruptedException {

		int resp = Messagebox.show(
				Labels.getLabel("customController.MSG.borrarPergunta"),
				Labels.getLabel("customController.window.title"),
				Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

		if (resp == Messagebox.YES) {

			confTotemService.borrar(confTotem);

			Messagebox.show(
					Labels.getLabel("customController.MSG.borrarOK"),
					Labels.getLabel("customController.window.title"),
					Messagebox.OK, Messagebox.INFORMATION);

			confTotemList.removeItem(confTotem);

			closeWindow();
		}
	}


	public ConfTotem getCustom() {
		return confTotem;
	}

	public void setCustom(ConfTotem confTotem) {
		this.confTotem = confTotem;
	}

	public Button getBtnApagar() {
		return btnApagar;
	}

	public void setBtnApagar(Button btnApagar) {
		this.btnApagar = btnApagar;
	}

	public Textbox getTxtChave() {
		return txtChave;
	}

	public void setTxtChave(Textbox txtChave) {
		this.txtChave = txtChave;
	}

	public Textbox getTxtValor() {
		return txtValor;
	}

	public void setTxtValor(Textbox txtValor) {
		this.txtValor = txtValor;
	}

	public List<SistemaEnum> getLsSistemaEnum() {
		return lsSistemaEnum;
	}

	public void setLsSistemaEnum(List<SistemaEnum> lsSistemaEnum) {
		this.lsSistemaEnum = lsSistemaEnum;
	}

	public Combobox getCmbSistema() {
		return cmbSistema;
	}

	public void setCmbSistema(Combobox cmbSistema) {
		this.cmbSistema = cmbSistema;
	}

}
