Ext.define("Portfolio.dynapp.controller.column.Properties", {

	extend: "Ext.app.ViewController",

	alias: "controller.dynapp.column.properties",

	action: "new",

	alternateClassName: [
		"Ext.dynapp.controller.column.Properties"
	],

	onAfterRender: function(item) {

		var i_controller = this;
		var i_propertyGrid = i_controller.getView();
		var i_record = i_propertyGrid.getRecord();
		var s_type = i_record.get("type");

		if (i_propertyGrid.meta) {
			var o_meta = i_propertyGrid.meta;
			if (o_meta.properties) {
				var o_properties = o_meta.properties;
				delete o_meta.properties;
				o_properties = Ext.applyIf(o_meta, o_properties);
				o_meta.properties = o_properties;
				o_meta.properties.type = o_meta.dataType || o_meta.datatype || "varchar";
				o_meta.properties.title = o_meta.title || o_meta.text || o_meta.header || "";
				o_meta.properties.nulls = o_meta.properties.nulls || 1;

				i_record.set(o_meta.properties);
			}
			else {
				i_record.set({
					name: o_meta.dataIndex,
					len: o_meta.len,
					title: o_meta.text,
					type: o_meta.datatype,
					dec: o_meta.dec,
					nulls: 1
				});

				var s_type = i_record.get("type");

				var o_template = i_propertyGrid.types[s_type];
				if (o_template && o_template.defaults) {
					for (var name in o_template.defaults) {
						i_record.set(name, o_template.defaults[name]);
					}
				}
			}
		}

		i_propertyGrid.updateValues(i_record);
	},

	onPropertyChange: function(source, recordId, value) {

		var i_controller = this;
		var i_propertyGrid = i_controller.getView();
		var i_record = i_propertyGrid.getRecord();
		i_record.set(recordId, value);
		if (recordId == "title") {
			var s_value = value;
			var i_window = i_propertyGrid.up("window");
			if (i_window.action == "new") {
				var s_title = i_window.oldTitle || i_window.getTitle();
				if (Ext.isObject(value)) {
					s_value = ip2.i18n(value);
				}
				var s_newTitle = i_window.setTitle(s_title + ": " + s_value);
				i_window.oldTitle = s_title;
			}
		}
	},

	onSave: function(i_button) {

		var i_controller = this;
		var i_window = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		var i_grid = i_viewModel.get("grid");
		var o_options = i_window.options || {};
		var o_values = i_window.getValue();
		o_values.properties.plugin = o_values.plugin;

		var o_data = {
			column: o_values.properties,
			position: o_values.position,
			dataIndex: o_values.dataIndex
		};
		var s_msg = "";
		if (i_window.action == "edit") {
			o_data.action = "alterColumn";
			s_msg = ip2.i18n({
				da: "Opdater...",
				en: "Updating..."
			});
		}
		else {
			o_data.action = "createColumn";
			s_msg = ip2.i18n({
				da: "Opretter...",
				en: "Creating..."
			});
		}

		i_window.el.mask(s_msg);

		Ext.Ajax.request({
			url: Portfolio.dynapp.util.Service.getPath(o_options),
			method: "POST",
			jsonData: o_data,
			success: function (response, request) {
				var result = Ext.decode(response.responseText, true) || {};
				if (result.success != undefined && !result.success) {
					i_window.el.unmask();
					ip2.messageBox({
						icon: Ext.MessageBox.ERROR,
						title: ip2.i18n({
							da: "Fejl",
							en: "Error"
						}),
						closable: true,
						message: result.message,
						yesText: ip2.i18n({
							da: "OK",
							en: "OK"
						})
					});
				}
				else {
					if (result.success) {
						Portfolio.dynapp.util.Service.getMeta(o_options, function(o_meta) {
							i_window.close();
							o_options.properties = o_meta.properties;
							o_options.srcPlugin = o_meta.srcPlugin;
							i_grid.doApply(o_options, o_meta.fields);
						});
					}
				}
			}
		});
	},

	onOpen: 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_grid = i_viewModel.get("grid");
		var i_main = i_grid.up("#dynapp-main");

		var o_values = i_form.getFieldValues();
		var o_options = i_grid.options;
		var a_fileSplit = o_values.file.split("/");

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

		var s_msg = ip2.i18n({
			da: "Åbner...",
			en: "Opening..."
		});

		i_window.el.mask(s_msg);

		Portfolio.dynapp.util.Service.getMeta(o_options, function(o_meta) {
			o_options.properties = o_meta.properties;
			o_options.srcPlugin = o_meta.srcPlugin;
			i_window.close();
			i_grid.doApply(o_options, o_meta.fields);
			if (i_main) {
				i_main.doUpdate(o_options);
			}
		});
	},

	onSchemaAdd: function() {

		var i_controller = this;
		var i_window = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		var i_grid = i_viewModel.get("grid");
		var o_options = i_window.options || {};
		var o_values = i_window.getValue();
		var i_main = i_grid.up("#dynapp-main");

		var o_data = {
			schema: o_values.library,
			description: o_values.description
		};

		var s_msg = "";
		o_data.action = "createSchema";
		s_msg = ip2.i18n({
			da: "Opretter...",
			en: "Creating..."
		});

		i_window.el.mask(s_msg);

		Ext.Ajax.request({
			url: Portfolio.dynapp.util.Service.getPath(o_options),
			method: "POST",
			jsonData: o_data,
			success: function (response, request) {
				i_window.el.unmask();
				var result = Ext.decode(response.responseText, true) || {};
				if (result.success != undefined && !result.success) {
					ip2.messageBox({
						icon: Ext.MessageBox.ERROR,
						title: ip2.i18n({
							da: "Fejl",
							en: "Error"
						}),
						closable: true,
						message: result.message,
						yesText: ip2.i18n({
							da: "OK",
							en: "OK"
						})
					});
				}
				else {
					if (result.success) {
						i_window.close();
						ip2.messageBox({
							icon: Ext.MessageBox.INFO,
							title: ip2.i18n({
								da: "Success",
								en: "success"
							}),
							closable: true,
							message: ip2.i18n({
								da: "Oprettelse komplet",
								en: "Creation complete"
							}),
							yesFn: function() {
								i_window.close();
								i_main.doUpdate(o_values);
							},
							yesText: ip2.i18n({
								da: "OK",
								en: "OK"
							})
						});
					}
				}
			}
		});
	},

	onTableAdd: function() {

		var i_controller = this;
		var i_window = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		var i_grid = i_viewModel.get("grid");
		var o_options = i_window.options || {};
		var o_values = i_window.getValue();
		var i_main = i_grid.up("#dynapp-main");

		var o_data = {
			schema: o_values.schema,
			table: o_values.table,
			description: o_values.description
		};

		var s_msg = "";
		o_data.action = "createTable";
		s_msg = ip2.i18n({
			da: "Opretter...",
			en: "Creating..."
		});

		i_window.el.mask(s_msg);

		Ext.Ajax.request({
			url: Portfolio.dynapp.util.Service.getPath(o_options),
			method: "POST",
			jsonData: o_data,
			success: function (response, request) {
				i_window.el.unmask();
				var result = Ext.decode(response.responseText, true) || {};
				if (result.success != undefined && !result.success) {
					ip2.messageBox({
						icon: Ext.MessageBox.ERROR,
						title: ip2.i18n({
							da: "Fejl",
							en: "Error"
						}),
						closable: true,
						message: result.message,
						yesText: ip2.i18n({
							da: "OK",
							en: "OK"
						})
					});
				}
				else {
					if (result.success) {
						o_options.library = o_values.schema;
						o_options.file = o_values.table;
						ip2.messageBox({
							icon: Ext.MessageBox.INFO,
							title: ip2.i18n({
								da: "Success",
								en: "success"
							}),
							closable: true,
							message: ip2.i18n({
								da: "Oprettelse komplet",
								en: "Creation complete"
							}),
							yesFn: function() {
								var s_msg = "";
								s_msg = ip2.i18n({
									da: "Henter meta data...",
									en: "Getting meta data..."
								});
								i_window.el.mask(s_msg);
								Portfolio.dynapp.util.Service.getMeta(o_options, function(o_meta) {
									i_window.close();
									o_options.plugin = o_meta.properties.plugin;
									o_options.srcPlugin = o_meta.srcPlugin;
									i_grid.doApply(o_options, o_meta.fields);
									if (i_main) {
										i_main.doUpdate(o_options);
									}
								});
							},
							yesText: ip2.i18n({
								da: "OK",
								en: "OK"
							})
						});
					}
				}
			}
		});
	},

	onSaveApp: function(i_button, f_callback) {

		var i_controller = this;
		var i_window = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		var i_grid = i_viewModel.get("grid");
		var o_options = i_grid.options || {};
		var o_values = i_window.getValue();

		var s_path = "/portfolio/ip2-run-dynapp.js?" + Ext.Object.toQueryString({
			lib: o_values.schema,
			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);
					}
				}
			}
		});
	},

	onClose: function() {

		var i_controller = this;
		var i_window = i_controller.getView();
		i_window.close();
	}
});
