package com.rjconsultores.ventaboletos.web.utilerias.render;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import org.zkoss.lang.Strings;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

import com.rjconsultores.ventaboletos.entidad.Voucher;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextbox;

public class RenderFaturarVoucher implements ListitemRenderer {

    public void render(Listitem lstm, Object o) throws Exception {
        Voucher vo = (Voucher) o;
        
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        NumberFormat cf = NumberFormat.getInstance();

        Listcell lc = new Listcell(vo.getVoucherId().toString());
        lc.setParent(lstm);

        lc = new Listcell(vo.getContrato().getNumContrato());        
        lc.setParent(lstm);

        if(vo.getDataValidade() != null){
        	lc = new Listcell(df.format(vo.getDataValidade()));
        }else{
        	lc = new Listcell("");
        }
        lc.setParent(lstm);

//numero Fatura        
        if( Strings.isBlank(vo.getNumFatura())){
        	lc = new Listcell();
	        MyTextbox txtFatura = new MyTextbox();
	        txtFatura.setWidth("100px;");
	        
	        txtFatura.addEventListener(Events.ON_CHANGE, new EventListener() {
	            @Override
	            public void onEvent(Event event) throws Exception {
	                vo.setNumFatura(txtFatura.getValue());
	            }
	        });
	        
	        lc.appendChild(txtFatura);
        }else {
        	lc = new Listcell(vo.getNumFatura());         	
        }
        lc.setParent(lstm);
        
      //Data Corte  
        if( Strings.isBlank(vo.getNumFatura())){
        	lc = new Listcell();
	        Datebox datCorte = new Datebox();
	        datCorte.setWidth("100px;");
	        
	        datCorte.addEventListener(Events.ON_BLUR, new EventListener() {
	            @Override
	            public void onEvent(Event event) throws Exception {
	                vo.setDataCorte(datCorte.getValue());
	            }
	        });
	        
	        lc.appendChild(datCorte);
        }else {
        	lc = new Listcell(df.format(vo.getDataCorte()));         	
        }
        lc.setParent(lstm);        

//Valor Licitado
        if(vo.getValorLicitado() != null){
        	lc = new Listcell(cf.format(vo.getValorLicitado()));        	
        }else{
        	lc = new Listcell("");
        }
        lc.setParent(lstm);

        if(vo.getValorLegalizado() != null){
        	lc = new Listcell(cf.format(vo.getValorLegalizado()));
        }else{
        	lc = new Listcell("");
        }
        lc.setParent(lstm);
        	
        lc = new Listcell(vo.getDescOrigem());      
        lc.setParent(lstm);

        lc = new Listcell(vo.getDescDestino());        
        lc.setParent(lstm);

        lstm.setAttribute("data", vo);
    }
}
