Ext.define("Portfolio.dynapp.controller.Table", {

	extend: "Ext.app.ViewController",

	alias: "controller.dynapp.table",

	alternateClassName: [
		"Ext.dynapp.controller.Table"
	],

	onSave: function(i_button) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_viewModel = i_view.getViewModel();
		var i_grid = i_viewModel.get("grid");
		var i_store = i_grid.getStore();
		var i_record = i_viewModel.get("record");

		if (!i_view.edit) {
			i_store.loadData([i_record], true);
			i_store.fireEvent("added", i_store, [i_record]);
			i_grid.view.refresh();
		}
		else {
			i_store.fireEvent("edit", i_store, [i_record]);
			i_record.commit();
		}
		i_view.close();
	},

	getFields: function(s_type, s_schema) {

		var i_self = this;
		var a_fields;
		switch (s_type) {
			case "affiliation":
				a_fields = i_self.getAffiliation();
				break;
			case "relation":
				a_fields = i_self.getRelations(s_schema);
				break;
		}
		return a_fields;
	},

	getModel: function(s_type) {

		var i_model;
		switch (s_type) {
			case "affiliation":
				i_model = Ext.ClassManager.get("Portfolio.dynapp.model.Affiliation");
				break;
			case "relation":
				i_model = Ext.ClassManager.get("Portfolio.dynapp.model.Relation");
				break;
		}
		return i_model;
	},

	getRelations: function(s_schema) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_schemaStore = Ext.create("Ext.data.Store", {
			fields: [
				{ name: "key" },
				{ name: "value" }
			],
			proxy: {
				type: "ajax",
				url: "ip2-services/ip2Services.aspx",
				params: {
					plugin: "LIB",
					func: "getDataModels"
				},
				reader: {
					type: "json",
					rootProperty: "rows",
					totalProperty: "totalRows"
				}
			}
		});

		var a_fields = [
			{
				xtype: "combotable",
				name: "table",
				schema: s_schema,
				itemId: "cmb-table",
				allowBlank: false,
				fieldLabel: ip2.i18n({
					da: "Tabel",
					en: "Table"
				}),
				bind: {
					value: "{record.table}"
				}
			},
			{
				xtype: "combobox",
				name: "schema",
				emptyText: "*CURRENT",
				allowBlank: true,
				fieldLabel: ip2.i18n({
					da: "Skema",
					en: "Schema"
				}),
				bind: {
					value: "{record.schema}"
				},
				itemId: "cmb-schema",
				displayField: "key",
				valueField: "value",
				allowBlank: false,
				store: i_schemaStore,
				queryMode: "remote",
				minChars: 1,
				triggerAction: "all",
				listeners: {
					select: {
						fn: function(item) {
							var i_cmbTable = i_view.down("#cmb-table");
							if (i_cmbTable) {
								i_cmbTable.schema = item.getValue();
								i_cmbTable.clearValue();
								var store = i_cmbTable.getStore();
								store.load();
							}
						}
					}
				}
			},
			{
				xtype: "textfield",
				name: "join",
				allowBlank: true,
				fieldLabel: ip2.i18n({
					da: "Join",
					en: "Join"
				}),
				bind: {
					value: "{record.join}"
				}
			}
		];
		return a_fields;
	},

	getAffiliation: function() {

		var a_fields = [
			{
				xtype: "textfield",
				name: "name",
				fieldLabel: ip2.i18n({
					da: "Navn",
					en: "Name"
				}),
				bind: {
					value: "{record.name}"
				}
			},
			{
				xtype: "textfield",
				name: "url",
				fieldLabel: ip2.i18n({
					da: "Url",
					en: "Url"
				}),
				bind: {
					value: "{record.url}"
				}
			}
		];
		return a_fields;
	}
});
