﻿Ext.namespace('Vt.Common');
Ext.namespace('Vt.Servizi');

Vt.Common.storeComuni = new Ext.data.JsonStore({
    xtype: 'jsonstore',
    //autoLoad: true,
    url: '/Info/GetComuni',
    root: 'items',
    idProperty: 'ID',
    fields: ['ID', 'Nome', 'ProvinciaID']
});
Vt.Common.storeComuni.load();

Vt.Common.storeComuni2 = new Ext.data.JsonStore({
    xtype: 'jsonstore',
    //autoLoad: true,
    url: '/Info/GetComuni',
    root: 'items',
    idProperty: 'ID',
    fields: ['ID', 'Nome', 'ProvinciaID']
});
Vt.Common.storeComuni2.load();

Vt.Servizi.datiImpresa = {
    layout: 'form',
    title: 'Dati Impresa',
    bodyStyle: 'padding:15px',
    //		width: 400,
    defaultType: 'textfield',
    defaults: {
        // applied to each contained item
        //			width: 230,
        grow: true,
        msgTarget: 'side'
    },
    items: [{
        fieldLabel: 'Denominazione (Indicare l’insegna dell’esercizio)',
        allowBlank: false,
        growMin: 300,
        name: 'denominazione'
    }, {
        xtype: 'radiogroup',
        fieldLabel: 'Tipologia',
        itemCls: 'x-check-group-alt',
        columns: 1,
        items: [{
            xtype: 'radio',
            checked: true,
            boxLabel: 'Ristorante',
            name: 'Tipologia',
            inputValue: 'Ristorante'
        }, {
            xtype: 'radio',
            labelSeparator: '',
            boxLabel: 'Bottega',
            name: 'tipologia',
            inputValue: 'Bottega'
        }]
    }, {
        fieldLabel: 'Indirizzo',
        allowBlank: false,
        name: 'indirizzo'
    }, {
        fieldLabel: 'Località',
        name: 'localita'
    }, {
        fieldLabel: 'CAP',
        allowBlank: false,
        name: 'cap'
    }, {
        xtype: 'combo',
        id: 'field-provincia',
        fieldLabel: 'Provincia',
        allowBlank: false,
        hiddenName: 'provincia',
        store: new Ext.data.SimpleStore({
            fields: ['provinciaID', 'provinciaNome', 'provinciaIcon'],
            data: [['AR', 'Arezzo', 'ux-provincia-icon-ar'], ['FI', 'Firenze', 'ux-provincia-icon-fi'], ['GR', 'Grosseto', 'ux-provincia-icon-gr'], ['LI', 'Livorno', 'ux-provincia-icon-li'], ['LU', 'Lucca', 'ux-provincia-icon-lu'], ['MS', 'Massa e Carrara', 'ux-provincia-icon-ms'], ['PI', 'Pisa', 'ux-provincia-icon-pi'], ['PT', 'Pistoia', 'ux-provincia-icon-pt'], ['PO', 'Prato', 'ux-provincia-icon-po'], ['SI', 'Siena', 'ux-provincia-icon-si']]
        }),
        valueField: 'provinciaID',
        displayField: 'provinciaNome',
        iconClsField: 'provinciaIcon',
        triggerAction: 'all',
        forceSelection: true,
        mode: 'local',
        listeners: {
            change: {
                fn: function (combo, value) {
                    //					alert('BIP');
                    var val = combo.getValue();
                    if (val) {
                        //						alert(val);
                        var comboComune = Ext.getCmp('field-comune');
                        comboComune.clearValue();
                        comboComune.store.filter('ProvinciaID', val);
                    }
                }
            }
        }
    }, {
        xtype: 'combo',
        id: 'field-comune',
        fieldLabel: 'Comune',
        allowBlank: false,
        //		name: 'comune',
        typeAhead: true,
        mode: 'remote',
        selectOnFocus: true,
        forceSelection: true,
        displayField: 'Nome',
        valueField: 'ID',
        hiddenName: 'comuneID',
        loadingText: 'Caricamento...',
        minChars: 1,
        triggerAction: 'all',
        lastQuery: '',
        store: Vt.Common.storeComuni
    }, {
        fieldLabel: 'Telefono',
        allowBlank: false,
        name: 'telefono'
    }, {
        fieldLabel: 'Fax',
        name: 'fax'
    }, {
        fieldLabel: 'E-mail',
        allowBlank: false,
        name: 'email'
    }]
};

Vt.Servizi.titolareImpresa = {
    layout: 'form',
    title: 'Dati Titolare',
    bodyStyle: 'padding:15px',
    //		width: 400,
    defaultType: 'textfield',
    defaults: {
        // applied to each contained item
        //			width: 230,
        msgTarget: 'side'
    },
    items: [{
        fieldLabel: 'Cognome',
        allowBlank: false,
        name: 'cognome'
    }, {
        fieldLabel: 'Nome',
        allowBlank: false,
        name: 'nome'
    }, {
        fieldLabel: 'Ragione Sociale',
        name: 'ragioneSociale'
    }, {
        fieldLabel: 'Indirizzo',
        name: 'indirizzoTitolare'
    }, {
        fieldLabel: 'Località',
        name: 'localitaTitolare'
    }, {
        fieldLabel: 'CAP',
        name: 'capTitolare'
    }, {
        xtype: 'combo',
        id: 'titolare--provincia',
        fieldLabel: 'Provincia',
        hiddenName: 'provinciaTitolare',
        store: new Ext.data.SimpleStore({
            fields: ['provinciaID', 'provinciaNome', 'provinciaIcon'],
            data: [['AR', 'Arezzo', 'ux-provincia-icon-ar'], ['FI', 'Firenze', 'ux-provincia-icon-fi'], ['GR', 'Grosseto', 'ux-provincia-icon-gr'], ['LI', 'Livorno', 'ux-provincia-icon-li'], ['LU', 'Lucca', 'ux-provincia-icon-lu'], ['MS', 'Massa e Carrara', 'ux-provincia-icon-ms'], ['PI', 'Pisa', 'ux-provincia-icon-pi'], ['PT', 'Pistoia', 'ux-provincia-icon-pt'], ['PO', 'Prato', 'ux-provincia-icon-po'], ['SI', 'Siena', 'ux-provincia-icon-si']]
        }),
        valueField: 'provinciaID',
        displayField: 'provinciaNome',
        iconClsField: 'provinciaIcon',
        triggerAction: 'all',
        forceSelection: true,
        mode: 'local',
        listeners: {
            change: {
                fn: function (combo, value) {
                    var val = combo.getValue();
                    if (val) {
                        var comboComune = Ext.getCmp('titolare-comune');
                        comboComune.clearValue();
                        comboComune.store.filter('ProvinciaID', val);
                    }
                }
            }
        }
    }, {
        xtype: 'combo',
        id: 'titolare-comune',
        fieldLabel: 'Comune',
        name: 'comuneTitolare1',
        typeAhead: true,
        mode: 'remote',
        selectOnFocus: true,
        allowBlank: true,
        forceSelection: true,
        displayField: 'Nome',
        valueField: 'Nome',
        hiddenName: 'comuneTitolare',
        loadingText: 'Caricamento...',
        minChars: 1,
        triggerAction: 'all',
        lastQuery: '',
        store: Vt.Common.storeComuni2
    }, {
        fieldLabel: 'Telefono',
        name: 'telefonoTitolare'
    }, {
        fieldLabel: 'Fax',
        name: 'faxTitolare'
    }]
};

Vt.Servizi.altro = {
    layout: 'form',
    title: 'Altro',
    bodyStyle: 'padding:15px',
    //		width: 400,
    defaultType: 'textfield',
    defaults: {
        // applied to each contained item
        //			width: 230,
        msgTarget: 'side'
    },
    items: [{
        fieldLabel: '?????'
    }, {
        fieldLabel: '?????'
    }, {
        fieldLabel: '?????'
    }, {
        fieldLabel: '?????'
    }]
};


Ext.onReady(function () {
    Vt.Servizi.dlg = new Ext.FormPanel({
        title: 'Richiesta di adesione',
        renderTo: 'richiesta-adesione',
        url: '/Servizi/Registra',
        width: 600,
        labelWidth: 200,
        monitorValid: true,
        labelSeparator: ':',
        items: [{
            html: '<div style="text-align:center;"><b>RICHIESTA DI ADESIONE</b></div><div>ti ricordiamo che la mail qui indicata sarà utilizzata sia per le comunicazioni interne alla rete che per essere visibile al pubblico, salvo tua diversa indicazione a: evapratesi@gmail.com</div>'
        }, {
            xtype: 'tabpanel',
            bodyStyle: 'padding:15px',
            activeTab: 0,
            defaults: { autoHeight: true, bodyStyle: 'padding:10px' },
            items: [Vt.Servizi.datiImpresa, Vt.Servizi.titolareImpresa/*, Vt.Servizi.altro*/]
        }],
        buttons: [{
            text: 'INVIA',
            scope: this,
            handler: function () {
                if (!Vt.Servizi.dlg.getForm().isValid()) {
                    Ext.MessageBox.alert('Message', 'Dati incompleti');
                    return;
                }
                Vt.Servizi.dlg.getForm().submit({

                    waitMsg: 'Saving...',       // Wait Message  

                    success: function (form, action) {      // When saving data success  

                        Ext.MessageBox.alert('Dati salvati', action.result.msg);

                        // clear the form  

                        Vt.Servizi.dlg.hide()
                        //simpleForm.getForm().reset();  

                    },

                    failure: function (form, action) {      // when saving data failed  
                        switch (action.failureType) {
                            case Ext.form.Action.CLIENT_INVALID: Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values'); break;
                            case Ext.form.Action.CONNECT_FAILURE: Ext.Msg.alert('Failure', 'Ajax communication failed'); break;
                            case Ext.form.Action.SERVER_INVALID: Ext.Msg.alert('Failure', action.result.msg);
                        }
                    }

                });


            }
        }]
    })
});

