Ext.define("Portfolio.registry.ui.schema.Save", {

	extend: "Ext.window.Window",

	alias: "widget.registry.ui.schema.save",

	layout: "fit",

	controller: "registry.ui.schema",

	viewModel: {
		type: "registry"
	},

	alternateClassName: [
		"Ext.registry.ui.schema.Save"
	],

	requires: [
		"Portfolio.registry.viewmodel.Registry",
		"Portfolio.registry.controller.ui.Schema",
	],

	initComponent: function() {

		var i_self = this;
		i_self.callParent();
		var i_viewmodel = i_self.getViewModel();
		i_viewmodel.setData({
			tree: i_self.tree
		});
		delete i_self.tree;
		i_self.add(i_self.createLayout());

		return i_self;
	},

	createLayout: function() {

		var i_self = this;
		var i_viewModel = i_self.getViewModel();
		var i_tree = i_viewModel.get("tree");
		var o_options = i_tree.options;

		var i_systemStore = Ext.create("Ext.data.Store", {
			fields: [
				{ name: "SYSTOK", type: "int" },
				{ name: "TITLE", type: "string" }
			],
			proxy: {
				type: "ajax",
				url: "/ip2-service/ip2navigator.aspx",
				reader: {
					type: "json",
					rootProperty: "rows"
				}
			},
			listeners: {
				beforeload: {
					fn: function(i_store, i_operation) {

						var i_proxy = i_store.getProxy();
						var o_extraParams = i_proxy.getExtraParams();

						Ext.apply(o_extraParams, {
							func: "database",
							dbName: "navsys",
							action: "list"
						});
					}
				}
			}
		});

		var i_applicationGroupStore = Ext.create("Ext.data.Store", {
			fields: [
				{ name: "AGRTOK", type: "int" },
				{ name: "TITLE", type: "string" }
			],
			proxy: {
				type: "ajax",
				url: "/ip2-service/ip2navigator.aspx",
				params: {
					func: "database",
					dbName: "navagr",
					action: "list"
				},
				reader: {
					type: "json",
					rootProperty: "rows"
				}
			},
			listeners: {
				beforeload: function(store) {

					var i_proxy = store.getProxy();
					var o_extraParams = i_proxy.getExtraParams();
					var i_cmb = i_self.down("#cmb-system");
					if (i_cmb) {
						var value = i_cmb.getValue();
						Ext.apply(o_extraParams, {
							system: value
						});
					}
				}
			}
		});

		var i_fieldset = Ext.widget("fieldset", {
			title: ip2.i18n("applicationLocation"),
			collapsible: true,
			defaults: {
				anchor: "100%",
				labelWidth: 190,
				width: 610
			},
			layout: "anchor",
			items: []
		});

		var i_uploadpicker = Ext.widget({
			xtype: "uploadpicker",
			path: "/system/shared/collection/icons/16x16/plain",
			minChars: 1,
			fieldLabel: ip2.i18n("APPICO"),
			name: "APPICO",
			allowBlank: true,
			ignoreStaticDir: true
		});

		var i_cmbSystem = Ext.widget("combobox", {
			fieldLabel: ip2.i18n("APPSYS"),
			name: "APPSYS",
			itemId: "cmb-system",
			displayField: "TITLE",
			valueField: "SYSTOK",
			allowBlank: false,
			store: i_systemStore,
			queryMode: "remote",
			minChars: 1,
			triggerAction: "all",
			listeners: {
				select: {
					fn: function(a_records) {
						var i_cmbApplicationGroup = i_self.down("#cmb-appGroup");
						if (i_cmbApplicationGroup) {
							i_cmbApplicationGroup.clearValue();
							var i_store = i_cmbApplicationGroup.getStore();
							i_store.load();
						}
					}
				}
			}
		});

		var i_cmbApplicationGroup = Ext.widget("combobox", {
			fieldLabel: ip2.i18n("APPAGR"),
			name: "APPAGR",
			itemId: "cmb-appGroup",
			displayField: "TITLE",
			valueField: "AGRTOK",
			allowBlank: false,
			store: i_applicationGroupStore,
			queryMode: "remote",
			minChars: 1,
			triggerAction: "all"
		});

		i_fieldset.add(i_cmbSystem);
		i_fieldset.add(i_cmbApplicationGroup);

		var i_form = Ext.widget("form", {
			layout: "anchor",
			trackResetOnLoad: true,
			autoScroll: true,
			bodyPadding: 5,
			defaults: {
				anchor: "100%"
			},
			items: [
				{
					xtype: "textfield",
					fieldLabel: ip2.i18n({
						da: "Skema",
						en: "Schema"
					}),
					name: "library",
					allowBlank: false,
					readOnly: true,
					value: o_options.library
				},
				{
					xtype: "textfield",
					fieldLabel: ip2.i18n({
						da: "Tabel",
						en: "Table"
					}),
					name: "file",
					allowBlank: false,
					readOnly: true,
					value: o_options.file
				},
				{
					xtype: "i18ntextfield",
					fieldLabel: ip2.i18n({
						da: "Title",
						en: "Title"
					}),
					value: o_options.file,
					languages: wsh.getLanguages("portfolio"),
					name: "text",
					listeners: {
						afterrender: function(item) {
							item.focus(100, false);
						}
					}
				},
				i_uploadpicker,
				i_fieldset
			],
			listeners: {
				validitychange: {
					fn: function(i_form, b_valid) {
						var i_open = i_self.down("#btn-save");
						if (i_open) {
							if (b_valid) {
								i_open.enable();
							}
							else {
								i_open.disable();
							}
						}
					}
				}
			},
			buttons: i_self.createButtons()
		});

		return i_form;
	},

	getValue: function() {

		var i_self = this;
		var i_form = i_self.down("form").getForm();
		var o_values = i_form.getFieldValues();
		return o_values;
	},

	createButtons: function() {

		var i_self = this;
		var a_buttons = [
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Gem",
					en: "Save"
				}),
				disabled: true,
				itemId: "btn-save",
				handler: "doSave"
			},
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Luk",
					en: "Close"
				}),
				itemId: "btn-close",
				handler: function() {
					i_self.close();
				}
			}
		];

		return a_buttons;
	}
});
