Ext.define("SPI.imlppo.controller.Main", {
	extend: "Ext.app.ViewController",

	alias: "controller.rest.controller.main",

	onAfterRender: function(i_panel) {

		var i_controller = this;
		//i_panel.events.itemdblclick.clearListeners();
		i_panel.events.itemcontextmenu.clearListeners();
		
//		i_panel.on({
//			itemdblclick: {
//				fn: "onEdit"
//			}
//		});
	},
	
	onEdit: function(i_panel, i_record) {
		var i_controller = this;
		var i_view = i_controller.getView();

		var o_keys = {};

		//o_keys[i_record.idProperty] = i_record.get(i_record.idProperty);

		var i_window = Ext.widget("window", {
			width: "90%",
			height: "90%",
			layout: "fit",
			modal: false,
			border: false
		});

		var i_entityForm = Ext.widget("entityform", {
			dmd: "dmd/spi-ppo.dmd",
			entity: "IMLQuantity",
			view: "portfolio",
			keys: o_keys,
			oldData: i_record.getData(),
			action: "update",
			dataParent: i_window,
			transactiontype: "IMLQuantity",
			activeTabIndex: 0,
			closable: false,
			createTitle: false
		});
		
		var model = ip2.model({dmd: 'dmd/spi-ppo.dmd', entity: 'IMLQuantity', view: 'portfolio', context: 'form'});
		
		model.setProxy(Ext.create('Ext.data.proxy.Rest', {
			type: "rest",
			url: "api/imlquantity",
			reader: {type: 'json', rootProperty: '_embedded.IMLQuantity'},
			actionMethods : {
			   update: "PATCH"
			}
			}
		));

		i_entityForm.upsertRecords = function(a_records, o_info, f_callback, n_token) {
			f_callback = f_callback || Ext.emptyFn;
			Ext.Array.each(a_records, function(i_record) {
				var i_save = model.create({});
				i_save.set(i_record.getData());
				i_save.save();
			})
			f_callback(a_records);
		}
		
		var i_actionBar = i_entityForm.getActionbar();
		i_actionBar.hide();

		var i_save = Ext.widget("themedbutton", {
			text: ip2.i18n({
				da: "Gem",
				en: "Save"
			}),
			disabled: true,
			iconCls: "icon-save",
			itemId: "button-save",
			handler: function() {

				i_window.setLoading(true);

				if (i_entityForm) {
					i_entityForm.save(function(i_record) {

						i_window.setLoading(false);

						i_window.close();
					});
				}
			}
		});

		var i_close = Ext.widget("themedbutton", {
			text: ip2.i18n({
				da: "Luk",
				en: "Close"
			}),
			iconCls: "icon-close",
			itemId: "button-close",
			handler: function() {

				i_window.close();
			}
		});

		var a_buttons = [];

		a_buttons.push("->");
		a_buttons.push(i_save);
		a_buttons.push(i_close);

		var i_footer = Ext.widget("toolbar", {
			dock: "bottom",
			ui: "footer",
			defaults: {
				minWidth: 75
			},
			items: a_buttons
		});

		i_entityForm.addDocked(i_footer);

		i_entityForm.on({
			dirtychange: {
				fn: function(i_form, b_dirty) {

					var b_valid = i_entityForm.isValid();

					if (b_valid) {
						i_save[b_dirty ? "enable" : "disable"]();
					}
					else {
						i_save.disable();
					}
				}
			},
			validitychange: {
				fn: function(i_form, b_valid) {

					if (b_valid) {
						var b_dirty = i_entityForm.isDirty();
						i_save[b_dirty ? "enable" : "disable"]();
					}
					else {
						i_save.disable();
					}
				}
			}
		});

		i_window.add(i_entityForm);

		i_window.show();
		
		/*Portfolio.app.fireEvent("startapplication", {
			title: "Test",
			dmd: "dmd/ctu-spi.dmd",
			entity: "IMLQuantity",
			view: "portfolio",
			oldData: i_record.getData(),
			keys: o_keys,
			action: "update",
			transactiontype: "IMLQuantity",
			activeTabIndex: 0
		});*/
	}
});
