﻿Ext.ns("eKonx.Membership.PermissionsInRole");
Ext.ns("eKonx.Plugins.Membership.PermissionsInRole");
//requied:
//config.roleID contains current role ID
//config.organizationID contains current organizationID
//config.getCurrentOrganizationRolePermissionsUrl - url to get list of organization permissions
//config.saveCurrentOrganizationRolePermissionsUrl - url to save list of organization permissions
//default url in eKonx.Urls.Membership
//all post contains parameters: roleID, organizationID

eKonx.Plugins.Membership.PermissionsInRole.IsInRoleColumn = new Ext.grid.CheckColumn(
{
	header: eKonx.Res.Common.Included,
	dataIndex: 'IsInRole',
	id: 'IsInRole',
	width: 55,
	editable: true
});
eKonx.Membership.PermissionsInRoleStore = new Ext.data.JsonStore(
{
	storeId: eKonx.Res.Membership.IDPrefix + 'PermissionsInRoleStore',
	root: "root",
	autoSave: false,
	autoLoad: false,
	remoteSort: true,
	idProperty: 'ID',
	proxy: new Ext.data.HttpProxy(
	{
		api:
		{
			read: eKonx.Urls.Membership.GetCurrentOrganizationRolePermissions,
			update: eKonx.Urls.Membership.SaveCurrentOrganizationRolePermissions
		},
		method: 'POST'
	}),
	writer: new Ext.data.JsonWriter({}),
	fields:
	[
		{ name: "SystemName" },
		{ name: "LocalizedName" },
		{ name: "Category" },
		{ name: "IsInRole", type: "boolean" },
		{ name: "ID" }
	]
});

eKonx.Membership.PermissionsInRole = Ext.extend(
Ext.grid.EditorGridPanel,
{
	PermissionsInRoleConfig:
	{
		title: eKonx.Res.Membership.PermissionsInRole,
		clicksToEdit: 1,
		plugins: [eKonx.Plugins.Membership.PermissionsInRole.IsInRoleColumn],
		cm: new Ext.grid.ColumnModel(
		[
			{
				dataIndex: "ID",
				header: "N",
				sortable: true,
				editable: false
			},
			{
				dataIndex: "SystemName",
				header: eKonx.Res.Common.Permission,
				sortable: true,
				editable: false
			},
			{
				dataIndex: "Category",
				header: eKonx.Res.Common.Category,
				sortable: true,
				editable: false
			},
			eKonx.Plugins.Membership.PermissionsInRole.IsInRoleColumn,
			{
				dataIndex: "LocalizedName",
				header: eKonx.Res.Common.Description,
				width: 380,
				sortable: false,
				editable: false
			}
		]),
		sm: eKonx.UI.GetCellSelectionModel(),
		store: eKonx.Membership.PermissionsInRoleStore,
		bbar:
		{
			xtype: "paging",
			store: eKonx.Membership.PermissionsInRoleStore,
			displayInfo: true,
			remoteSort: true,
			items:
			[
				'-',
				{
					margins: '0 10 0 0',
					xtype: "tbfill"
				},
				{
					xtype: 'button',
					text: eKonx.Res.Common.SaveButton,
					width: 50,
					handler: function()
					{
						var st = this.ownerCt.store;
						st.save();
						st.commitChanges();
					}
				}
			]
		},
		listeners:
		{
			beforeshow: function()
			{
				var st = this.getStore();
				if (this.getCurrentOrganizationRolePermissionsUrl)
					st.proxy.api.read.url = this.getCurrentOrganizationRolePermissionsUrl;
				if (this.saveCurrentOrganizationRolePermissions)
					st.proxy.api.update.url = this.saveCurrentOrganizationRolePermissions;
				st.baseParams = { organizationID: this.organizationID, roleID: this.roleID };
				st.reload();
			}
		}
	},
	constructor: function(config)
	{
		config = config || {};
		Ext.apply(config, this.PermissionsInRoleConfig);
		eKonx.Membership.PermissionsInRole.superclass.constructor.call(this, config);
	}
});


