<?php

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_grid.php");

$tabId = $_POST['uniqueTabId'];
//convert tree id to project id!
$reqUrl = '_forms/_ajax/Permissions/ajax.projectUsers.php';

//check if user types exist
$sql = "SELECT count(*) as cnt,user_type FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids".( $_POST['id'] == 'all' ? '' :  " WHERE id_project IN ({$_POST['id']})" ) ." GROUP BY user_type";
	$res = $db->SQLexecute($sql);
	while ($row = $db->fetchAssoc($res))
	{
		$types[$row['user_type']] = $row['cnt']; 
	}
	
if($types[1] > 0 || $types[2] == 0)
{
	$gridCati = new MakeGrid( $tabId.'_cati' );
	
	$gridCati->setMainTable('_www_ids',DB_WARPIT_WEBCATI_BASE)
		->setTitle('Cati - users')
		->activateRemove()
		->setCheckBoxSelectionModel()
		->setReqUrl($reqUrl)
		->setPaging(100)
		->setSearchField()
		->setGrouping('proj_name')
		->flex(1)
		->baseParams = "par: '{$_POST['id']}',type:1";
		
	$gridCati->addColumn('proj_name','Project');
	$gridCati->addColumn('loginSID','Login ID');
	
	$gridCati->addColumn('id');
	$gridCati->addColumn('id_type');
	/*$gridCati->addColumn('user_type','Pool')->renderer("function(val){
		return val==1 ? 'Webcati' : 'Userspool';
	}");*/
	
	$gridCati->addColumn('username','Username');
	$gridCati->addColumn('password','Password')->renderer("function(val,md,rec){
			return rec.data.id_type == 1 ? '*****' : val;
	}");
	$gridCati->addColumn('Firstname','First name');
	$gridCati->addColumn('Surname','Surname');
	$gridCati->addColumn('email','Email')->width(200);
	//$gridCati->addColumn('language','Language')->width(55);
	//$gridCati->addColumn('debugMode','Debug mode')->type('checkColumn')->width(70);
	//$gridCati->addColumn('permissionGroup','Group')->type('combo')->comboTable("_PermissionGroups|".$_POST['par'],'remote')->comboValue('id')->comboDisplay('idLab')
		;
	$gridArray[] = $gridCati->getName();
	echo $gridCati->createGrid();
}
if($types[2]>0)
{
	$grid = new MakeGrid($tabId.'_usersPool');
	$grid->setMainTable('_www_ids',DB_WARPIT_WEBCATI_BASE)
		->setTitle('Users pool')
		->activateRemove()
		->setCheckBoxSelectionModel()
		->setReqUrl($reqUrl)
		->setPaging()
		->setSearchField()
		->setGrouping('id_project')
		->flex(1)
		->baseParams = "par: '{$_POST['id']}',type:2";
		
	$grid->addColumn('id');
	$grid->addColumn('id_project','Project')->hidden(true);
	
	$grid->addColumn('loginSID','Login ID');
		
	include (PATH_TO_ROOT.'webuserspool/_forms/grid.showgroup.php');
	
	$gridArray[] = $grid->getName();
	echo $grid->createGrid();	
}
echo "mainTabPanel.add({ 
		id:'$tabId',
		closable:true,
		title: 'Project - Users',
		RMreopen : function(p){ 
			$gridCati.getStore().reload( { params:{
				par: p.id
			} } );
		},
		iconCls: 'icon-webuserspool-tab',
		layout:'vbox',
		items:[
".implode(',',$gridArray). " ] }).show();";


?>