﻿Ext.ns("eKonx.Organizations");
Ext.ns("eKonx.Plugins.Organizations");
Ext.ns("eKonx.Res.Organizations");
Ext.ns("eKonx.Urls.Organizations");

eKonx.Res.Organizations =
{
	CreatePermission: "Создать новое разрешение",
	CreateRole: "Создать новую роль",
	Domain: "Домен",
	NewRoleName: "Имя новой роли",
	NewPermissionName: "Имя нового разрешения",
	NewPermissionDescription: "Описание нового разрешения",
	NewPermissionCategory: "Категория нового разрешения",
	Title: "Организации",
	PageDescription: "Управление организациями, зарегистрированными в системе.",
	OrganizationName: "Организация",
	OrganizationSystemName: "В системе",
	RoleInOrganization: "Включена",
	RolesInOrganization: "Роли в организации",
	PermissionsInOrganization: "Разрешения в организации",
	SiteImagesPath: "Путь к изображениям",
	SiteImagesUrl: "Url к изображениям",
	IDPrefix: "ofe_"
}

eKonx.Urls.Organizations =
{
	GetOrganizationsForEdit: "Organizations/GetOrganizationsForEdit",
	SaveOrganizationsForEdit: "Organizations/SaveOrganizationsForEdit",
	GetOrganizationRoles: "Organizations/GetOrganizationRoles",
	SaveOrganizationRoles: "Organizations/SaveOrganizationRoles",
	GetOrganizationPermissions: "Organizations/GetOrganizationPermissions",
	SaveOrganizationPermissions: "Organizations/SaveOrganizationPermissions",
	CreateRole: "Organizations/CreateRole",
	CreatePermission: "Organizations/CreatePermission"
}

eKonx.Plugins.Organizations.AllowedColumn = new Ext.grid.CheckColumn(
{
	dataIndex: "Allowed",
	header: eKonx.Res.Common.Allowed,
	sortable: true,
	width: 100,
	editable: true
});

eKonx.OrganizationsStore = new Ext.data.JsonStore(
{
	storeId: eKonx.Res.Organizations.IDPrefix + 'OrganizationsStore',
	root: "root",
	autoSave: false,
	autoLoad: true,
	remoteSort: true,
	idProperty: 'ID',
	proxy: new Ext.data.HttpProxy(
	{
		api:
		{
			read: eKonx.Urls.Organizations.GetOrganizationsForEdit,
			update: eKonx.Urls.Organizations.SaveOrganizationsForEdit
		},
		method: 'POST'
	}),
	writer: new Ext.data.JsonWriter({}),
	fields:
	[
		{ name: "Name" },
		{ name: "SystemName" },
		{ name: "Domain", type: "string" },
		{ name: "Description", type: "string" },
		{ name: "Allowed", type: "boolean" },
		{ name: "SiteImagesPath", type: "string" },
		{ name: "SiteImagesUrl", type: "string" },
		{ name: "ID" }
	]
});

eKonx.Organizations = Ext.extend(
Ext.Panel,
{
	OrganizationsConfig:
	{
		title: eKonx.Res.Organizations.Title,
		height: 590,
		items:
		[
			{
				html: eKonx.Res.Organizations.PageDescription,
				height: 40,
				bodyStyle: "padding: 10px 10px 10px 10px;",
				border: false
			},
			{
				xtype: 'editorgrid',
				id: eKonx.Res.Organizations.IDPrefix + 'Organizations',
				height: 550,
				forceFit: true,
				clicksToEdit: 1,
				plugins: [eKonx.Plugins.Organizations.AllowedColumn],
				cm: new Ext.grid.ColumnModel(
				[
					{
						dataIndex: "ID",
						header: "N",
						sortable: true,
						editable: false
					},
					{
						dataIndex: "Name",
						header: eKonx.Res.Organizations.OrganizationName,
						sortable: true,
						editable: false
					},
					{
						dataIndex: "SystemName",
						header: eKonx.Res.Organizations.OrganizationSystemName,
						sortable: true,
						editable: false
					},
					{
						dataIndex: "Domain",
						header: eKonx.Res.Organizations.Domain,
						sortable: true,
						editable: true
					},
					{
						dataIndex: "SiteImagesPath",
						header: eKonx.Res.Organizations.SiteImagesPath,
						width: 150,
						sortable: true,
						editable: true
					},
					{
						dataIndex: "SiteImagesUrl",
						header: eKonx.Res.Organizations.SiteImagesUrl,
						width: 150,
						sortable: true,
						editable: true
					},
					eKonx.Plugins.Organizations.AllowedColumn,
					{
						dataIndex: "ID",
						header: 'Edit',
						width: 35,
						isAction: true,
						sortable: true,
						align: 'center',
						css: 'cursor:pointer;',
						click: function(column, rowIndex, data)
						{
							var data = this.grid.getStore().getAt(rowIndex).data;
							//if (eKonx.Cache.User.Role != "Application Administrator") return;
							this.grid.ownerCt.GetOrganizationEditorWindow(
								this.grid.ownerCt,
								{
									OrganizationID: data.ID,
									IsNew: false
								}).show();
						},
						renderer: function(val)
						{
							if (eKonx.Cache.User.Role == "Application Administrator")
								return "<img src='../../I/Actions/Modify.gif' style='cursor:hand'/>";
							return "";
						}
					},
					{
						dataIndex: "ID",
						header: "Fields",
						width: 40,
						isAction: true,
						align: 'center',
						css: 'cursor:pointer;',
						renderer: function(val)
						{
							if (eKonx.Cache.User.Role == "Application Administrator")
								return "<img src='../../Scripts/ext-3.0.0/resources/icons/fam/application_form_edit.png' />";
							return "";
						},
						click: function(column, rowIndex, data)
						{
							debugger;
						}
					}
				]),
				sm: eKonx.UI.GetCellSelectionModel(),
				store: eKonx.OrganizationsStore,
				bbar: new Ext.PagingToolbar(
				{
					//pageSize: 25,
					store: eKonx.OrganizationsStore,
					displayInfo: true,
					//displayMsg: 'Displaying topics {0} - {1} of {2}',
					//emptyMsg: "No topics to display",
					remoteSort: true,
					items:
					[
						'-',
						{
							margins: '0 10 0 0',
							xtype: "tbfill"
						},
						'-',
						{
							xtype: 'button',
							text: "Формы",
							handler: function()
							{
								var wforms = eKonx.Organizations.Personalization.GetPersonalizationEditor(1);
								wforms.show();
							}
						},
						'-',
						{
							xtype: 'button',
							text: eKonx.Res.Common.AddButton,
							width: 50,
							handler: function()
							{
								var panel = this.findParentByType('editorgrid').ownerCt;
								panel.GetOrganizationEditorWindow(panel, { IsNew: true }).show();
							}
						},
						'-',
						{
							xtype: 'button',
							text: eKonx.Res.Common.SaveButton,
							width: 50,
							handler: function()
							{
								var st = this.findParentByType("editorgrid").getStore();
								st.save();
								st.commitChanges();
							}
						}
					]
				})
			}
		]
	},
	GetOrganizationEditorWindow: function(parent, data)
	{
		return new Ext.Window(
		{
			modal: true,
			organizationID: data.OrganizationID,
			isNew: data.IsNew,
			width: 830,
			height: 560,
			layout: "fit",
			closable: true,
			resizable: true,
			parent: parent,
			items:
			[
				{
					xtype: 'tabpanel',
					id: eKonx.Res.Organizations.IDPrefix + 'OrganizationEditorPanel',
					activeTab: 0,
					border: false,
					listeners: 	//for tabpanel
					{
					beforerender: function()
					{
						this.add(new eKonx.Organizations.Roles({ organizationID: data.OrganizationID }));
						this.add(new eKonx.Organizations.Permissions({ organizationID: data.OrganizationID }));
						this.setActiveTab(0);
					}
				},
				items: []
			}
			]
		});
	},
	constructor: function(config)
	{
		config = config || {};
		Ext.apply(config, this.OrganizationsConfig);
		eKonx.Organizations.superclass.constructor.call(this, config);
	}
});
