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

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

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

	controller: "rest.controller.ppofactory",

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

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

		var workflowForm = Ext.widget("form", {
			region: "north",
			bodyPadding: 5,
			/*bodyStyle: 'background-color:#ff8d00',*/
			height: 50,
			layout: {
				type: "hbox",
				align: "center"
			},
			defaults: {
				margin: "0 5 0 0"
			},
			items: []
		});		

		var ppoGrid = Ext.widget("ip2pagingGridOld", {
			autoLoad: false,
			searchCombo: false,
			border: false,
			filterable: false,
			dmd: {
				path: "dmd/spi-ppo.dmd",
				entityName: "PPO"
			},
			uiName: "factory",
			flex: 1,
			listeners: {
				afterrender: "onAfterRender",
				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

						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();

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



		/* var showClosedCheckbox = Ext.widget("checkbox", {
			boxLabel: 'Show closed PreOrders',
			name: 'open',
			padding: '2 10',
			checked: false,
			listeners: {
				change: function(me) {
					ppoGrid.getStore().loadPage(1);
				}
			}
		});
		*/

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

		var btnCreatePK2Order = Ext.widget("button", {
			itemId: "button-createPK2PO",
			text: "Create PK2 Purchase Order",
			margin: '2 5',
			handler: function(me) {
				if (confirm('Create purchase order in Pak2000?')) {

				}
			}

		});
		workflowForm.items.add(btnCreatePK2Order);

		var btnViewPK2Order = Ext.widget("button", {
			itemId: "button-viewPK2PO",
			text: "View PK2 Purchase Order",
			margin: '2 5',
			disabled: true,
			handler: function(me) {
			}

		});
		workflowForm.items.add(btnViewPK2Order);

		var btnClosePPO = Ext.widget("button", {
			itemId: "button-closePPO",
			text: "Close selected",
			margin: '2 5',
			handler: function(me) {
				ppoGrid.getSelection().forEach(r => r.set("status", "Closed"));
				ppoGrid.getStore().save();
			}

		});
		workflowForm.items.add(btnClosePPO);

		var locationPicker = Ext.widget("combosql", {
			displayField: "name",
			valueField: "key",
			fieldLabel: "Select factory",
			editable: false,
			allowBlank: false,
			forceSelection: true,
			url: "api/picker/location",
			totalProperty: "totalRows",
			padding: '2 10',
			width: 300,
			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;

	}
});
