Ext.define("SPI.imlppo.view.PpoEditor", {
	extend: "Ext.form.Panel",
	alias: "widget.imlppo.view.PpoEditor",

	layout: {
		type: "vbox",
		align: "stretch"
	},

	dataRecord: {
		status: "Draft",
		ordertype: "I",
		keys: {id: null}
	},

	config: {
		contentForm: null
	},

	listeners: {
		dirtychange: {
			fn: function(i_form, b_dirty) {
				//var i_entityPanel =	this.getContentForm();
				//i_entityPanel.fireEvent("dirtychange", i_entityPanel.getContent().getForm(), b_dirty);
			}
		}
	},

	initComponent: function() {
		this.callParent(arguments);
		this.add(this.createPpoForm(this.dataRecord));
		this.add(this.createSouth());
	},

	createPpoForm : function(record) {
		var i_self = this;
		var form = Ext.widget("entityform", {
			dmd: "dmd/spi-ppo.dmd",
			entity: "PPO",
			view: "portfolio",
			keys: {id: record.keys.id},
			oldData: record,
			action: "update",
			closable: false,
			flex: 2,
			layout: "fit",
			split: true,
			activeTabIndex: 0,
			title: "",
			listeners: {
			}
		});
		this.setContentForm(form);
		return form;
	},

	createSouth: function(me, ppoId) {
		return this.createLinesGrid();
		return Ext.widget("panel", {
			title: "Order lines",
			split: true,
			flex: 1,
			layout: {
				type: "hbox",
				align: "stretch"
			},
			items: [
				this.createLinesGrid(),
				this.createLineForm()
			]
		});
	},

	createLineForm: function() {
		var i_form = Ext.widget("entityform", {
			dmd: "dmd/spi-ppo.dmd",
			entity: "PPOLines",
			view: "portfolio",
			keys: {id: 0},
			oldData: {},
			action: "update",
			closable: false,
			allowExport: false,
			allowLink: false,
			flex: 1,
			layout: "fit",
			split: true,
			activeTabIndex: 0,
			title: "",
			listeners: {
			}		
		});
		return i_form;
	},

	createLinesGrid: function() {
		var i_self = this;
		var linesGrid = Ext.widget("entity.grid", {
			autoLoad: false,
			searchCombo: false,
			filterable: false,
			allowSearch: false,
			allowExport: false,
			allowLink: false,
			showTotalCount: false,
			dmd: {
				path: "dmd/spi-ppo.dmd",
				entityName: "PPOLines"
			},
			uiName: "portfolio",
			flex: 1,
			split: true,
			listeners: {
				afterrender: function(me) {
					me.getStore().loadPage(1);
				},
				beforeupdate: function(me, config) {
				},
				beforeCreate: function(me, config) {
					config.data = {
						ppo_id: i_self.dataRecord.keys.id, 
						article_id: 0, 
						factory_location: i_self.getContentForm().keys.factory_location
					}
				}
			}
		});
		linesGrid.getStore().on({
			beforeLoad: {
				fn: function(store, operation) {
					var params = store.getProxy().getExtraParams();
					Ext.apply(params, {
						ppoid: i_self.dataRecord.keys.id
					});
				}
			}			
		});

		return linesGrid;
	},

	showInWindow() {
		var winSize = ip2.getWindowSize();
	
		var ppo_window = Ext.widget("window", {
			modal: false,
			closable: true,
			collapsible: true,
			resizable: true,
			width: winSize.width * 0.75,
			height: winSize.height * 0.96,
			layout: "fit",
			items: [this]
		});

		ppo_window.show();

	}
})