ip2.declare("PortFolio.user.table.Access", {
	xtype: "container",
	require: [
		"Ext.ux.navigation.view.RolePermissionPanel"
	],
	layout: "accordion",
	onInitialize: function(widget, owner) {
		Ext.Ajax.request({
			url: "/ip2-services/ip2transaction.aspx",
			jsonData: {
				action: "ip2registry.getTree",
				key: "%",
				registry: "NAVDTAACC"
			},
			success: function(response, request) {
				var o_result = {};
				try {

					o_result = JSON.parse(response.responseText);
					var o_tables = o_result.tables;
					var a_keys = Ext.Object.getKeys(o_tables.child || {});

					Ext.Array.each(a_keys, function(s_key) {

						var o_child = o_tables.child[s_key];
						var o_properties = o_child.child || {};
						var s_table = "";
						var s_libl = "";
						var s_dmd = "";
						var s_ui = "";
						var o_extra = {};

						if (o_properties.table) {
							s_table = o_properties.table.value;
						}
						if (o_properties.libl && o_properties.libl.value != "*LIBL") {
							s_libl = o_properties.libl.value;
						}
						if (o_properties.dmd) {
							s_dmd = o_properties.dmd.value;
						}
						if (o_properties.ui) {
							s_ui = o_properties.ui.value;
						}
						if (o_properties.extraparams) {

							o_child = o_properties.extraparams.child || {};
							if (o_child.where) {
								o_extra.where = o_child.where.value;
							}
						}
						if (s_table) {
							var i_panel = Ext.widget("rolepermissionpanel", {
								record: owner.dataParent.getRecord(),
								oldID: owner.dataParent.oldID,
								options: {
									library: s_libl,
									file: s_table,
									dmd: s_dmd,
									ui: s_ui,
									extraParams: o_extra
								},
								key: o_child.key
							});

							var i_top = Ext.widget("panel", {
								title: ip2.i18n(o_child.value),
								layout: "fit",
								items: [i_panel]
							});
							widget.add(i_top);
						}

					});
				}
				catch(err) {

				}
			}
		});


	},
	isDirty: function() {

		var i_self = this;
		var a_grids = i_self.query("rolepermissiongrid");
		var b_dirty = false;
		Ext.Array.each(a_grids, function(i_grid) {

			if (!b_dirty) {

				var i_store = i_grid.getStore();
				var a_records = i_store.getRange();
				var a_original = i_store.originalRecords = i_store.originalRecords || [];

				if (a_records.length != a_original.length) {
					b_dirty = true;
					return false;
				}

				Ext.Array.each(a_records, function(i_record) {
					var b_found = false;
					Ext.Array.each(a_original, function(i_original) {
						if (i_record.get(i_record.idProperty) == i_original.get(i_original.idProperty)) {
							b_found = true;
							return false;
						}
					});
					if (!b_found) {
						b_dirty = true;
						return false;
					}
				});
				if (b_dirty) {
					return false;
				}
			}
		});

		i_self.dirty = b_dirty;

		return b_dirty;
	},
	resetDirty: function() {

		var i_self = this;
		var a_grids = i_self.query("rolepermissiongrid");

		Ext.Array.each(a_grids, function(i_grid) {

			var i_store = i_grid.getStore();
			var a_records = i_store.getRange();

			Ext.Array.each(a_records, function(i_record) {
				i_record.commit(true);
			});

			i_store.originalRecords = a_records;
		});

		i_self.dirty = false;
		var i_widget = i_self.up("ip2widget");
		if (i_widget) {
			i_widget.fireEvent("dirtychange", i_widget, false);
		}
	},
	doSave: function(f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var i_self = this;
		var i_widget = i_self.up("ip2widget");
		var a_panel = i_self.query("rolepermissionpanel");

		var n_call = a_panel.length || 1;

		var f_ticker = function() {
			n_call--;
			if (n_call === 0) {
				f_callback();
				i_self.resetDirty();
			}
		};

		if (a_panel.length > 0) {
			Ext.Array.each(a_panel, function(i_panel) {

				var i_controller = i_panel.getController();
				var i_viewModel = i_controller.getViewModel();
				i_viewModel.set({
					record: i_widget.dataParent.getRecord()
				});
				var i_grid = i_viewModel.get("permissionGrid");
				var i_store = i_grid.getStore();
				var a_records = i_store.getRange();
				var a_removed = i_store.getRemovedRecords();

				var a_toRemove = [];

				Ext.Array.each(a_removed, function(i_removed) {
					var b_found = false;
					Ext.Array.each(a_records, function(i_record) {
						if (i_record.get(i_record.idProperty) == i_removed.get(i_removed.idProperty)) {
							b_found = true;
							return false;
						}
					});
					if (!b_found) {
						a_toRemove.push(i_removed);
					}
				});

				i_controller.onGridDrop(a_records, function(o_result) {
					if (a_toRemove.length > 0) {
						i_controller.onRowsRemove(a_toRemove, function(o_result) {
							f_ticker();
						});
					}
					else {
						f_ticker();
					}
				});
			});
		}
		else {
			f_ticker();
		}
	},
	doDelete: function(f_callback) {

		f_callback = f_callback || Ext.emptyFn;
		f_callback();
	}
});
