TJ.Admin.WorkflowGridPanel = Ext.extend(Ext.grid.GridPanel,{
	stripeRows: true,
	border: false,
	objectClass: '',
	objectTable: 'All objects',
	title:  'Content roles for All objects',
	initComponent: function () {

		this.columns = [{	header: "","sortable":false,"width":30,renderer: this.iconRenderer},
						{	header: "User/Usergroup","sortable":true,"width":160,"dataIndex":"user", renderer: this.userRenderer},
						{	header: "Object","sortable":true,"width":160,"dataIndex":"object"},
						{	header: "Own","sortable":true,"width":60,"dataIndex":"own", renderer: this.ownRenderer			},
						{	header: "All","sortable":true,"width":60,"dataIndex":"all",renderer: this.allRenderer},
						{	header: "Scope","sortable":true,"width":80,"dataIndex":"scope", renderer: this.scopeRenderer}
						];





		this.deleteBtn= new Ext.Toolbar.Button({
			text: 'Remove',
			disabled: true,
			tooltip: 'Remove the selected entries',
			iconCls: 'icon_delete',
			scope: this,
			handler: function() {
				var records = this.getSelectionModel().getSelections();

				if(records.length) {
					var msg='<TABLE>';
					var ids = [];
					for(var i=0; i < records.length; i++) {
						msg=msg +'<TR><TD>' + (( records[i].data['id']< 0) ? 'Usergroup&nbsp; ':'User&nbsp; ')  + '</TD><TD>' + records[i].data['user']+'</TD></TR>';
						ids[i]=records[i].data['id'];
					}
					msg=msg+'</TABLE>';


					Ext.Msg.show({
					   title:'Please confirm',
					   width: 300,
					   msg: 'Are you sure you want to remove the selected users/usergroups?<BR> ' + msg,
					   buttons: Ext.Msg.YESNO,
					   records: records,
					   scope: this,
					   fn: function(btn){ if(btn=='yes') {

				   				Ext.Ajax.request ({ url:	TJ.LANGUAGESS + 'admin/security/roles:remove',
													ids: ids,
									params : {ids: ids.join(','), tableclass: this.objectClass},
									scope: this,
									callback: function(options, success, response)  {
										var res=TJ.Admin.ProcessJSONResponse(options,success,response);
											this.store.reload();
									}
								});
							}
					   },

					   animEl: this.deleteBtn,
					   icon: Ext.MessageBox.QUESTION
					});
				}
			}

		});



		this.tbar = [
		{
			text: 'Add User or Usergroup',
	 		iconCls: 'icon_user_add',
            handler : this.addUser,
			scope: this
		},
		this.deleteBtn
		];





//		this.typeFilter.on('select',function(cb){this.store.baseParams={table: cb.getValue()}; console.log(cb.getValue());this.store.reload();},this);
		this.store = new Ext.data.Store({
			remoteSort: false,
			proxy:		new Ext.data.HttpProxy ({
									url:  TJ.LANGUAGESS + 'admin/security/roles:getdata' }),
			reader:		new Ext.data.JsonReader({
										root: 'rows',
										totalProperty: 'numrows',
										id: 'id'},

										[{"name":"id","type":"int"},
										 {"name":"user"},
										 {"name":"object"},
										 {"name":"class"},
										 {"name":"own","type":"int"},
 										 {"name":"all","type":"int"},
 										 {"name":"scope"}
										])
		});


		this.sm = new Ext.grid.RowSelectionModel({
				singleSelect: this.singleSelect });
		this.bbar = new Ext.PagingToolbar({
			pageSize: 500,
			store: this.store,
			displayInfo: true,
			displayMsg: 'Displaying entries {0} - {1} of {2}',
			emptyMsg: 'No entries to display'
		});
		TJ.Admin.TableGridPanel.superclass.initComponent.call(this);

		this.getSelectionModel().on('selectionchange',function(sm){
			this.deleteBtn.setDisabled(sm.getCount()===0);
		},this);

	},
	setTableClass: function(table,objclass){
		this.objectClass =objclass;
		this.objectTable =table;
		this.setTitle('Content roles for ' + table);

		this.store.baseParams={table: objclass};
		if(this.rendered)
			this.store.reload();



	},
	iconRenderer: function(user, p,record){
		if (record.data.id < 0)
			return '<div class="icon_group" style="background: no-repeat;" title="Usergroup">&nbsp;</div>';
		return '<div class="icon_user" style="background: no-repeat;" title="User">&nbsp;</div>';

	},
	addUser: function(){
		this.addUserForm.table = {tableclass:this.objectClass, display: this.objectTable};
		this.addUserForm.show();
	},
	userRenderer: function(user, p,record){
		return user;
	},
	scopeRenderer: function(val){
		return String.format('{0} ({1})',TJ.TableScopes[val-1],val);
	},
	ownRenderer: function(own, p,record){
		if(own === 0) return 'None';
		if(own == 3) return 'Create';
		if(own == 4) return 'Publish';
		return '';
	},
	allRenderer: function(all){
		if(all === 0) return 'None';
		if(all == 1) return 'View';
		if(all == 2) return 'Translate';
		if(all == 3) return 'Edit';
		if(all == 4) return 'Publish';
		return '';
	},
    onRender : function(ct, position){
		TJ.Admin.TableGridPanel.superclass.onRender.call(this,ct,position);
		this.store.load();


		this.addUserForm = new Ext.Window ({
				layout: 'fit',
				modal: true,
				width: 600,
				height: 300,
				closeAction:'hide',
				plain: true,
				title: 'Add a new user role',
				items:
					new TJ.Admin.StatusPanelForm( {
						formconfig: {
							url:   TJ.LANGUAGESS + 'admin/security/roles:save',
							items:  this.userformfields,
							buttons:  [{"text":"Save","value":"close"}],
							listeners: { render: {scope:this, fn: function(fp) {
								var field;
								field = fp.getForm().findField('usergroup');
								field.on('change',function(f){if(f.getValue() > 0)  this.findField('user').setValue('');},fp.getForm());
								field = fp.getForm().findField('user');
								field.on('change',function(f){if(f.getValue() > 0)  this.findField('usergroup').setValue('');},fp.getForm());
							}},
							actioncomplete: {scope: this, fn: function(fp){ this.store.reload(); this.addUserForm.hide();}}
							}


						}
					}),
				listeners:{beforeshow:{scope:this, fn:function(win){
						win.setTitle('Add a new user role for ' + win.table.display);
						win.formPanel.form.getForm().reset();
						win.formPanel.form.getForm().setValues({table: win.table.tableclass});
						}},
						render: {scope:this, fn: function(win){
							win.formPanel = win.items.items[0];

							}}
						}
		});


	}
});

TJ.Admin.TypeTreePanel = Ext.extend(Ext.tree.TreePanel,{
		title: 'Choose Object',
		autoScroll:true,
		animate:true,
		enableDD:false,
		root:  new Ext.tree.TreeNode({
							text: '<b>All objects</b>',
							draggable: false,
							id: '0',
							typeclass: '',
							expanded: true,
							iconCls: 'icon_table_multiple'
								}),
		initComponent: function(){

			for (var i=0; i< this.types.length;i++)
					this.root.appendChild(new Ext.tree.TreeNode({text:this.types[i][1],typeclass:this.types[i][0], iconCls: 'icon_table'}));

			TJ.Admin.TypeTreePanel.superclass.initComponent.call(this);
		},
		render: function(ct,position){
			TJ.Admin.TypeTreePanel.superclass.render.call(this,ct,position);
			this.getRootNode().select();
	}

});




TJ.Admin.WorkflowPanel = Ext.extend(Ext.Panel, {
	title: 'Content Roles Management',
	border: false,
	frame: false,
	layout:'border',
	initComponent: function(){
		console.log(this);
//			TJ.TableScopes = this.scopes;
		this.wgp=new TJ.Admin.WorkflowGridPanel({userformfields :this.userformfields});




		this.ttp = new TJ.Admin.TypeTreePanel({types: this.tables});
				this.items = [ { region: 'west',
						 layout: 'fit',
						 items: this.ttp,
						 width: 300,
 						 split: true,
						 frame: false
						},
						{	region: 'center',
							layout: 'fit',
							items: this.wgp,
							frame: false
						}
						];
		TJ.Admin.WorkflowPanel.superclass.initComponent.call(this);
		this.ttp.getSelectionModel().on('selectionchange',function(sm,node){
			if(node){
				this.wgp.setTableClass(node.attributes.text, node.attributes.typeclass);

			}
		},this);



	},
	render: function(ct,position){
		TJ.Admin.WorkflowPanel.superclass.render.call(this,ct,position);

	}
});

Ext.reg('tjadmin_workflowpanel',TJ.Admin.WorkflowPanel);
