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

	extend: "Ext.window.Window",

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

	layout: "fit",

	viewModel: {
		type: "registry"
	},

	controller: "registry.ui.schema",

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

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

	initComponent: function() {

		var i_self = this;
		i_self.callParent();

		i_self.add(i_self.createLayout());

		return i_self;
	},

	createLayout: function() {

		var i_self = this;

		var i_schemaStore = Ext.create("Ext.data.Store", {
			fields: [
				{ name: "key" },
				{ name: "value" }
			],
			proxy: {
				type: "ajax",
				url: "ip2-services/ip2Services.aspx",
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			},
			listeners: {
				beforeload: {
					fn: function(store, operation) {

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

						Ext.apply(o_extraParams, {
							plugin: "LIB",
							func: "getDataModels"
						});
					}
				}
			}
		});

		var i_fileStore = Ext.create("Ext.data.Store", {
			fields: [
				{ name: "key" },
				{ name: "value" }
			],
			proxy: {
				type: "ajax",
				url: "ip2-services/ip2Services.aspx",
				extraParams: {
					plugin: "DB2",
					func: "getDataModels"
				},
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			},
			listeners: {
				beforeload: {
					fn: function(store, operation) {

						var i_proxy = store.getProxy();
						var o_extraParams = i_proxy.getExtraParams();
						var i_comboLib = i_self.down("#cmb-lib");

						if (i_comboLib) {
							Ext.apply(o_extraParams, {
								lib: i_comboLib.getValue()
							});
						}
					}
				}
			}
		});

		var i_form = Ext.widget("form", {
			layout: "anchor",
			trackResetOnLoad: true,
			autoScroll: true,
			bodyPadding: 5,
			defaults: {
				anchor: "100%"
			},
			items: [
				{
					xtype: "combobox",
					fieldLabel: ip2.i18n({
						da: "Skema",
						en: "Schema"
					}),
					name: "library",
					itemId: "cmb-lib",
					displayField: "key",
					valueField: "value",
					allowBlank: false,
					store: i_schemaStore,
					queryMode: "remote",
					minChars: 1,
					triggerAction: "all",
					listeners: {
						afterrender: {
							fn: function(item) {
								item.focus();
							}
						}
					}
				},
				{
					xtype: "combobox",
					fieldLabel: ip2.i18n({
						da: "Tabel",
						en: "Table"
					}),
					name: "file",
					allowBlank: false,
					displayField: "key",
					valueField: "value",
					store: i_fileStore,
					queryMode: "remote",
					minChars: 1,
					triggerAction: "all",
					listeners: {
						select: function(i_field, i_record) {

							if (i_record.get(i_field.valueField)) {

								var s_value = i_record.get(i_field.valueField);
								var a_split = s_value.split("/");
								var s_lib = a_split.shift();
								var i_comboLib = i_self.down("#cmb-lib");

								if (i_comboLib) {
									i_comboLib.setValue(s_lib);
									i_comboLib.setRawValue(s_lib);
									i_comboLib.validate();
								}
							}
						}
					}
				}
			],
			listeners: {
				validitychange: {
					fn: function(i_form, b_valid) {
						var i_open = i_self.down("#btn-open");
						if (i_open) {
							if (b_valid) {
								i_open.enable();
							}
							else {
								i_open.disable();
							}
						}
					}
				}
			},
			buttons: i_self.createButtons()
		});

		return i_form;
	},

	createButtons: function() {

		var i_self = this;
		var a_buttons = [
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Åben",
					en: "Open"
				}),
				itemId: "btn-open",
				handler: "doOpen"
			},
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Luk",
					en: "Close"
				}),
				itemId: "btn-close",
				handler: function() {
					i_self.close();
				}
			}
		];

		return a_buttons;
	}
});
