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

	extend: "Ext.window.Window",

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

	layout: "fit",

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

	controller: "dynapp.column.properties",

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

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

	initComponent: function() {

		var i_self = this;
		i_self.callParent();

		var i_viewModel = i_self.getViewModel();
		i_viewModel.setData({
			grid: i_self.grid
		});

		i_self.add(i_self.createLayout());

		return i_self;
	},

	createLayout: function() {

		var i_self = this;

		var i_form = Ext.widget("form", {
			layout: "anchor",
			trackResetOnLoad: true,
			autoScroll: true,
			bodyPadding: 5,
			defaults: {
				anchor: "100%"
			},
			items: [
				{
					xtype: "textfield",
					name: "library",
					fieldLabel: ip2.i18n({
						da: "Navn",
						en: "Name"
					}),
					enforceMaxLength: true,
					maxLength: 10,
					allowBlank: false,
					validator: function(value) {

						var s_error = true;
						var i_regex = new RegExp("^[0-9]", "g");
						if (i_regex.test(value)) {
							s_error = ip2.i18n({
								da: "Dette må ikke starte med tal",
								en: "This may not begin with a number"
							});
						}
						return s_error;
					}
				},
				{
					xtype: "textarea",
					name: "description",
					fieldLabel: ip2.i18n({
						da: "Beskrivelse",
						en: "Description"
					}),
					allowBlank: true
				}
			],
			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"
				}),
				itemId: "btn-save",
				handler: "onSchemaAdd"
			},
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Luk",
					en: "Close"
				}),
				itemId: "btn-close",
				handler: function() {
					i_self.close();
				}
			}
		];

		return a_buttons;
	}
});
