Ext.ns('Pubbli');

Pubbli.OrderGrid = Ext.extend(Ext.grid.EditorGridPanel, {
	initComponent : function() {

		this.orderStore = new Ext.data.Store({
					autoLoad : true,
					url : 'http://www.pubblicommerce.com/action_orders.php?reading=1&listing_id=' + this.oListingId,
					reader : new Ext.data.XmlReader({
								record : 'order',
								id : 'order_reader',
								idProperty : 'id'
							}, ['id', 'buyer_name',
									'shipping', 'product', 'status', 'date_of_order', 'date_confirm'])
		});
		
		
		var config = {
			title : 'Ordini',
			frame : true,
			forceFit : true,
			store : this.orderStore,
			columns : [{
						header : "Order Id",
						width : 60,
						dataIndex : 'id',
						sortable : true
					}, {
						header : "Buyer Name",
						width : 100,
						dataIndex : 'buyer_name',
						sortable : true
					}, {
						header : "Shipping",
						width : 60,
						dataIndex : 'shipping',
						sortable : true
					},  {
						header : "Product",
						width : 60,
						dataIndex : 'product',
						sortable : true						
					},  {
						header : "Status",
						width : 130,
						dataIndex : 'status',
						//sortable : true,
						editor: new Ext.form.ComboBox({
							typeAhead: true,
							triggerAction: 'all',
							store : new Ext.data.ArrayStore({
								fields: [
									'name'
								],
								data: [['Lavorazione'], ['Conferma'], ['Non confermato']]
							}),
							lazyRender:true,
							mode: 'local',
							valueField: 'name',
							displayField: 'name',
							selModel : new Ext.grid.RowSelectionModel(),
							listeners : {
								scope : this,
								select : function(combo){
											Ext.Ajax.request({
												url : 'http://www.pubblicommerce.com/action_orders.php?storing=1&listing_id=' + this.oListingId,
												method : 'POST',		
												params : {
													status : combo.getValue(),
													order_id : this.getSelectionModel().selection.record.get('id')
												},
												success : function(o) {
													var json = Ext.decode(o.responseText);
													
													if(json.success){
															Ext.Msg.alert('Done', 'Saved successfuly');
													}else{
															Ext.Msg.alert('Error Occured : ', json.error);
													}
													
												},
												failure : function(o) {
													Ext.Msg.alert('Error Occured : Connection Failure!');
												},
												scope : this
											});
								}
							}
						})						
					},  {
						header : "Date of Order",
						width : 140,
						dataIndex : 'date_of_order',
						sortable : true
					},  {
						header : "Date of Confirmation",
						width : 140,
						dataIndex : 'date_confirm',
						sortable : true
					}],
			width : 540,
			height : 200
		};

		Ext.apply(this, Ext.apply(this.initialConfig,
						config));
						
		Pubbli.OrderGrid.superclass.initComponent.apply(
				this, arguments);
	}
});

