﻿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',
		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',
		name: 'telefono'
	}, {
		fieldLabel: 'Fax',
		name: 'fax'
	}, {
		fieldLabel: 'E-mail',
		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'
	}, {
		fieldLabel: 'E-mail',
		allowBlank: false,
		vtype: 'email',
		name: 'emailTitolare'
	}]
};

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: 90,
		monitorValid: true,
		labelSeparator: ':',
		items: [{
			html: '<div style="text-align:center;"><b>RICHIESTA DI ADESIONE</b></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);
						}
					}

				});


			}
		}]
	})
});
