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

	extend: "Ext.panel.Panel",
	layout: {
		type: "vbox",
		align: "stretch"
	},
	config: {
		grid: null,
		locationPicker: null
	},

	requires: [
		"SPI.imlppo.controller.ppo",
		"SPI.imlppo.view.PpoEditor"
	],

	controller: "rest.controller.ppo",

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

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

		var workflowForm = Ext.widget("form", {
			region: "north",
			height: 50,
			bodyPadding: 5,
			layout: {
				type: "hbox",
				align: "center"
			},
			defaults: {
				margin: "0 5 0 0"
			},
			items: []
		});		

		var ppoGrid = Ext.widget("ip2pagingGridOld", {
			autoLoad: true,
			searchCombo: false,
			border: false,
			dmd: {
				path: "dmd/spi-ppo.dmd",
				entityName: "PPO"
			},
			uiName: "cs",
			flex: 1,
			listeners: {
				afterRender: function(me) {
					//me.events.itemcontextmenu.clearListeners(); // don't show contextmenu
				},
				beforeUpdate: {
					fn: function(me, config) {
						Ext.widget("imlppo.view.PpoEditor", {
							dataRecord: config
						}).showInWindow();
						return false;
					}
				},
				beforeCreate: {
					fn: function(me, config) {
						// put default data into config before creating new entity
						// If return false then abort operation
						// Can be used to show custom form instead of the standard form from portfolio
						Ext.widget("imlppo.view.PpoEditor", {
							dataRecord: {
								cust_location: i_self.getLocationPicker().getValue(),
								status: "Draft",
								ordertype: "I",
								createdby: ip2.getUser().get("USRUSRTKN"),
								keys: {id: 0}
							}
						}).showInWindow();
						return false;
					}
				},
				
			}
		});

		ppoGrid.getFilterBar().hide();
		var toolbar = ppoGrid.getEntityToolBar();
		toolbar.removeExport();

		ppoGrid.getStore().on({
			beforeLoad: {
				fn: function(store, operation) {
					var params = store.getProxy().getExtraParams();
					var status = pporadio.getValue();
					var location = i_self.getLocationPicker().getValue();
					Ext.apply(params, {
						location: location,
						status: status
					});
				}
			}
		});

		var pporadio = Ext.widget("radiogroup", {
			fieldLabel: "Show status",
			labelWidth: 75,
			vertical: true,
			flex: 1,
			maxWidth: 400,
			margin: "2 5",
			items: [
				{boxLabel: 'Draft', name: 'status', inputValue: 'Draft', checked: true},
				{boxLabel: 'Ready', name: 'status', inputValue: 'Ready'},
				{boxLabel: 'Order', name: 'status', inputValue: 'Order'},
				{boxLabel: 'Closed', name: 'status', inputValue: 'Closed'},
				{boxLabel: 'All', name: 'status', inputValue: '%'}
			],
			listeners: {
				change: function(me) {
					ppoGrid.getStore().loadPage(1);
					btnMarkReady.setDisabled(me.getValue().status!="Draft");
				}
			}
		});
		workflowForm.items.add(pporadio);

		var btnMarkReady = Ext.widget("button", {
			itemId: "button-markReady",
			text: "Mark selected Ready for prod",
			margin: '2 5',
			handler: function(me) {
				ppoGrid.getSelection().forEach(r => r.set("status", "Ready"));
				ppoGrid.getStore().save();
				//ppoGrid.view.refresh();
			}
		});
		workflowForm.items.add(btnMarkReady);

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

		workflowForm.items.add(Ext.widget("tbfill"));
		workflowForm.items.add(locationPicker);	
		this.setLocationPicker(locationPicker);

		items.push(workflowForm);
		items.push(ppoGrid);

		return items;
	}
});
