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

	extend: "Ext.window.Window",

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

	layout: "fit",

	viewModel: {
		type: "dynapp.columnproperties"
	},

	controller: "dynapp.column.properties",

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

	requires: [
		"Portfolio.dynapp.controller.column.Properties",
		"Portfolio.dynapp.viewmodel.ColumnProperties"
	],

	config: {
		grid: null
	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent();

		var i_viewModel = i_self.getViewModel();

		i_viewModel.setData({
			grid: i_self.getGrid()
		});

		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",
				extraParams: {
					plugin: "LIB",
					func: "getDataModels"
				},
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			}
		});

		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_cmb = i_self.down("#cmb-lib");

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

		var i_schema = Ext.widget("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(i_field) {
						i_field.focus(false, 10);
					},
					buffer: 250
				}
			}
		});

		var i_table = Ext.widget("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_cmbLib = i_self.down("#cmb-lib");
						if (i_cmbLib) {
							i_cmbLib.setValue(s_lib);
							i_cmbLib.setRawValue(s_lib);
							i_cmbLib.validate();
						}
					}
				}
			}
		});

		var i_form = Ext.widget("form", {
			layout: "anchor",
			bodyPadding: 5,
			defaults: {
				anchor: "100%"
			},
			items: [
				i_schema,
				i_table
			],
			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: "onOpen"
			},
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Luk",
					en: "Close"
				}),
				itemId: "btn-close",
				handler: function() {
					i_self.close();
				}
			}
		];

		return a_buttons;
	}
});
