/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */


Ext.onReady(function(){
Ext.QuickTips.init();


 

	
//inizio contenuti in formato tabs

	var xg = Ext.grid;
	var xg5 = Ext.grid;



//json Famiglia
		oplUsato = new Ext.data.Store({
										proxy: new Ext.data.HttpProxy({url: 'http://www.incidere.com/ControlPanel/richieste_ext/usato.php?task=HOMEPAGE&lib=yes&limit=10', method: 'POST' }),
										//reader: new Ext.data.JsonReader({}, ['us_famiglia', 'us_id', 'us_marca', 'us_modello', 'us_datains', 'us_costo', 'us_status', 'us_sfam']),
										
										reader: new Ext.data.JsonReader({fields: [
																						{name:'us_famiglia'},
																						{name:'us_id'},
																						{name:'us_marca'},
																						{name:'us_modello'},
																						{name:'us_costo'},
																						{name:'us_status'},
																						{name:'us_sfam'},
																						{name:'us_datains', type: 'date', dateFormat: 'Y-m-d'}
																					]}),
										remoteSort: false
		});
		
		
oplUsato.load();



//json Famiglia
		oplUsatoc = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: 'http://www.incidere.com/ControlPanel/richieste_ext/usatoc.php?task=HOMEPAGE&lib=yes&limit=10', method: 'POST' }),
		reader: new Ext.data.JsonReader({ fields: [
													 {name:'cerco_fam'},
													 {name:'cerco_id'}, 
													 {name:'cerco_marca'}, 
													 {name:'cerco_modello'}, 
													 {name:'cerco_datains', type: 'date', dateFormat: 'Y-m-d'}, 
													 {name:'cerco_cos2'}, 
													 {name:'cerco_status'}
													 ]}),
		remoteSort: false
	});
		
		
oplUsatoc.load();



//apro la griglia
var sm2 = new xg.CheckboxSelectionModel();
var sm5 = new xg.CheckboxSelectionModel()


  var tabs = new Ext.TabPanel({
        renderTo: 'dx_a',
        width:740,
		
        activeTab: 0,
		deferredRender: false, 
		plain: true,
        frame:true,
        defaults:{autoHeight: true},
        items:[
            { 	title: 'Vendo Usato',
				items: 

															 grid4 = new xg.GridPanel({
																	//loadMask: {msg:"Caricamento dati..."},
																	loadMask: true,
																	id:'grid4',
																	autoHeight: true,
																	maxHeight:260,
																	width:740,
																	
																	frame:false,
																	//title:'Elenco inserzioni Vendo Usato',
																	//iconCls: 'icon-grid',
																	//renderTo: 'table',
																	stripeRows: true,
																	store: oplUsato,
																	listeners:{
																				cellclick: doppioClick
																				},
															
																	/*store: new Ext.data.Store({
																								reader: reader,
																								data: xg.dummyData
																								}),*/
															
															
																	cm: new xg.ColumnModel([
																								{id:'us_id',header: "Tipologia", sortable: true, dataIndex: 'us_famiglia', width:130},
																								{header: "",  sortable: true, dataIndex: 'us_sfam', width:150},
																								{header: "Marca",  sortable: true, dataIndex: 'us_marca', width:130},
																								{header: "Modello",  sortable: true, dataIndex: 'us_modello', width:100},
																								{header: "Prezzo in €",  sortable: true, dataIndex: 'us_costo', width:70, align: 'right'},
																								{header: "Data Inserimento",  sortable: true, dataIndex: 'us_datains', width:90, renderer: Ext.util.Format.dateRenderer('d-M-Y'), align: 'right'}
																							]),
																	sm: sm2, 
																	viewConfig: {
																					forceFit:true
																				}
															
															
																})







			},
			{ 	title: 'Cerco Usato',
				items: 

															 grid5 = new xg5.GridPanel({
																	//loadMask: {msg:"Caricamento dati..."},
																	loadMask: true,
																	id:'grid5',
																	autoHeight: true,
																	maxHeight:260,
																	width:740,
																	frame:false,
																	//title:'Elenco inserzioni Vendo Usato',
																	//iconCls: 'icon-grid',
																	//renderTo: 'table',
																	stripeRows: true,
																	store: oplUsatoc,
																	listeners:{
																				cellclick: doppioClick5
																				},
															

															
																	cm: new xg5.ColumnModel([
																								
																								{id:'cerco_id',header: "Tipologia", sortable: true, dataIndex: 'cerco_fam', width:200},
																								{header: "Marca",  sortable: true, dataIndex: 'cerco_marca', width:150},
																								{header: "Modello",  sortable: true, dataIndex: 'cerco_modello', width:150},
																								{header: "Prezzo in €",  sortable: true, dataIndex: 'cerco_cos2', width:120, align: 'right'},
																								{header: "Data Inserimento",  sortable: true, dataIndex: 'cerco_datains', width:110, renderer: Ext.util.Format.dateRenderer('d-M-Y'), align: 'right'}
																							]),
																	sm: sm5,
															
																	viewConfig: {
																					forceFit:true
																				}
															
															
																})







			}
			
			
			
			
			
			
			
			
			
			]
});






function doppioClick(){
	 if(grid4.selModel.getCount() == 1) // only one president is selected here
    {
      modificaUsato();
    } else if(grid4.selModel.getCount() > 1){
            Ext.Msg.show({title: 'Attenzione',
																																 msg: 'E\' possibile modificare un usato alla volta.',
																																 
																																 icon: Ext.MessageBox.WARNING,
																					 												buttons: Ext.Msg.OK

																																 
																																})
    } else {
      Ext.Msg.show({title: 'Attenzione',
																																 msg: 'Per modifica un usato devi prima selezionarne uno dalla lista.',
																																 
																																 icon: Ext.MessageBox.WARNING,
																					 												buttons: Ext.Msg.OK

																																 
																																})
	  //Ext.MessageBox.alert('','');
    }
}
	
	
	
 function modificaUsato(){
								
													 var selections = grid4.selModel.getSelections();
													 var prez = [];
													 for(i = 0; i< grid4.selModel.getCount(); i++){
																									  prez.push(selections[i].json.us_id);
																									 };
													 var selectedId = prez;
													 
													 
													var redirect = 'index.php?dest=dettagli_ext&tip=vendo&us_id='+ selectedId; 
													window.location = redirect;
  }
	












// SECONDO TAB SECONDO ELENCO
   // This was added in Tutorial 6
function doppioClick5(){
	 if(grid5.selModel.getCount() == 1) // only one president is selected here
    {
      modificaUsato5();
    } else if(grid5.selModel.getCount() > 1){
            Ext.Msg.show({title: 'Attenzione',
																																 msg: 'E\' possibile modificare un usato alla volta.',
																																 
																																 icon: Ext.MessageBox.WARNING,
																					 												buttons: Ext.Msg.OK

																																 
																																})
    } else {
      Ext.Msg.show({title: 'Attenzione',
																																 msg: 'Per modifica un usato devi prima selezionarne uno dalla lista.',
																																 
																																 icon: Ext.MessageBox.WARNING,
																					 												buttons: Ext.Msg.OK

																																 
																																})
	  //Ext.MessageBox.alert('','');
    }
}





function modificaUsato5(){
								
													 var selections = grid5.selModel.getSelections();
													 var prez = [];
													 for(i = 0; i< grid5.selModel.getCount(); i++){
																									  prez.push(selections[i].json.cerco_id);
																									 };
													 var selectedId = prez;
													 
													 
													var redirect = 'index.php?dest=dettagli_ext&tip=cerco&us_id='+ selectedId; 
													window.location = redirect;
  }
	

//fine contenuti in formato tabs
}); // chiuso onReady
