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

	extend: "Ext.panel.Panel",
	layout: {
		type: "vbox",
		align: "stretch"
	},

	config: {
		locationPicker: null,
		quantityGrid: null,
		customerGrid: null
	},

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

	controller: "rest.controller.main",

	initComponent: function() {
		ip2.getUser().set("ACCESS", true);
		this.callParent(arguments);
		this.add(this.createLayout());
		ip2.getUser().set("LOCATION", 'R');
	},

	createLayout: function() {
		var items = [];
		var i_self = this;

		var quantityGrid = Ext.widget("ip2pagingGridOld", {
			autoLoad: false,
			allowSearch: false,
			border: false,
			split: true,
			filterable: false,
			dmd: {
				path: "dmd/spi-ppo.dmd",
				entityName: "IMLQuantity"
			},
			uiName: "list",
			flex: 1,
			listeners: {}
		});
		quantityGrid.getEntityToolBar().removeExport();
		quantityGrid.getStore().on({
			beforeLoad: {
				fn: function(store, operation) {
					store.getProxy().setUrl("/api/IMLQuantity/byCustomer");
				}
			}
		});

		i_self.setQuantityGrid(quantityGrid);

		var customerGrid = Ext.widget("ip2pagingGridOld", {
			autoLoad: false,
			searchCombo: false,
			border: false,
			filterable: false,
			split: true,
			dmd: {
				path: "dmd/spi-pk2.dmd",
				entityName: "scustopf"
			},
			uiName: "search",
			flex: 1,
			listeners: {
				select: function(me, selection) {
						var grid = i_self.getQuantityGrid();
						var params = grid.getStore().getProxy().getExtraParams();
						Ext.apply(params, {cust_id: selection.data.cucusi});
						grid.getStore().loadPage(1);
				}
			}
		});
		customerGrid.getEntityToolBar().removeExport();
		customerGrid.getEntityToolBar().removeAdd();
		//TODO: PNO - Hvorfor sættes store.proxy.url ikke kun ved init?
		//TODO: PNO - Hvorfor står der ikke search i entityToolbat search felt?
		//TODO: PNO - Sort kol skal være mapping og ikke name
		//TODO: tilføj success: false ved server fejl

		//TODO: api/customer mangler location binding
		//TODO: Nyt element skal fødes med nøgle værdier (location, cust_id)
		customerGrid.getStore().on({
			beforeLoad: {
				fn: function(store, operation) {
					store.getProxy().setUrl("/api/customer");
					var params = store.getProxy().getExtraParams();
					var l = i_self.getLocationPicker();
					var model = l.getSelection();
					var active = onlyActiveCustomerCheckbox.getValue();
					Ext.apply(params, {location: model.data.key, onlyActive: active});
				}
			}
		});

		var locationPicker = Ext.widget("combosql", {
			displayField: "name",
			valueField: "key",
			fieldLabel: "Select location",
			editable: false,
			allowBlank: false,
			forceSelection: true,
			url: "api/picker/location",
			totalProperty: "totalRows",
			padding: '2 15',
			listeners: {
				change: {
					fn: function(me, selection) {
						customerGrid.getStore().loadPage(1);
						me.getTrigger('clear').hide();
					},
					buffer: 10
				},
				afterRender: function(me) {
					me.getStore().load({
						callback() {
							me.select(ip2.getUser().get("LOCATION"))
						}
					});
				}
			}
		});

		customerGrid.getEntityToolBar().items.add(locationPicker);

		var onlyActiveCustomerCheckbox = Ext.widget("checkbox", {
			boxLabel: 'Only active customers',
			name: 'active',
			padding: '2 15',
			checked: true,
			listeners: {
				change: function(me) {
					customerGrid.getStore().loadPage(1);
				}
			}
		});

		customerGrid.getEntityToolBar().items.add(onlyActiveCustomerCheckbox);

		this.setLocationPicker(locationPicker);
		this.setCustomerGrid(customerGrid);

		items.push(customerGrid);
		items.push(quantityGrid);

		return items;
	}

});
