Ext.define("Ext.ux.form.field.Registration", {

	alias: [
		"widget.ux.form.field.registration"
	],

	extend: "Ext.form.FieldContainer",

	mixins: {
		field: "Ext.form.field.Field"
	},

	config: {
		actionBar: null,
		grid: null
	},

	minWidth: 300,
	minHeigt: 200,

	layout: "fit",

	listeners: {
		afterrender: {
			fn: function(i_field) {
				i_field.fireEvent("change", i_field, i_field.getValue());
			}
		},
		change: {
			fn: function(i_field, value) {

				var i_user = Portfolio.util.Handler.getUser();

				var i_grid = i_field.getGrid();
				var i_store = i_grid.getStore();

				var n_index = i_store.find("USREML", i_user.get("USREML"));

				var i_actionBar = i_field.getActionBar();
				var i_buttion = i_actionBar.items.get("button-register");

				i_buttion[n_index > -1 ? "disable" : "enable"]();
			}
		}
	},

	initComponent: function() {

		var i_self = this;

		i_self.callParent(arguments);
		i_self.add(i_self.createGrid());
		i_self.initField();

		return i_self;
	},

	createActionBar: function() {

		var i_self = this;

		var i_actionBar = Ext.widget("toolbar", {
			dock: "top",
			items: [
				{
					xtype: "button",
					iconCls: "pf-add",
					itemId: "button-register",
					text: ip2.i18n({
						da: "Tilmeld",
						en: "Register"
					}),
					handler: function(i_button) {

						var i_user = Portfolio.util.Handler.getUser();

						var i_grid = i_self.getGrid();
						var i_store = i_grid.getStore();

						i_store.loadData([{
							USRUSRTKN: i_user.get("USRUSRTKN"),
							NOTIFY: true,
							USREML: i_user.get("USREML")
						}], true);
					}
				}
			]
		});

		i_self.setActionBar(i_actionBar);

		return i_actionBar;
	},

	createGrid: function() {

		var i_self = this;

		var i_grid = Ext.widget("gridpanel", {
			columns: i_self.createColumns(),
			store: i_self.createStore(),
			listeners: {
				itemcontextmenu: {
					fn: function(i_view, i_record, i_html, n_index, i_event) {

						i_event.stopEvent();

						var i_user = Portfolio.util.Handler.getUser();

						var a_coords = i_event.getXY();

						if (i_record.get("USREML") == i_user.get("USREML")) {

							var a_items = [];

							var i_delete = Ext.widget("menuitem", {
								text: ip2.i18n({
									da: "Slet",
									en: "Delete"
								}),
								itemId: "itmDelete",
								iconCls: "pf-delete",
								handler: function () {

									var i_grid = i_self.getGrid();
									var i_store = i_grid.getStore();

									i_store.remove(i_record);
								}
							});

							a_items.push(i_delete);

							var i_menu = Ext.create("Ext.menu.Menu", {
								allowOtherMenus: false,
								items: a_items
							});

							i_menu.showAt([a_coords[0], a_coords[1]]);
						}
					}
				}
			}
		});

		i_grid.addDocked(i_self.createActionBar());

		i_self.setGrid(i_grid);

		return i_grid;
	},

	createStore: function() {

		var i_self = this;

		var i_store = Ext.create("Ext.data.Store", {
			autoLoad: false,
			fields: [
				{
					name: "NOTIFY",
					type: "boolean"
				},
				{
					name: "USREML",
					type: "string"
				}
			],
			listeners: {
				datachanged: {
					fn: function(i_store) {
						i_self.checkChange();
					}
				},
				update: {
					fn: function() {
						i_self.checkChange();
					}
				}
			}
		});

		return i_store;
	},

	createColumns: function() {

		var a_columns = [
			{
				xtype: "checkcolumn",
				dataIndex: "NOTIFY",
				text: ip2.i18n({
					da: "Aktiv",
					en: "Active"
				}),
				width: 80,
				sortable: false,
				menuDisabled: true,
				draggable: false,
				listeners: {
					beforecheckchange: {
						fn: function(i_column, n_index, b_checked) {

							var i_owner = i_column.ownerCt;
							var i_grid = i_owner.grid;
							var i_store = i_grid.getStore();
							var i_record = i_store.getAt(n_index);

							var i_user = Portfolio.util.Handler.getUser();

							return i_user.get("USREML") == i_record.get("USREML");
						}
					}
				}
			},
			{
				dataIndex: "USREML",
				sortable: false,
				menuDisabled: true,
				draggable: false,
				text: ip2.i18n({
					da: "E-mail",
					en: "E-mail"
				}),
				flex: 1
			}
		];

		return a_columns;
	},

	getValue: function() {

		var i_self = this;

		var i_grid = i_self.getGrid();
		var i_store = i_grid.getStore();

		var a_records = i_store.getRange();

		var a_data = [];

		Ext.Array.each(a_records, function(i_record) {

			a_data.push(i_record.getData());
		});

		return Ext.encode(a_data);
	},

	setValue: function(value) {

		var i_self = this;
		var i_grid = i_self.getGrid();
		var i_store = i_grid.getStore();

		if (Ext.isEmpty(value, false)) {
			value = null;
		}

		if (Ext.isBoolean(value)) {

			var i_user = Portfolio.util.Handler.getUser();

			value = {
				USRUSRTKN: i_user.get("USRUSRTKN"),
				NOTIFY: true,
				USREML: i_user.get("USREML")
			};
		}

		var a_values = Ext.Array.from(value);

		if (Ext.isString(value)) {
			value = Ext.decode(value, true) || "";

			a_values = Ext.Array.from(value);
		}

		i_store.loadData(a_values);

		return i_self.mixins.field.setValue.call(i_self, i_self.getValue());
	},

	isDirty: function() {

		var i_self = this;

		var s_originalValue = i_self.originalValue;
		var s_value = i_self.getValue();

		return s_originalValue != s_value;
	}
});
