﻿Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'qtip';

    var stoAssunto = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({ url: 'GetData.aspx' }),
        reader: new Ext.data.JsonReader({}, ['id_tp_assunto', 'assunto']),
        baseParams: {
            listarTpAssunto: true
        },
        remoteSort: true
    });
    stoAssunto.load();

    var FormContato = new Ext.form.FormPanel({
        labelWidth: 90,
        width: 530,
        border: false,
        frame: false,
        buttonAlign: 'center',
        defaultType: 'textfield',
        renderTo: 'ContIntForm',
        bodyStyle: "background-color: transparent;",
        items: [
            new Ext.form.ComboBox({
            id: 'cmbAssunto',
            store: stoAssunto,
            fieldLabel: '* Assunto',
            displayField: 'assunto',
            valueField: 'id_tp_assunto',
            typeAhead: true,
            mode: 'local',
            width: 420,
            listWidth: 420,
            forceSelection: true,
            selectOnFocus: true,
            lazyRender: true,
            emptyText: 'Selecione um assunto',
            tabIndex: 1
        }),
        {
            fieldLabel: '* Nome',
            id: 'txtNome',
            allowBlank: false,
            width: 420,
            tabIndex: 2
        },
        {
            fieldLabel: '* Email',
            id: 'txtEmail',
            allowBlank: false,
            width: 420,
            tabIndex: 3
        },
        {
            fieldLabel: '* Cidade',
            id: 'txtCidade',
            allowBlank: false,
            width: 420,
            tabIndex: 4
        },
        {
            fieldLabel: '  Telefone',
            id: 'txtTelefone',
            width: 210,
            tabIndex: 5
        },
        {
            fieldLabel: '   Celular',
            id: 'txtCelular',
            width: 210,
            tabIndex: 6
        },
            new Ext.form.TextArea({
            fieldLabel: '* Mensagem',
            id: 'txaMensagem',
            allowBlank: false,
            width: 420,
            height: 200,
            tabIndex: 7
        })
        ],
        buttons: [
            new Ext.Button({
            id: 'limparContato',
            text: 'Limpar',
            tooltip: 'Limpar',
            tabIndex: 25,
            handler: function() {
                FormContato.getForm().reset();
            }
        }),
            new Ext.Button({
            id: 'enviarContato',
            text: 'Enviar',
            tooltip: 'Enviar',
            tabIndex: 22,
            handler: function() {
                FormContato.getForm().submit({
                    url: 'Fale_Conosco.aspx',
                    params: { envia_email: true },
                    method: 'POST',
                    waitTitle: 'Aguarde..',
                    waitMsg: 'Enviando informações...',
                    success: function() {
                        Ext.Msg.alert('Status', 'Mensagem Enviada com sucesso!', function(btn, text) {
                            if (btn == 'ok') {
                                FormContato.getForm().reset();
                            }
                        });
                    },
                    failure: function(form, action) {
                        if (action.failureType == 'server') {
                            obj = Ext.util.JSON.decode(action.response.responseText);
                            Ext.Msg.alert('Não foi posssivel enviar o e-mail!', obj.errors.reason);
                            FormContato.getForm().reset();
                        }
                    }
                });
            }
        })
        ]
    });
});
