package com.rjconsultores.ventaboletos.web.gui.controladores.seguridad;

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.util.Clients;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.entidad.FiscalImpressora;
import com.rjconsultores.ventaboletos.service.FiscalImpressoraService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

@Controller("editarFiscalImpressoraController")
@Scope("prototype")
public class EditarFiscalImpressoraController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	
	private static Logger log = Logger.getLogger(EditarFiscalImpressoraController.class);

	@Autowired
	private FiscalImpressoraService fiscalImpressoraService;
	
	private MyListbox fiscalImpressoraList;
	
	private Textbox txtMarca;
	private Textbox txtModelo;
	private Textbox txtNumserie20;
	private Textbox txtVersionsb;
	private Textbox txtCniee;
	
	private FiscalImpressora fiscalImpressora;
	
	public Textbox getTxtMarca() {
		return txtMarca;
	}

	public void setTxtMarca(Textbox txtMarca) {
		this.txtMarca = txtMarca;
	}

	public Textbox getTxtModelo() {
		return txtModelo;
	}

	public void setTxtModelo(Textbox txtModelo) {
		this.txtModelo = txtModelo;
	}

	public Textbox getTxtVersionsb() {
		return txtVersionsb;
	}

	public void setTxtVersionsb(Textbox txtVersionsb) {
		this.txtVersionsb = txtVersionsb;
	}

	public Textbox getTxtCniee() {
		return txtCniee;
	}

	public void setTxtCniee(Textbox txtCniee) {
		this.txtCniee = txtCniee;
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		fiscalImpressora = new FiscalImpressora();

		Integer fiscalimpressoraId = (Integer) Executions.getCurrent().getArg().get("fiscalimpressoraId");
		if(fiscalimpressoraId != null && fiscalimpressoraId > 0) {
			fiscalImpressora = fiscalImpressoraService.obtenerID((Integer)Executions.getCurrent().getArg().get("fiscalimpressoraId"));
		}
		fiscalImpressoraList = (MyListbox) Executions.getCurrent().getArg().get("fiscalImpressoraList");
		
        txtCniee.focus();
	}

	public FiscalImpressora getFiscalImpressora() {
		return fiscalImpressora;
	}

	public void setFiscalImpressora(FiscalImpressora fiscalImpressora) {
		this.fiscalImpressora = fiscalImpressora;
	}
	
	public void onClick$btnSalvar() {
		try {
			txtCniee.getText();
			fiscalImpressoraService.actualizacion(getFiscalImpressora());
			
			if(fiscalImpressoraList != null) {
				fiscalImpressoraList.updateItem(getFiscalImpressora());
			}
			
			Messagebox.show(
					Labels.getLabel("busquedaFiscalImpressoraController.MSG.suscribirOK"),
					Labels.getLabel("busquedaFiscalImpressoraController.window.title"),
					Messagebox.OK, Messagebox.INFORMATION);
			
			closeWindow();
		} catch (Exception ex) {
			log.info(ex.getLocalizedMessage());
			Clients.alert(ex.getLocalizedMessage(),
					Labels.getLabel("busquedaFiscalImpressoraController.window.title"), Messagebox.INFORMATION);
		}
	}

	public Textbox getTxtNumserie20() {
		return txtNumserie20;
	}

	public void setTxtNumserie20(Textbox txtNumserie20) {
		this.txtNumserie20 = txtNumserie20;
	}
	
}
