﻿Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    
    var btnCancelar = {
        scope  : this,
        text   : 'Voltar',
        handler: function() { document.location=urlBase }
    };
    
    var btnOk = {
        scope  : this,    
        text   : 'Confirmar',    
        handler: function() { gravarDados(); }
    }
    Ext.applyIf(btnOk, ComproCard.Buttons.Ok);    
    Ext.applyIf(btnCancelar, ComproCard.Buttons.Cancelar);      
    
    var txtCPF      = new Ext.ux.colfield( form, 'cpf', 'CPF', 'txtCPF', 120, { allowBlank: false } );
    var txtDataNasc = new Ext.ux.colfield( form, 'd', 'Nascimento', 'txtDataNasc', 100, { allowBlank: false } );
    var txtCEP      = new Ext.ux.colfield( form, 'cep', 'CEP', 'txtCEP', 120, { allowBlank: false } );
    var txtTelefone = new Ext.ux.colfield( form, 'tel', 'Telefone', 'txtTelefone', 120, { allowBlank: false } );
    var txtEmail    = new Ext.ux.colfield( form, 'e', 'e-mail', 'txtEmail', 300, { allowBlank: false } );
    
    var form = new Ext.FormPanel({
        title: 'Associar-se ao C.A.T.I.',
        frame: true,
        width: 520,
        autoHeight: true,
        id: 'frm-novoassociado',
        url: 'PropostasController.ashx?action=add', 
        method: 'post',
        labelWidth: 80,
        defaultType: 'textfield',
        cls: 'x-panel-mc vc-panel-mc',
        items: [
              {
                xtype: 'fieldset',                
                title: 'Dados pessoais',
                defaultType: 'textfield',
                autoHeight: true,
                items: [
                          txtCPF,
                          {
                            fieldLabel: 'Nome',
                            name: 'txtNome',
                            id: 'txtNome',
                            maxLength: 25,
                            width:300,
                            allowBlank: false
                          },
                          txtDataNasc]
              },
              {
                xtype: 'fieldset',                
                title: 'Contato',
                defaultType: 'textfield',
                autoHeight: true,
                items: [
                          {
                            fieldLabel: 'Endereço',
                            name: 'txtEndereco',
                            id: 'txtEndereco',
                            maxLength: 70,
                            width:300,
                            allowBlank: false
                          },
                          {
                            fieldLabel: 'Complemento',
                            name: 'txtComplemento',
                            id: 'txtComplemento',
                            maxLength: 30,
                            width:150,
                            allowBlank: true
                          },
                          {
                            fieldLabel: 'Bairro',
                            name: 'txtBairro',
                            id: 'txtBairro',
                            maxLength: 40,
                            width:200,
                            allowBlank: false
                          },
                          {
                            fieldLabel: 'Cidade',
                            name: 'txtCidade',
                            id: 'txtCidade',
                            maxLength: 40,
                            width:200,
                            allowBlank: false
                          },
                          {
                            fieldLabel: 'Estado',
                            name: 'txtUF',
                            id: 'txtUF',
                            maxLength: 2,
                            width:25,
                            allowBlank: false
                          },
                          txtCEP,
                          txtTelefone,
                          txtEmail
                       ]
              }],
              buttons: [btnOk, btnCancelar]
    });
    
    resetaForm = function() {
      form.getForm().reset(); 
      Ext.getCmp('txtCPF').focus();
    }
             
    atualizaCampos = function(dados) {
      /*
        Ext.getCmp('txtNumCartao').setValue(dados.NumeroCartao);
        Ext.getCmp('txtNome').setValue(dados.Nome);
        Ext.getCmp('txtConvenio').setValue(dados.NomeEmpresa);
        Ext.getCmp('txtProduto').setValue(dados.NomeProduto);
        Ext.getCmp('txtMatricula').setValue(dados.Matricula);
        Ext.getCmp('txtSalario').setValue(dados.Salario);
        Ext.getCmp('txtLimite').setValue(dados.Limite);
        Ext.getCmp('txtSaldo').setValue(dados.Saldo);
        Ext.getCmp('txtNovoLimite').setValue(dados.Limite);
        Ext.getCmp('txtNovoLimite').focus();
      */
    }

    emiteBoleto = function(dados) {
       window.open('boleto.aspx?cpf=' + txtCPF.getValue() + '&fid=' + dados.FaturaID);
    }
    
    gravarDados = function() {
        var form = Ext.getCmp('frm-novoassociado').getForm();        
        if (!form.isValid())
	        Ext.Msg.show({ title    : 'Erro',
                           msg      : 'Favor corrigir o(s) erro(s) encontrado(s).',
                           buttons  : Ext.Msg.OK
                         });
	    else 
	    {
	      form.submit({ waitMsg: 'Aguarde. Gravando alterações....', params: { acao: 'add' },
	                   url: 'PropostasController.ashx',
	                   failure: function(frm, action) {
                                   Ext.MessageBox.alert('Erro', action.result.errorInfo);
                                },
    	                       
	                   success: function(frm, action) {
	                               if (!action.result)
   	                                 Ext.MessageBox.alert('ERRO', 'Erro na comunicação com o servidor.');
   	                               else
   	                               {
	                                 Ext.MessageBox.alert('Confirmação', action.result.info, function() { emiteBoleto(action.result) } );	                            
	                               }
	                            }
	                  });
	    }
	}

    ComproCard.Layout.init();
    var mainPanel = Ext.getCmp('pnl-main');
    mainPanel.add(form);
    mainPanel.doLayout();
    ComproCard.Layout.removeLoading();
  	//Ext.get("frm-novoassociado").center();
});

