Ext.define("Portfolio.designer.import.view.Main", {

	extend: "Ext.form.Panel",

	border: false,

	bodyPadding: 10,

	layout: "anchor",

	defaults: {
		xtype: " textfield",
		labelAlign: "left",
		labelWidth: 150,
		anchor: "100%"
	},

	requires: [
		"Portfolio.designer.ui.DataModels"
	],

	config: {
		importType: "library"
	},

	alias: [
		"widget.designer.import.view.main"
	],

	initComponent: function() {

		var i_self = this;

		i_self.buttons = i_self.createButtons();

		i_self.callParent();

		i_self.add(i_self.createItems());

		i_self.enableBubble([
			"import",
			"cancel"
		]);
	},

	createButtons: function() {

		var i_self = this;

		var a_buttons = [];

		var i_okButton = Ext.widget("button", {
			text: "Import",
			handler: function(i_button) {

				var i_form = i_button.up("form").getForm();

				var o_extend = {};
				var s_importType = i_self.getImportType();

				var i_field = i_form.findField("table");

				if (i_form.isValid()) {

					if (s_importType == "library") {
						o_extend.library = i_field.getValue();
						o_extend.table = "*all";
					}
					else {

						var s_table = i_field.getValue();
						var a_split = s_table.split("/");

						o_extend = Ext.apply(o_extend,  {
							type: "baseTable",
							baseTable: s_table,
							table: a_split[1],
							library: a_split[0]
						});

					}

					var s_params = Ext.Object.toQueryString({
						action: "getImportEntityList",
						lib: o_extend.library,
						tbl: o_extend.table
					});

					Ext.Ajax.request({
						url: "/ip2-services/ip2dmdProxy.aspx?" + s_params,
						success: function (i_response) {
							var o_result = Ext.decode(i_response.responseText);
							var o_model = o_result.model;

							i_self.fireEvent("import", i_self, o_result);
						},
						failure: function() {
							i_self.fireEvent("cancel", i_self);
						}
					});
				}
			}
		});

		var i_cancel = Ext.widget("button", {
			text: "Cancel",
			handler: function(i_button) {

				i_self.fireEvent("cancel", i_self);
			}
		});

		a_buttons.push(i_okButton);
		a_buttons.push(i_cancel);

		return a_buttons;
	},

	createItems: function() {

		var i_self = this;
		var s_importType = i_self.getImportType();

		var s_plugin = "";
		var s_label = "";

		switch(s_importType) {
			case "library":
				s_label = "Library";
				s_plugin = "LIB";
				break;
			case "table":
				s_label = "Table";
				s_plugin = "DB2";
				break;
		}

		var a_items = [];

		var i_dataField = Ext.widget("ip2DataModels", {
			fieldLabel: s_label,
			name: "table",
			allowBlank: false,
			minChars: s_importType == "table" ? 4 : 2,
			listeners: {
				afterrender: {
					fn: function(i_field) {
						i_field.focus(false);
					},
					buffer: 250
				}
			}
		});

		var i_store = i_dataField.getStore();

		i_store.on({
			beforeload: {
				fn: function(i_store, i_operation) {

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

					Ext.apply(o_extraParams, {
						plugin: s_plugin
					});
				}
			}
		});

		a_items.push(i_dataField);

		return a_items;
	}
});
