Ext.define("SPI.imlppo.view.Main", {

	extend: "Ext.panel.Panel",

	layout: "fit",

	config: {
		grid: null
	},

	requires: [
		"SPI.imlppo.controller.Main"
	],

	controller: "rest.controller.main",

	initComponent: function() {
	
		ip2.getUser().set("ACCESS", true);

		var i_self = this;

		i_self.callParent(arguments);

		i_self.add(i_self.createGrid());
	},

	createGrid: function() {

		var i_self = this;

		var i_created = Ext.widget("ip2pagingGridOld", {
			autoLoad: true,
			searchCombo: false,
			border: false,
			dmd: {
				path: "dmd/ctu-spi.dmd",
				entityName: "Customer"
			},
			uiName: "portfolio",
			flex: 1,
			listeners: {afterrender: "onAfterRender"}
		});

		var i_store = i_created.getStore();
		var i_model = i_store.getModel();
		var i_columnManager = i_created.getColumnManager();

		/*var a_fields = i_model.getFields();
		var a_columns = i_columnManager.getColumns();
		
		var a_newColumns = [];
		var a_newFields = [];

		Ext.Array.each(a_columns, function(i_column) {
			var config = i_column.initialConfig;
			a_newColumns.push(Ext.apply({}, config));
		});

		Ext.Array.each(a_fields, function(i_field) {
			a_newFields.push(i_field);
		});*/
		
		i_store.setProxy(Ext.create('Ext.data.proxy.Rest', {
			type: "rest",
			url: "api/imlquantity",
			reader: {type: 'json', rootProperty: '_embedded.imlquantity', totalProperty: 'page.totalElements'},
			pageParam: "page",
			startParam: "start",
			limitParam: "size",
			actionMethods : {
			   update: "PATCH"
			}
			}
		));
		
		i_store.events.beforeload.clearListeners();
		
		i_store.on({
			beforeLoad: {
				fn: function(i_store, i_operation) {
					var i_proxy = i_store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();
					
					i_proxy.setUrl("api/imlquantity");
					
					if (o_extraParams.hasOwnProperty("search")) {
						i_proxy.setUrl("api/imlquantity/search/byName");
					}
					Ext.apply(o_extraParams, {page: i_operation.page-1})
				}
			}
		});
		
		/*var model = ip2.model({dmd: 'dmd/ctu-spi.dmd', entity: 'IMLQuantity', view: 'portfolio', context: 'grid'});
		model.setProxy(Ext.create('Ext.data.proxy.Rest', {
			type: "rest",
			url: "api/imlquantity",
			reader: {type: 'json', rootProperty: '_embedded.imlquantity', totalProperty: 'page.totalElements'},
			pageParam: "page",
			startParam: "start",
			limitParam: "size",
			actionMethods : {
			   update: "PATCH"
			}
			}
		));*/
		
		
		
		
		/*var i_userModel = Ext.define('SPI.User', {
		    extend: 'Ext.data.Model',
		    fields: [
				{name: 'id', type: 'string'},
				{name: 'pwd', type: 'string'},
				{name: 'name', type: 'string'},
				{name: 'status', type: 'string'},
				{name: 'lang', type: 'string'},
			],
			proxy: {
				type: "rest",
				url: "api/user",
				reader: {type: 'json', rootProperty: '_embedded.user', totalProperty: 'page.totalElements'},
				pageParam: "page",
				startParam: "start",
				limitParam: "size"
			},
		});		
		
		var a_columns = [
			{dataIndex: 'id', header: 'Id', width: 100},
			{dataIndex: 'pwd', header: 'Pwd', width: 100, renderer: function(value) {return '****'} },
			{dataIndex: 'name', header: 'Name', flex: 1, editor: 'textfield'},
			{dataIndex: 'status', header: 'Status', width: 100},
			{dataIndex: 'lang', header: 'Language', width: 100},
		];*/
		
		/*var i_restStore = Ext.create("Ext.data.Store", {
			autoLoad: true,
			pageSize: 5,
			model: model,
			listeners: {beforeLoad: {fn: function(i_store, i_operation) {
				var i_proxy = i_store.getProxy();
				var o_extraParams = i_proxy.getExtraParams();
				Ext.apply(o_extraParams, {page: i_operation.getPage()-1})
			}}}
		});

		var i_button_add = Ext.widget("themedbutton", {
			iconCls: "add",
			itemId: "button-add",
			handler: function() {
				var i_model = i_restStore.getModel();
				var i_new = i_model.create({});
				//i_new.save();
				i_restStore.add(i_new);
			}
		});*/

		/*var i_toolbar = Ext.widget("pagingtoolbar", {
			dock: "top",
			store: i_restStore,
			displayInfo: true
		});
		i_toolbar.insert(11, ["|", i_button_add]);

		var i_grid = Ext.widget("gridpanel", {
			title: "IML quantities",
			columns: a_newColumns,
			border: false,
			store: i_restStore,
			plugins: {
		        ptype: 'rowediting',
		        clicksToEdit: 1
		    },
		    listeners: {edit: {fn: function(editor, e) {e.record.save()}}}
		});

		i_grid.addDocked(i_toolbar);*/

		i_self.setGrid(i_created);

		return i_created;
	}
});
