﻿Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'qtip';

    var stoAssunto = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({ url: '/DataMaster.aspx' }),
        reader: new Ext.data.JsonReader({}, ['id_tp_assunto', 'assunto']),
        baseParams: {
            listarTpAssunto: true
        },
        remoteSort: true
    });
    stoAssunto.load();

    var FormContato = new Ext.form.FormPanel({
        labelWidth: 100,
        url: 'Contato.aspx',
        width: 490,
        border: false,
        frame: false,
        buttonAlign: 'right',
        defaultType: 'textfield',
        renderTo: 'ContIntForm',
        bodyStyle: "background-color: transparent;",
        items: [
            new Ext.form.ComboBox({
                id: 'cmbAssunto',
                store: stoAssunto,
                fieldLabel: '* Assunto',
                allowBlank: false,
                displayField: 'assunto',
                valueField: 'id_tp_assunto',
                typeAhead: true,
                mode: 'local',
                width: 370,
                listWidth: 370,
                forceSelection: true,
                selectOnFocus: true,
                lazyRender: true,
                emptyText: 'Selecione um assunto',
                tabIndex: 1
            }),
            {
                fieldLabel: '* Nome',
                id: 'Nome',
                allowBlank: false,
                width: 370,
                tabIndex: 2
            },
            {
                fieldLabel: '* Email',
                id: 'Email',
                allowBlank: false,
                width: 370,
                tabIndex: 3
            },
            {
                fieldLabel: '* Cidade',
                id: 'Cidade',
                allowBlank: false,
                width: 370,
                tabIndex: 4
            },
            {
                fieldLabel: '  Telefone',
                id: 'Telefone',
                width: 165,
                tabIndex: 5
            },
            {
                fieldLabel: '   Celular',
                id: 'Celular',
                width: 165,
                tabIndex: 6
            },
            new Ext.form.TextArea({
                fieldLabel: '* Mensagem',
                id: 'Mensagem',
                allowBlank: false,
                width: 370,
                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: 'Contato.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();
                            }
                        }
                    });
                }
            })
        ]
    });
});
