﻿function jEstados()
{
   
    var jDT;
    var tpl;
    var mostraResultado = false;
    var retorno;
    
    var render = {
        main: function()
        {
            tpl.common.tpl_main.link = 'novo estado';
            tpl.common.tpl_main.onclick = 'javascript:jsEstados.exec.novoCadastro()';
            tpl.common.tpl_main.link2 = 'listar estados';
            tpl.common.tpl_main.onclick2 = 'javascript:jsEstados.exec.main()';
            return tpl.common.tpl_main.getHtml();
        },
        resultadoLista: function()
        {
            var sb = new StringBuilder();
            
            for (var i = 0; i < parseInt(jDT.getTotRegs()); i++)
            {
                tpl.estados.tpl_trResultado.nome = jDT.getDrValue(i, "Nome");
                tpl.estados.tpl_trResultado.sigla = jDT.getDrValue(i, "Uf");
                tpl.estados.tpl_trResultado.onclickEditar = 'javascript:jsEstados.exec.editar(' + i + ')';
                tpl.estados.tpl_trResultado.onclickExcluir = 'javascript:jsEstados.exec.excluir(' + parseInt(jDT.getDrValue(i, "CodEstado"))+','+"\'"+ jDT.getDrValue(i, "Uf")+"\'"+')';
                sb.append(tpl.estados.tpl_trResultado.getHtml());
            };
            //tpl.estados.tpl_mainResultado.titulo = "Lista de Estados";
            tpl.estados.tpl_mainResultado.trResultado = sb.toString();
            return tpl.estados.tpl_mainResultado.getHtml();
        },
        novoCadastro: function(pTitulo)
        {
            tpl.estados.tpl_formCadastro.titulo = pTitulo;
            tpl.estados.tpl_formCadastro.onclick = 'javascript:jsEstados.exec.inserir();';
            return tpl.estados.tpl_formCadastro.getHtml();
        },
        optionEstados: function(pDatatable)
		{
		    pDatatable.sort("Uf", false);
		    
		   	var sb = new StringBuilder();
			sb.append(jsCommon.exec.getOptionSelecione());
			
			var total = pDatatable.getTotRegs();
			for(var i = 0; i < total; i++)
			{			
				tpl.tpl_option.value = pDatatable.getDrValue(i, "CodEstado");
				tpl.tpl_option.text = pDatatable.getDrValue(i, "Uf");
				sb.append(tpl.tpl_option.getHtml());
			}
			return sb.toString(); 
		}
    };
    
    var on = {
        sort: function(pColuna, pReverse)
        {
            jDT.sort(pColuna, pReverse);            
        }
    };
    
    var ctrlRefs = {
        divMainConteudo: function(){return $('mainConteudo');},
        divTituloTopo: function(){return $('divTituloTopo');},
        divResultadoLista: function(){return $('divResultadoLista');},
        divCadastro: function(){return $('divCadastro');},
        divErro: function(){return $('divErro');}
    };
    
    var callbacks = {
        excluir: function(res)
        {
            if (res.value == 0)
            {
                jsCommon.exec.erroCadastro(ctrlRefs.divErro(), "Não foi possível excluir");
            }
            else
            {
                jsCommon.exec.sucessoCadastro(ctrlRefs.divErro(), "Operação realizada com sucesso.");
            };
        }
    };
    
    var startEnv = {
        getDTestados: function()
        {
            jDT = new jDatatable(Estados.listar().value); 
            on.sort("Nome", false);
        },
        listar: function()
        {
            startEnv.getDTestados();
            ctrlRefs.divResultadoLista().style.display = '';
            $html(ctrlRefs.divResultadoLista(), render.resultadoLista());
            if(mostraResultado == true)
            {
                if (retorno.value == 0)
                {
                    jsCommon.exec.erroCadastro(ctrlRefs.divErro(), "Não foi possível excluir");
                }
                else
                {
                    jsCommon.exec.sucessoCadastro(ctrlRefs.divErro(), "Operação realizada com sucesso.");
                };
                mostraResultado = false;
                ctrlRefs.divResultadoLista().style.display = 'none';
            };
        },
        main: function()
        {
            $html(ctrlRefs.divMainConteudo(), render.main());
            $html(ctrlRefs.divErro(), '');
            startEnv.listar();            
        },
        inserir: function()
        {
            retorno = Estados.inserir(($get("txt_cod") == "" ? 0 : parseInt($get("txt_cod"))), $get("txt_nome"), $get("txt_uf"));
            mostraResultado = true;
        },
        editar: function(pIndice)
        {
            $html(ctrlRefs.divErro(), '');
            ctrlRefs.divResultadoLista().style.display = 'none';
            $html(ctrlRefs.divCadastro(), render.novoCadastro("Editar Estados"));
            
            $set('txt_cod', jDT.getDrValue(pIndice, "CodEstado"));
            $set('txt_nome', jDT.getDrValue(pIndice, "Nome"));
            $set('txt_uf', jDT.getDrValue(pIndice, "Uf"));
        },
        novoCadastro: function()
        {
            $html(ctrlRefs.divErro(), '');
            ctrlRefs.divResultadoLista().style.display = 'none';
            $html(ctrlRefs.divCadastro(), render.novoCadastro("Novo Estado"));
            ctrlRefs.divCadastro().style.display = "";
        },
        excluir: function(pCod, pUf)
        {
            jDT = new jDatatable(Cidades.listarUF(pUf).value);
            if(parseInt(jDT.getTotRegs()) == 0)
            {
                Estados.excluir(pCod, callbacks.excluir);
            }
            else
            {
                jsCommon.exec.erroCadastro(ctrlRefs.divErro(), "Não foi possível excluir");
            };
        },
        getEstados: function()
        {
            return Estados.listar();
        }
    };
    
    this.exec = {
        main: function()
        {
            startEnv.main();
        },
        inserir: function()
        {
            startEnv.inserir();
            startEnv.main();
        },
        editar: function(pIndice)
        {
            ctrlRefs.divCadastro().style.display = "";
            startEnv.editar(pIndice);
        },
        excluir: function(pCod, pUf)
        {
            if (confirm("Você deseja realmente excluir?"))
            {
                startEnv.excluir(pCod, pUf);
                startEnv.listar();
                ctrlRefs.divResultadoLista().style.display = 'none';
            };
        },
        novoCadastro: function()
        {
            startEnv.novoCadastro();
        },
        getEstados: function()
        {
            return startEnv.getEstados();
        },
        getOptionEstados: function()
        {
            if (jDT != undefined)
                return render.optionEstados(jDT);
            else
            {
                startEnv.getDTestados();
                return render.optionEstados(jDT);
            };
        }
    };
    
    this.constructor = new function()
    {
        tpl = new Template("templates");
    };
};