Ext.define("Portfolio.menu.view.Editor", {

	xtype: "portfolio-menu-editor",

	extend: "Portfolio.entity.view.Tree",
	
	alias: [
		"widget.portfolio-menu-editor"
	],

	dmd: "dmd/portfolio.dmd",
	entity: "NAVMNUET00",
	viewName: "portfolio",
	context: "grid",
	parentID: "MNUPNTTKN",
	childID: "MNUMNUTKN",
	searchCombo: false,
	allowLink: false,
	allowExport: false,
	allowFilter: true,
	allowSearch: true,
	autoLoad: false,
	sequence: true,

	requires: [
		"Portfolio.menu.form.Link",
		"Portfolio.menu.form.App"
	],

	config: {
		recordStore: null
	},

	initComponent: function() {

		var i_self = this;

		var i_model = i_self.createModel();

		var i_root = i_model.create({
			MNUMNUTKN: 1,
			MNUPNTTKN: 0,
			MNUPROP_NAME: "...",
			MNUPROP_SEQ: 0,
			PROPERTIES: "{title: \"...\"}"
		});

		i_self.root = i_root;
		
		i_self.callParent(arguments);

		i_self.createRecordStore();

		var i_store = i_self.getStore();
		var i_proxy = i_store.getProxy();

		i_proxy.setUrl("/wsh-services/wshProxy.aspx");

		i_store.on({
			beforeload: {
				fn: function(i_store, i_operation) {

					var i_proxy = i_store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();
					var i_node = i_operation.node;
					delete i_operation.node;

					Ext.apply(o_extraParams, {
						action: "treeStore",
						orderBy: "jsonGetNum(MNUPROP, 'seq')",
						parentidField: "MNUPNTTKN",
						propertiesField: "MNUPROP",
						entity: "NAVMNUET00",
						idField: "MNUMNUTKN",
						xpd: "navigation.xpd",
						dmd: "dmd/portfolio.dmd",
						displayCheckbox: false,
						node: i_node.get("ID")
					});

					delete o_extraParams.where;
				}
			},
			load: {
				fn: function(i_store, a_records) {

					var i_root = i_self.getRootNode();
					var n_count = 0;

					Ext.Array.each(a_records, function(i_record) {
						if (i_record.get("iconCls") == "webshop-parent-node") {
							i_record.set("iconCls", "pf-folder");
							i_record.commit();
						}
						if (i_record.get("iconCls") == "webshop-child-node") {
							i_record.set("iconCls", "pf-document_empty");
							i_record.commit();
						}
					});

					i_root.cascadeBy(function(i_node) {
						n_count++;
					});

					var i_options = i_self.getColumnManager().getHeaderByDataIndex("options");
					i_options.setText(n_count);
				}
			}
		});

		var a_columns = i_self.createColumns();

		i_self.reconfigure(null, a_columns);

		var i_rootNode = i_self.getRootNode();
		
		i_rootNode.set({
			allowDrag: false,
			allowDrop: true
		});

		i_rootNode.expand();

		i_self.events.itemcontextmenu.clearListeners();

		i_self.on({
			beforeitemdblclick: {
				fn: function(i_view, i_node, i_element, n_index, i_event) {
					i_self.editType(i_node);
					return false;
				}
			},
			itemcontextmenu: {
				fn: function(i_view, i_node, i_item, n_index, i_event) {

					i_event.preventDefault();

					var a_coords = i_event.getXY();
					var a_items = i_self.getMenuItems(i_node);

					var i_menu = Ext.widget("menu", {
						plain: true,
						items: a_items
					});

					i_menu.showAt(a_coords[0], a_coords[1]);
				}
			}
		});

	},

	createColumns: function() {

		var i_self = this;

		var a_columns = [
			{
				xtype: "treecolumn",
				header: "Folder",
				dataIndex: "TITLE",
				flex: 1,
				sortable: false,
				groupable: false,
				filterable: false,
				menuDisabled: true,
				renderer: function(value, i_node, i_record) {
		
					var o_properties = Ext.decode(i_record.get("PROPERTIES"), true) || {};
		
					if (Ext.isString(o_properties.title)) {
						if (o_properties.title.startsWith("{") && o_properties.title.endsWith("}")) {
							o_properties.title = Ext.decode(o_properties.title, true) || o_properties.title;
						}
					}
				
					return Ext.isObject(o_properties.title) ? ip2.i18n(o_properties.title) : o_properties.title;
				}
			},
			{
				header: "Url",
				dataIndex: "url",
				flex: 1,
				menuDisabled: true,
				sortable: false,
				filterable: false,
				renderer: function(value, i_node, i_record) {
	
					var o_properties = Ext.decode(i_record.get("PROPERTIES"), true) || {};
		
					return o_properties.url || o_properties.appFolder || "";
				}
			},
			{
				header: "Type",
				dataIndex: "text",
				flex: 1,
				align: "center",
				menuDisabled: true,
				sortable: false,
				filterable: false,
				renderer: function(value, i_node, i_record) {
		
					var s_text = value;
					var o_properties = Ext.decode(i_record.get("PROPERTIES"), true) || {};
		
					switch(o_properties.__cid__) {
						case 1:
							break;
						case 2:
							s_text = ip2.i18n({
								da: "Gruppe",
								en: "Group"
							});
							break;
						case 3:
							s_text = ip2.i18n({
								da: "Link",
								en: "Link"
							});
							break;
						case 4:
							s_text = ip2.i18n({
								da: "App",
								en: "App"
							});
							break;
					}
		
					return s_text;
				}
			},
			{
				xtype: "actioncolumn",
				align: "center",
				dataIndex: "options",
				menuDisabled: true,
				hideable: false,
				sortable: false,
				groupable: false,
				filterable: false,
				text: "0",
				items: [
					{
						xtype: "button",
						iconCls: "pf-options",
						width: 20,
						ui: "function-action",
						handler: function (i_tree, n_row, n_column, i_item, i_event, i_record, i_row) {
		
							var o_events = i_self.events || {
								itemcontextmenu: {}
							};
		
							var a_listeners = Ext.Array.from(o_events.itemcontextmenu.listeners);
		
							if (!Ext.isEmpty(a_listeners)) {
		
								var i_view = i_self.view;
								i_view.select(i_record);
		
								Ext.Array.each(a_listeners, function(i_listener) {
									i_listener.fn.call(i_self, i_view, i_record, i_row, n_row, i_event);
								});
							}
						}
					}
				]
			}
		];

		return a_columns;
	},

	createRecordStore: function() {

		var i_self = this;

		var s_view = i_self.getViewName();

		var i_model = i_self.createModel();

		var i_store = Ext.create("Ext.data.Store", {
			model: i_model,
			proxy: Portfolio.util.Handler.getProxyByEntity({
				dmd: i_self.getDmd(),
				entityName: i_self.getEntity(),
				uiName: i_self.getViewName(),
				context: "form"
			}),
			autoLoad: false,
			listeners: {
				beforeload: {
					fn: function(i_store, i_operation) {

						var i_proxy = i_store.getProxy();
						var o_extraParams = i_proxy.getExtraParams();
						
						var i_selected = i_self.getSelectionModel().getSelection()[0];

						var o_keys = {};
						
						o_keys[i_model.idProperty] = i_selected.get("ID");

						Ext.apply(o_extraParams, {
							action: "getRow",
							context: "form",
							dmdFile: i_self.getDmd(),
							uiName: s_view,
							key: Ext.encode(o_keys)
						});
					}
				}
			}
		});
		
		i_self.setRecordStore(i_store);
	},

	getMenuItems: function(i_node) {

		var i_self = this;
		var i_store = i_self.getStore();

		var o_properties = Ext.decode(i_node.get("PROPERTIES"), true) || {};

		var a_items = [];

		if (o_properties.__cid__ == 2 || i_node.isRoot()) {

			var i_addItem = Ext.widget("menuitem", {
				text: ip2.i18n({
					da: "Tilføj",
					en: "Add"
				}),
				menu: {
					plain: true,
					items: [
						{
							xtype: "menuitem",
							text: ip2.i18n({
								da: "Gruppe",
								en: "Group"
							}),
							handler: function() {
								i_self.createType(i_node, 2);
							}
						},
						{
							xtype: "menuitem",
							text: ip2.i18n({
								da: "Link",
								en: "Link"
							}),
							handler: function() {
								i_self.createType(i_node, 3);
							}
						},
						{
							xtype: "menuitem",
							text: ip2.i18n({
								da: "App",
								en: "App"
							}),
							handler: function() {
								i_self.createType(i_node, 4);
							}
						}
					]
				}
			});

			a_items.push(i_addItem);
		}

		if (!i_node.isRoot()) {

			var i_editItem = Ext.widget("menuitem", {
				text: ip2.i18n({
					da: "Redigér",
					en: "Edit"
				}),
				handler: function() {
					
					i_self.editType(i_node);
				}
			});

			a_items.push(i_editItem);

			if (i_node.isLeaf()) {
				
				var i_copyItem = Ext.widget("menuitem", {
					text: ip2.i18n({
						da: "Kopier",
						en: "Copy"
					}),
					handler: function() {
						
						i_self.copyType(i_node);
					}
				});
	
				a_items.push(i_copyItem);
			}

			var i_deleteItem = Ext.widget("menuitem", {
				text: ip2.i18n({
					da: "Slet",
					en: "Delete"
				}),
				handler: function() {

					Portfolio.util.Handler.messageBox({
						icon: Ext.Msg.WARNING,
						message: Portfolio.util.Handler.i18n({
							da: "Vil du slette " + i_node.get("TITLE"),
							en: "Do you want to delete "+  i_node.get("TITLE") 
						}),
						yesText: Portfolio.util.Handler.i18n({
							da: "Ja",
							en: "Yes"
						}),
						yesFn: function () {
			
							var i_model = i_self.createModel();

							var i_created = i_model.create({});

							i_created.set(i_model.idProperty, i_node.get("ID"));
							
							var o_keys = {};

							i_self.setLoading(ip2.i18n({
								da: "Sletter...",
								en: "Deleting..."
							}));

							o_keys[i_created.idProperty] = i_created.get(i_created.idProperty);

							var o_batch = Ext.apply({}, {
								type: "delete",
								dmd: i_self.getDmd(),
								entity: i_self.getEntity(),
								key: o_keys
							});

							Ext.Ajax.request({
								url: "/ip2-services/ip2dmdProxy.aspx",
								jsonData: {
									batch: [
										o_batch
									]
								},
								success: function (i_response) {

									i_self.setLoading(false);
									
									var i_store = i_self.getStore();

									var i_parent = i_node.isRoot() ? i_node : i_node.parentNode;

									i_parent.collapse();
									i_parent.removeAll(false);
			
									i_store.read({
										node: i_parent,
										callback: function() {
											i_parent.expand();
										}
									});
								},
								failure: function() {
									i_self.setLoading(false);
								}
							});
						},
						noText: Portfolio.util.Handler.i18n({
							da: "Nej",
							en: "No"
						})
					});
				}
			});

			a_items.push(i_deleteItem);
		}

		if (o_properties.__cid__ == 2 || i_node.isRoot()) {

			var i_refresh = Ext.widget("menuitem", {
				text: ip2.i18n({
					da: "Genindlæs",
					en: "Refresh"
				}),
				handler: function() {
	
					var i_selected = i_self.getSelection()[0];
	
					i_selected.collapse();
					i_selected.removeAll(false);
	
					i_store.read({
						node: i_selected,
						callback: function(i_store) {
							i_selected.expand();
						}
					});
				}
			});

			a_items.push(i_refresh);
		}

		return a_items;
	   
	},

	createType: function(i_parent, n_type) {

		var i_self = this;

		n_type = n_type || 1;

		var s_main = "";

		var s_title = ip2.i18n({
			da: "Opret",
			en: "Create"
		});

		var a_title = [
			s_title
		];

		switch(n_type) {
			case 2:
				s_main = "portfolio-menu-form-link";
				a_title.push(ip2.i18n({
					da: "gruppe",
					en: "group"
				}));
				break;
			case 3:
				s_main = "portfolio-menu-form-link";
				a_title.push(ip2.i18n({
					da: "link",
					en: "link"
				}));
				break;
			case 4:
				s_main = "portfolio-menu-form-app";
				a_title.push(ip2.i18n({
					da: "app",
					en: "app"
				}));
				break;
		}

		var i_window = Portfolio.util.Handler.getWindow({
			title: a_title.join(" - "),
			layout: "fit",
			modal: true
		});

		var i_model = i_self.createModel();

		var i_created = i_model.create({});

		var i_panel = Ext.widget(s_main, {
			record: i_created
		});

		var i_toolbar = Ext.widget("toolbar", {
			dock: "bottom",
			ui: "footer",
			items: [
				"->",
				{
					xtype: "button",
					text: ip2.i18n({
						da: "Annuller",
						en: "Cancel"
					}),
					tabIndex: 1,
					focusable: true,
					itemId: "close",
					handler: function () {
						i_window.close();
					}
				},
				{
					xtype: "button",
					text: ip2.i18n({
						da: "Gem",
						en: "Save"
					}),
					itemId: "saveclose",
					ui: "primary-button",
					hidden: true,
					focusable: true,
					tabIndex: 1,
					handler: function (i_button) {
						i_window.fireEvent("save", i_panel);
					}
				}
			]
		});

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

					var b_valid = i_panel.isValid();
					var i_content = i_panel.getContent();

					if (b_valid) {

						var i_saveclose = i_toolbar.items.get("saveclose");

						i_saveclose[b_dirty ? "show" : "hide"]();

						i_content.setIconCls(b_dirty ? "x-dot" : "");

					}
				}
			},
			validitychange: {
				fn: function(i_form, b_valid){

					var i_content = i_panel.getContent();

					var i_saveclose = i_toolbar.items.get("saveclose");

					i_content.tab.removeCls("x-tab-error");

					if (!b_valid) {
						i_content.tab.addCls("x-tab-error");
						i_saveclose.hide();
					}
					else {
						var b_dirty = i_panel.isDirty();
						i_panel.fireEvent("dirtychange", i_panel, b_dirty);
					}
				}
			}
		});

		i_window.on({
			save: {
				fn: function(i_panel) {

					i_window.setLoading(ip2.i18n({
						da: "Gemmer..",
						en: "Saving..."
					}));

					i_created.set("MNUPNTTKN", i_parent.get("ID"));

					var i_promise = new Ext.Promise(function(f_resolve) {

						var o_keys = {};

						o_keys[i_created.idProperty] = null;

						var o_batch = Ext.apply({}, {
							type: "upsert",
							dmd: i_self.getDmd(),
							entity: i_self.getEntity(),
							transactiontype: i_self.getEntity(),
							key: o_keys,
							row: i_created.getData()
						});

						Ext.Ajax.request({
							url: "/ip2-services/ip2dmdProxy.aspx",
							jsonData: {
								batch: [
									o_batch
								]
							},
							success: function (i_response) {

								var o_result = Ext.decode(i_response.responseText, true) || {
									batch: [{
										rows: [{}]
									}]
								};

								var o_batch = o_result.batch[0];
								var o_row = o_batch.row || {};

								i_created.set(o_row);
								i_panel.updateNewRecord(i_created);
								f_resolve();
								
							}
						});
					}).then(function(){
						return i_panel.doSave();
					}).then(function(o_value) {

						var a_children = Ext.Array.from(i_parent.childNodes);

						var o_last = Ext.decode(i_parent.get("PROPERTIES"), true) || {};

						if (a_children.length > 0) {
							var i_last = a_children[a_children.length -1];
							o_last = Ext.decode(i_last.get("PROPERTIES"), true) || {};
						}

						var o_properties = Ext.apply({
							__cid__: n_type,
							seq: parseFloat(o_last.seq) + 1
						}, o_value);

						i_created.set("MNUPROP", Ext.encode(o_properties));

						var o_keys = {};

						o_keys[i_created.idProperty] = i_created.get(i_created.idProperty);

						var o_batch = Ext.apply({}, {
							type: "upsert",
							dmd: i_self.getDmd(),
							entity: i_self.getEntity(),
							transactiontype: i_self.getEntity(),
							key: o_keys,
							row: i_created.getData()
						});

						Ext.Ajax.request({
							url: "/ip2-services/ip2dmdProxy.aspx",
							jsonData: {
								batch: [
									o_batch
								]
							},
							success: function (i_response) {
								var i_store = i_self.getStore();

								i_parent.collapse();
								i_parent.removeAll(false);
		
								i_store.read({
									node: i_parent,
									callback: function() {
										i_parent.expand();
										i_window.setLoading(false);
										i_window.close();
									}
								});
							},
							faliure: function(o_error) {
								i_window.setLoading(false);
							}
						});
					});
				}
			}
		});

		i_window.addDocked(i_toolbar);

		i_window.add(i_panel);
				
		i_window.show();
	},

	editType: function(i_node) {

		var i_self = this;

		var o_properties = Ext.decode(i_node.get("PROPERTIES"), true) || {};

		var i_model = i_self.createModel();

		var i_recordStore = i_self.getRecordStore();

		i_recordStore.load({
			callback: function(a_records, i_store) {

				var i_record = a_records[0];

				var o_values =  Ext.decode(i_record.get("MNUPROP"), null) || {};

				var i_created = i_model.create(o_values);
		
				i_created.set(i_record.idProperty, i_record.get(i_record.idProperty));
		
				var n_type = o_properties.__cid__;
		
				var s_title = ip2.i18n({
					da: "Redigér",
					en: "Edit"
				});
		
				var a_title = [
					s_title,
					i_node.get("TITLE")
				];
		
				var s_main = "";
		
				switch(n_type) {
					case 2:
					case 3:
						s_main = "portfolio-menu-form-link";
						break;
					case 4:
						s_main = "portfolio-menu-form-app";
						break;
				}
		
				var i_window = Portfolio.util.Handler.getWindow({
					title: a_title.join(" - "),
					layout: "fit",
					modal: true
				});
		
				var i_panel = Ext.widget(s_main, {
					record: i_created
				});
		
				var i_toolbar = Ext.widget("toolbar", {
					dock: "bottom",
					ui: "footer",
					items: [
						"->",
						{
							xtype: "button",
							text: ip2.i18n({
								da: "Annuller",
								en: "Cancel"
							}),
							tabIndex: 1,
							focusable: true,
							itemId: "close",
							handler: function () {
								i_window.close();
							}
						},
						{
							xtype: "button",
							text: ip2.i18n({
								da: "Gem",
								en: "Save"
							}),
							itemId: "saveclose",
							ui: "primary-button",
							hidden: true,
							focusable: true,
							tabIndex: 1,
							handler: function (i_button) {
								i_window.fireEvent("save", i_panel);
							}
						}
					]
				});
		
				i_panel.on({
					dirtychange: {
						fn: function(i_form, b_dirty) {
		
							var b_valid = i_panel.isValid();
							var i_content = i_panel.getContent();
		
							if (b_valid) {
		
								var i_saveclose = i_toolbar.items.get("saveclose");
		
								i_saveclose[b_dirty ? "show" : "hide"]();
		
								i_content.setIconCls(b_dirty ? "x-dot" : "");
		
							}
						}
					},
					validitychange: {
						fn: function(i_form, b_valid){
		
							var i_content = i_panel.getContent();
		
							var i_saveclose = i_toolbar.items.get("saveclose");
		
							i_content.tab.removeCls("x-tab-error");
		
							if (!b_valid) {
								i_content.tab.addCls("x-tab-error");
								i_saveclose.hide();
							}
							else {
		
								var b_dirty = i_panel.isDirty();
		
								if (b_dirty) {
									i_panel.fireEvent("dirtychange", i_panel, b_dirty);
								}
							}
						}
					}
				});
		
				i_window.on({
					save: {
						fn: function(i_panel) {
		
							i_window.setLoading(ip2.i18n({
								da: "Gemmer..",
								en: "Saving..."
							}));
		
							i_panel.doSave().then(function(o_values) {
		
								Ext.apply(o_properties, o_values);
		
								i_record.set("MNUPROP", Ext.encode(o_properties));
		
								var o_keys = {};
		
								o_keys[i_record.idProperty] = i_record.get(i_record.idProperty);
		
								var o_batch = Ext.apply({}, {
									type: "upsert",
									dmd: i_self.getDmd(),
									entity: i_self.getEntity(),
									transactiontype: i_self.getEntity(),
									key: o_keys,
									row: i_record.getData()
								});
		
								Ext.Ajax.request({
									url: "/ip2-services/ip2dmdProxy.aspx",
									jsonData: {
										batch: [
											o_batch
										]
									},
									success: function (i_response) {
										i_node.set("PROPERTIES", Ext.encode(o_properties));
										i_window.setLoading(false);
										i_window.close();
										i_node.commit(true);
									},
									faliure: function(o_error) {
										i_window.setLoading(false);
									}
								});
							});
						}
					}
				});
		
				i_window.addDocked(i_toolbar);
		
				i_window.add(i_panel);
						
				i_window.show();

			}
		});

	},

	copyType: function(i_node) {

		var i_self = this;

		var o_properties = Ext.decode(i_node.get("PROPERTIES"), true) || {};

		var i_model = i_self.createModel();

		var i_recordStore = i_self.getRecordStore();

		var i_parent = i_node.parentNode;

		i_recordStore.load({
			callback: function(a_records, i_store) {

				var i_record = a_records[0];

				var o_values =  Ext.decode(i_record.get("MNUPROP"), null) || {};

				var i_temp = i_model.create(o_values);
		
				var n_type = o_properties.__cid__;
		
				var s_title = ip2.i18n({
					da: "Kopier",
					en: "Copy"
				});
		
				var a_title = [
					s_title,
					i_node.get("TITLE")
				];
		
				var s_main = "";
		
				switch(n_type) {
					case 2:
					case 3:
						s_main = "portfolio-menu-form-link";
						break;
					case 4:
						s_main = "portfolio-menu-form-app";
						break;
				}
		
				var i_window = Portfolio.util.Handler.getWindow({
					title: a_title.join(" - "),
					layout: "fit",
					modal: true
				});
		
				var i_panel = Ext.widget(s_main, {
					record: i_temp
				});
		
				var i_toolbar = Ext.widget("toolbar", {
					dock: "bottom",
					ui: "footer",
					items: [
						"->",
						{
							xtype: "button",
							text: ip2.i18n({
								da: "Annuller",
								en: "Cancel"
							}),
							tabIndex: 1,
							focusable: true,
							itemId: "close",
							handler: function () {
								i_window.close();
							}
						},
						{
							xtype: "button",
							text: ip2.i18n({
								da: "Gem",
								en: "Save"
							}),
							itemId: "saveclose",
							ui: "primary-button",
							hidden: true,
							focusable: true,
							tabIndex: 1,
							handler: function (i_button) {
								i_window.fireEvent("save", i_panel);
							}
						}
					]
				});
		
				i_panel.on({
					dirtychange: {
						fn: function(i_form, b_dirty) {
		
							var b_valid = i_panel.isValid();
							var i_content = i_panel.getContent();
		
							if (b_valid) {
		
								var i_saveclose = i_toolbar.items.get("saveclose");
		
								i_saveclose[b_dirty ? "show" : "hide"]();
		
								i_content.setIconCls(b_dirty ? "x-dot" : "");
		
							}
						}
					},
					validitychange: {
						fn: function(i_form, b_valid){
		
							var i_content = i_panel.getContent();
		
							var i_saveclose = i_toolbar.items.get("saveclose");
		
							i_content.tab.removeCls("x-tab-error");
		
							if (!b_valid) {
								i_content.tab.addCls("x-tab-error");
								i_saveclose.hide();
							}
							else {
		
								var b_dirty = i_panel.isDirty();
		
								if (b_dirty) {
									i_panel.fireEvent("dirtychange", i_panel, b_dirty);
								}
							}
						}
					}
				});
		
				i_window.on({
					save: {
						fn: function(i_panel) {

							var i_created = i_model.create({});

							i_window.setLoading(ip2.i18n({
								da: "Gemmer..",
								en: "Saving..."
							}));

							i_created.set("MNUPNTTKN", i_parent.get("ID"));
		
							var i_promise = new Ext.Promise(function(f_resolve) {

								var o_keys = {};
		
								o_keys[i_created.idProperty] = null;
		
								var o_batch = Ext.apply({}, {
									type: "upsert",
									dmd: i_self.getDmd(),
									entity: i_self.getEntity(),
									transactiontype: i_self.getEntity(),
									key: o_keys,
									row: i_created.getData()
								});
		
								Ext.Ajax.request({
									url: "/ip2-services/ip2dmdProxy.aspx",
									jsonData: {
										batch: [
											o_batch
										]
									},
									success: function (i_response) {
		
										var o_result = Ext.decode(i_response.responseText, true) || {
											batch: [{
												rows: [{}]
											}]
										};
		
										var o_batch = o_result.batch[0];
										var o_row = o_batch.row || {};
		
										i_created.set(o_row);
										i_panel.updateNewRecord(i_created);
										f_resolve();
										
									}
								});
							}).then(function(){
								return i_panel.doSave();
							}).then(function(o_values) {
								
								var a_children = Ext.Array.from(i_parent.childNodes);

								var o_last = Ext.decode(i_parent.get("PROPERTIES"), true) || {};

								if (a_children.length > 0) {
									var i_last = a_children[a_children.length -1];
									o_last = Ext.decode(i_last.get("PROPERTIES"), true) || {};
								}

								var o_properties = Ext.apply({
									__cid__: n_type,
									seq: parseFloat(o_last.seq) + 1
								}, o_values);

								i_created.set("MNUPROP", Ext.encode(o_properties));

								var o_keys = {};

								o_keys[i_created.idProperty] = i_created.get(i_created.idProperty);

								var o_batch = Ext.apply({}, {
									type: "upsert",
									dmd: i_self.getDmd(),
									entity: i_self.getEntity(),
									transactiontype: i_self.getEntity(),
									key: o_keys,
									row: i_created.getData()
								});

								Ext.Ajax.request({
									url: "/ip2-services/ip2dmdProxy.aspx",
									jsonData: {
										batch: [
											o_batch
										]
									},
									success: function (i_response) {
										var i_store = i_self.getStore();

										i_parent.collapse();
										i_parent.removeAll(false);
				
										i_store.read({
											node: i_parent,
											callback: function() {
												i_parent.expand();
												i_window.setLoading(false);
												i_window.close();
											}
										});
									},
									faliure: function(o_error) {
										i_window.setLoading(false);
									}
								});
							});
						}
					}
				});
		
				i_window.addDocked(i_toolbar);
		
				i_window.add(i_panel);
						
				i_window.show();

			}
		});
	},

	createViewConfig: function() {

		var i_self = this;

		var i_model = i_self.createModel();

		var o_config = Ext.apply({}, {
			plugins: {
				ptype: "treeviewdragdrop",
				dropGroup: "entitytree",
				dragGroup: "entitytree",
				appendOnly: false,
				dropZone: {
					isValidDropPoint: function(i_node, s_position, i_dragZone, i_event, o_data) {

						var i_dropzone = this;
						var i_view = i_dropzone.view;
						var i_overRecord = i_view.getRecord(i_node);
						var a_records = Ext.Array.from(o_data.records);

						var i_record = a_records[0];

						var b_valid = false;

						if (i_overRecord != i_record) {
							if (!i_overRecord.isRoot()) {
								b_valid = s_position != "append";
							}
						}

						return b_valid;
					}
				}
			},
			listeners: {
				beforedrop: {
					fn: function(i_node, o_data, i_overModel, s_dropPosition, i_dropHandler, e) {

						var i_record = o_data.records[0];

						var i_before = i_overModel.previousSibling;
						var i_after = i_overModel.nextSibling;
						
						var o_properties = Ext.decode(i_overModel.get("PROPERTIES"), true) || {};
						var o_recordProperties = Ext.decode(i_record.get("PROPERTIES"), true) || {};

						var n_over = parseFloat(o_properties.seq);
						var n_extra = 0;
						var n_sequnce;


						if (isNaN(n_over)) {
							n_over = 1;
						}

						if (s_dropPosition === "before") {
							if (i_before) {
								var o_before = Ext.decode(i_before.get("PROPERTIES"), true) || {};
								n_extra = parseFloat(o_before.seq);

								if (isNaN(n_extra)) {
									n_extra = 0;
								}
							}

							n_sequnce = ((n_extra + n_over) / 2);
						}
						if (s_dropPosition === "after") {
							if (i_after) {
								var o_after = Ext.decode(i_after.get("PROPERTIES"), true) || {};
								n_extra = parseFloat(o_after.seq);

								if (isNaN(n_extra)) {
									n_extra = 0;
								}
							}
							else {
								n_extra = n_over + 1;
							}
							n_sequnce = ((n_over + n_extra) / 2);
						}
						
						o_recordProperties.seq = n_sequnce;

						i_record.set("PROPERTIES", Ext.encode(o_recordProperties));

						i_dropHandler.wait = true;

						var o_keys = {};

						o_keys[i_model.idProperty] = i_record.get("ID");

						Ext.Ajax.request({
							url: "/ip2-services/ip2dmdProxy.aspx",
							params: {
								action: "getRow",
								context: "form",
								dmdFile: i_self.getDmd(),
								uiName: i_self.getViewName(),
								model: i_self.getEntity(),
								key: Ext.encode(o_keys)
							},
							success: function (o_response) {
				
								var o_result = Ext.decode(o_response.responseText, true) || {
									success: false,
									rows: [{}]
								};
				
								if (!o_result.rows) {
									o_result.rows = [{}];
								}
				
								var o_data = o_result.rows[0];
				
								var i_loaded = i_model.create(o_data);

								var i_parent = i_overModel.parentNode;

								var n_tkn = i_parent.isRoot() ? i_parent.get("MNUMNUTKN") : i_parent.get("ID");
				
								i_loaded.set({
									MNUPROP: Ext.encode(o_recordProperties),
									MNUPNTTKN: n_tkn
								});

								i_self.upsertRecord(i_loaded);
								i_dropHandler.processDrop();
							},
							failure: function() {
				
								i_dropHandler.cancelDrop();
							}
						});
						
						return false;
					}
				}
			}
		});

		return o_config;
	}
});