Ext.define("Portfolio.registry.controller.ui.Schema", {
	extend: "Ext.app.ViewController",

	alias: "controller.registry.ui.schema",

	alternateClassName: [
		"Ext.registry.controller.ui.Schema"
	],

	doOpen: function() {

		var i_controller = this;
		var i_window = i_controller.getView();
		var i_form = i_window.down("form").getForm();
		var i_viewModel = i_controller.getViewModel();
		var i_tree = i_viewModel.get("tree");;

		var o_values = i_form.getValues();
		o_values.file = i_form.findField("file").getRawValue();
		var o_options = i_tree.options;
		var a_fileSplit = o_values.file.split("/");

		o_options.library = o_values.library;
		o_options.file = a_fileSplit.pop();

		var i_root = i_tree.getRootNode();
		var i_store = i_tree.getStore();

		var i_proxy = i_store.getProxy();

		i_store.options = o_options;
		i_proxy.setUrl(Ext.registry.util.Service.getPath());
		i_root.removeAll(false);
		i_store.load({
			node: i_root
		});

		i_window.close();

		i_tree.doUpdate(o_options);
	},

	doSave: function(i_button) {

		var i_controller = this;
		var i_window = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		var i_tree = i_viewModel.get("tree");
		var o_options = i_tree.options;
		var o_values = i_window.getValue();

		var s_message = ip2.i18n({
			da: "Gemmer...",
			en: "Saving..."
		});

		i_window.el.mask(s_message);

		var s_path = "/portfolio/ip2-run-dynapp.js?" + Ext.Object.toQueryString({
			lib: o_values.library,
			file: o_values.table
		});

		var i_model = ip2.model({
			dmd: "dmd/portfolio.dmd",
			entityName: "NAVMNUET00",
			uiName: "portfolio",
			context: "form"
		});

		var i_record = i_model.create({
			MNUMNUTKN: 0,
			MNUPNTTKN:  o_values.APPAGR ? o_values.APPAGR : o_values.APPSYS,
			MNUPROP: Ext.encode({
				title: o_values.text,
				icon: o_values.APPICO,
				url: s_path,
				__cid__: 3
			})
		});

		var o_keys = {};

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

		var o_batch = Ext.apply({}, {
			type: "upsert",
			dmd: "dmd/portfolio.dmd",
			entity: "NAVMNUET00",
			key: o_keys,
			row: i_record.getData()
		});

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

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

				Ext.applyIf(o_result, {
					success: false,
					batch: [{
						row: {}
					}]
				});

				if (!o_result.success) {

					Portfolio.util.Handler.messageBox({
						icon: Ext.MessageBox.ERROR,
						title: ip2.i18n({
							da: "Fejl",
							en: "Error"
						}),
						closable: true,
						message: o_result.message,
						yesFn: function() {
							f_callback(false);
						},
						yesText: ip2.i18n({
							da: "OK",
							en: "OK"
						})
					});
				}
				else {
					if (o_result.success) {

						var a_rows = o_result.batch[0].row;

						f_callback(a_rows);
					}
				}
			}
		});
	}
});
