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

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

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.event.Event;
import org.zkoss.zul.Button;
import org.zkoss.zul.Intbox;

import com.rjconsultores.ventaboletos.entidad.OrgaoConcedente;
import com.rjconsultores.ventaboletos.entidad.TaxaEmbarqueKm;
import com.rjconsultores.ventaboletos.service.OrgaoConcedenteService;
import com.rjconsultores.ventaboletos.service.TaxaEmbarqueKmService;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyComboboxEstandar;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;
import com.rjconsultores.ventaboletos.web.utilerias.MyTextboxDecimal;
import com.rjconsultores.ventaboletos.web.utilerias.render.RenderTaxaEmbarqueKm;

/**
 *
 * @author Administrador
 */
@Controller("editarTaxaEmbarqueKmController")
@Scope("prototype")
public class EditarTaxaEmbarqueKmController extends MyGenericForwardComposer {

    @Autowired
    private TaxaEmbarqueKmService taxaEmbarqueKmService;
    @Autowired
    private OrgaoConcedenteService orgaoConcedenteService;
    private OrgaoConcedente orgao;
    private TaxaEmbarqueKm taxaEmbarqueKm;
    private List<TaxaEmbarqueKm> lsTaxaEmbarqueKm;
    private List<OrgaoConcedente> lsOrgaoConcedente;
    private MyListbox taxaEmbarqueKmList;
    private MyComboboxEstandar cmbOrgao;
    private Button btnApagar;
    private Button btnAdicionarTaxaEmbarqueKm;
    private Intbox txtKm;
    private MyTextboxDecimal txtValor;
    private static Logger log = Logger.getLogger(EditarTaxaEmbarqueKmController.class);

    public Button getBtnApagar() {
        return btnApagar;
    }

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

    public TaxaEmbarqueKm getTaxaEmbarqueKm() {
        return taxaEmbarqueKm;
    }

    public void setTaxaEmbarqueKm(TaxaEmbarqueKm taxaEmbarqueKm) {
        this.taxaEmbarqueKm = taxaEmbarqueKm;
    }

    public Button getBtnAdicionarTaxaEmbarqueKm() {
        return btnAdicionarTaxaEmbarqueKm;
    }

    public void setBtnAdicionarTaxaEmbarqueKm(Button btnAdicionarTaxaEmbarqueKm) {
        this.btnAdicionarTaxaEmbarqueKm = btnAdicionarTaxaEmbarqueKm;
    }

    public List<OrgaoConcedente> getLsOrgaoConcedente() {
        return lsOrgaoConcedente;
    }

    public void setLsOrgaoConcedente(List<OrgaoConcedente> lsOrgaoConcedente) {
        this.lsOrgaoConcedente = lsOrgaoConcedente;
    }

    public List<TaxaEmbarqueKm> getLsTaxaEmbarqueKm() {
        return lsTaxaEmbarqueKm;
    }

    public void setLsTaxaEmbarqueKm(List<TaxaEmbarqueKm> lsTaxaEmbarqueKm) {
        this.lsTaxaEmbarqueKm = lsTaxaEmbarqueKm;
    }

    public MyListbox getTaxaEmbarqueKmList() {
        return taxaEmbarqueKmList;
    }

    public void setTaxaEmbarqueKmList(MyListbox taxaEmbarqueKmList) {
        this.taxaEmbarqueKmList = taxaEmbarqueKmList;
    }

    public OrgaoConcedente getOrgao() {
        return orgao;
    }

    public void setOrgao(OrgaoConcedente orgao) {
        this.orgao = orgao;
    }

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

        lsOrgaoConcedente = orgaoConcedenteService.obtenerTodos();

        super.doAfterCompose(comp);

        orgao = (OrgaoConcedente) Executions.getCurrent().getArg().get("orgao");
        taxaEmbarqueKmList.setItemRenderer(new RenderTaxaEmbarqueKm());
        lsTaxaEmbarqueKm = new ArrayList<TaxaEmbarqueKm>();

        if (orgao != null && orgao.getOrgaoConcedenteId() != null) {
            lsTaxaEmbarqueKm = taxaEmbarqueKmService.buscarPorOrgao(orgao);
            taxaEmbarqueKmList.setData(lsTaxaEmbarqueKm);
            cmbOrgao.setDisabled(Boolean.TRUE);
            cmbOrgao.setValue(orgao.getDescOrgao());
        }
    }

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

        try {
            if (lsTaxaEmbarqueKm.isEmpty()) {
                Messagebox.show(
                        Labels.getLabel("editarTaxaEmbarqueKmController.MSG.necessaitaInfromar.value"),
                        Labels.getLabel("editarTaxaEmbarqueKmController.window.title"),
                        Messagebox.OK, Messagebox.EXCLAMATION);
                return;
            }

            for (TaxaEmbarqueKm km : lsTaxaEmbarqueKm) {
                if (km.getTaxaembarquekmId() == null) {
                    taxaEmbarqueKmService.suscribir(km);
                }
            }
            Messagebox.show(
                    Labels.getLabel("editarTaxaEmbarqueKmController.MSG.suscribirOK"),
                    Labels.getLabel("editarTaxaEmbarqueKmController.window.title"),
                    Messagebox.OK, Messagebox.INFORMATION);

            closeWindow();


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

        }
    }

    public void onClick$btnApagar(Event ev) throws InterruptedException {
        try {
            int resp = Messagebox.show(
                    Labels.getLabel("editarTaxaEmbarqueKmController.MSG.borrarPergunta"),
                    Labels.getLabel("editarTaxaEmbarqueKmController.window.title"),
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

            if (resp == Messagebox.YES) {

                taxaEmbarqueKmService.borrar(taxaEmbarqueKm);

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

                taxaEmbarqueKmList.removeItem(taxaEmbarqueKm);

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

    public void onClick$btnAdicionarTaxaEmbarqueKm(Event ev) throws InterruptedException {
        cmbOrgao.getValue();
        txtKm.getValue();
        txtValor.getValue();

        if (lsTaxaEmbarqueKm.isEmpty()) {
            lsTaxaEmbarqueKm = taxaEmbarqueKmService.buscarPorOrgao( (OrgaoConcedente) cmbOrgao.getSelectedItem().getValue());
            taxaEmbarqueKmList.setData(lsTaxaEmbarqueKm);
        }

        //ChecandoDuplicado
        boolean existe = false;
        for (TaxaEmbarqueKm km : lsTaxaEmbarqueKm) {
            if (km.getKmate().equals(txtKm.getValue())) {
                existe = true;
            }
        }

        if (existe) {
            Messagebox.show(
                    Labels.getLabel("editarTaxaEmbarqueKmController.MSG.existe"),
                    Labels.getLabel("editarTaxaEmbarqueKmController.window.title"),
                    Messagebox.OK, Messagebox.INFORMATION);
            
            limparInput();
            
            return;
        }

        TaxaEmbarqueKm km = new TaxaEmbarqueKm();
        km.setOrgaoconcedenteId((OrgaoConcedente) cmbOrgao.getSelectedItem().getValue());
        km.setKmate(txtKm.getValue());
        km.setValortaxa(txtValor.getValueDecimal());
        km.setActivo(true);
        km.setFecmodif(Calendar.getInstance().getTime());
        km.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());

        lsTaxaEmbarqueKm.add(km);
        taxaEmbarqueKmList.setData(lsTaxaEmbarqueKm);

        cmbOrgao.setDisabled(Boolean.TRUE);
        limparInput();

    }

    private void limparInput() {
        txtKm.setConstraint("");
        txtValor.setConstraint("");
        txtKm.setText("");
        txtValor.setText("");
        txtKm.setConstraint("no empty, no zero, no negative");
        txtValor.setConstraint("no empty, no zero, no negative");
    }

    public void onClick$btnRemoverTaxaEmbarqueKm(Event ev) {
        TaxaEmbarqueKm cd = (TaxaEmbarqueKm) taxaEmbarqueKmList.getSelected();
        if (cd != null) {
            cd.setActivo(Boolean.FALSE);
            cd.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
            cd.setFecmodif(Calendar.getInstance().getTime());
            taxaEmbarqueKmService.actualizacion(cd);

            lsTaxaEmbarqueKm.remove(cd);
            taxaEmbarqueKmList.setData(lsTaxaEmbarqueKm);
        }
    }
}
