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

	
$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
$tabId = $_POST['uniqueTabId'];
$idProject = $_POST['id_project'];
$projName = $_POST['project'];

$capi = 1;
$tablePrefix = '_Capi';
$telTablePrefix = 'capi___';
$tabelIdField = 'id_capiTable';

	//lets get the telephone database
	$sql = "SELECT name,$tabelIdField FROM _Projects INNER JOIN {$tablePrefix}Tables ON {$tablePrefix}Tables.id = $tabelIdField  WHERE _Projects.id = $idProject";
	
	$telTable = $db->fetchRow($db->SQLexecute($sql));
	$telId = $telTable[1];
	$telTable = $telTable[0];
	
	if(!$telTable)
	{
		echo "Ext.Msg.alert('Error','Please attach {$tablePrefix} table to the project!');
		removeSession('$tabId')";
		exit;
	}

	$telTable = $telTablePrefix.$telTable;
	
	$cat=new MakeGrid($tabId.'_selector');
	$cat->reqUrl='webcati/_forms/sampler/window.capiSampler.php';
	$hideFields = array('tdisable','id','id_import','address','x_coordinate', 'y_coordinate', 'population', 'time_import','proj_reserved');
	
	// get all integer fields
	$fields = $db->getFields($telTable,'int');
	
	$cat->addColumn('id','sample fields');
	$cat->setCheckBoxSelectionModel()->setSortable(false)
	->addConfig('enableHdMenu','false');
	foreach ($fields as $fld) {
		if(!in_array($fld,$hideFields))
			$data[] = "'$fld'";
	}
	
	$cat->dataArray = "[ [".implode('],[',$data).'] ]'; 
	
	$cat->region('west');
	$cat->setWidth(200);
	
	$cat->addTbarItem("{xtype:'button',text:'Create sample',iconCls: 'RMicon_make_sample',handler:function(){
			var d = $cat.getSelectedIds(); 
			if(d.length == 0) return;
			CallScript('webcati/_forms/capiManagment/window.capiSampler.php',{
				projName:'$projName'
				,id_proj:'$idProject'
				,capi:$capi
				,capiTable:'$telTable'
				,id_capiName:$telId
				,fields:d.join() 
			});
	 }},{
	text:'Preview sample',
	iconCls:'RMicon_preview_cati',
	handler: function(){
		addTabAuto('manageSamples_$idProject','webcati/_forms/capiManagment/forms.manageCapiSamples.php',
			{
				id_proj:'$idProject'
				,projName:'$projName'
				,capiTable:'$telTable'
				,id_capiTable:$telId
			}
		);
	}
}");
	
	
	echo $cat->createGrid();
	echo "mainTabPanel.add({xtype:'panel',
			id: '$tabId',
			title: 'Create field sample - $projName',
			iconCls: 'RMicon_make_sample',
			closable: 'true',
			layout:'border',
			items:[
			$cat,
			{xtype:'panel',
				id: 'samplerHolder_$projName',
				layout:'card',
				region: 'center'
			}]
	}).show();"
	
?>	