<?php

include("_dirinfo.php");

include (PATH_TO_ROOT."init.php");
//include (PATH_TO_ROOT."webhelp/_class/class.Extjs_tree.php");
//include (PATH_TO_ROOT."_support/class.UserSession.php");
include (PATH_TO_ROOT."_class/class.Extjs_grid.php");

//$UserSession = new UserSession($db);

$treeName = $_POST[treeName].'Help';

$used_lang = 1;

//topic grid
$gridTopic = new MakeGrid();

$gridTopic->setMainTable($_POST['table'],DB_WARPIT_HELP);

/*if($help['topic']<=0) $gridTopic->setTitle("Topics");
else $gridTopic->setTitle("Sub-Topics");*/

//$gridTopic->addConfig("anchor", "'-10 -100'");
$gridTopic->addConfig("autoHeight", "'TRUE'");

//$gridTopic->addConfig("layout", "{padding:'5'}");

$gridTopic->addColumn('id');
$gridTopic->addColumn('id_lang');
$gridTopic->addColumn(array(dataIndex=>'title',header=>'Search',width => 600));

$gridTopic->addColumn('text');

if($_POST['s']!='' && $_POST['s']!=' '){
	$gridTopic->selectSQL = "SELECT a.node_img,a.id_sup,a.id,b.title AS title,a.topic FROM ".$_POST['table']." AS a, ".$_POST['table']."Lang AS b WHERE (b.title LIKE '%".$_REQUEST['s']."%' OR b.text LIKE '%".$_REQUEST['s']."%') AND a.topic=1 AND a.id = b.id_sup AND b.id_lang = '".$used_lang."' ORDER BY a.topic ASC, a.orderBy";
}else{
	$gridTopic->selectSQL = "SELECT a.node_img,a.id_sup,a.id,b.title AS title,a.topic FROM ".$_POST['table']." AS a, ".$_POST['table']."Lang AS b WHERE (b.title LIKE '%".$_REQUEST['s']."%' OR b.text LIKE '%".$_REQUEST['s']."%') AND a.topic=2 AND a.id = b.id_sup AND b.id_lang = '".$used_lang."' ORDER BY a.topic ASC, a.orderBy";
}

//$gridTopic->setExpander('<p><div><h1>Opis</h1>{text}</div></p><div class="detailData"/>');
//$gridTopic->setExpander('<p><div>{text}</div></p><div class="detailData"/>');

$gridTopic->setPaging(15)
	->setAutoExpand('title')
	->setFiltering(true)
	//->setSearchField('Search')
	;

//echo "<script>";

echo $gridTopic->createStore();
echo $gridTopic->createGrid();

?>

var helpTpl = new Ext.XTemplate('<div style="padding: 0px;"><!-- <h1>Search</h1> --><p id="searchText">Please, type search text into input field and click Search button.</p><br /></div>');
	helpTpl.compile();

var helpSearchPanel = new Ext.Panel({
	id: 'helpSearch',
	title:'Help Search',
	iconCls:'icon-help-tab',
	closable: true,
	//html : 'To je html vsebina', 
	
	autoScroll: true,
		
	layout:{
			type:'vbox',
			padding:'5',
			align:'stretch'
	},
	items:[
	{ xtype:'panel',height:20,id:'helpSearchRes',tpl:helpTpl },
	
	<?php echo $gridTopic->getGridName();?>
			
		]
	,tbar:[{
            xtype:'textfield',
            id: 'testFieldHelpSearch'
        }, '-', {
            text:'Search',
           	iconCls:'iconHelpSearch',			
            handler:function(){
            	
            	$sVarInput = Ext.getCmp("testFieldHelpSearch").el.dom.value;
            	            	
            	$sOutput = 'Search results for "'+$sVarInput+'":';
            	
            	document.getElementById("searchText").innerHTML = $sOutput;
            	            	
            	/*
            	$sVar = 'action=searchTopics&RMtable=<?php echo $_POST["table"];?>&s='+$sVarInput;
            	
            	Ext.Ajax.request({
				   url: 'webhelp/_ajaxManipulators/helpDataManipulator.php',
				   success: function(response, opts) {
						Ext.getCmp('helpSearchRes').el.dom.innerHTML = response.responseText;
						//alert(opts);
						//console.log(opts);
				   },
				   failure: function(response, opts) {
				   				alert('error');
				   			},
				   method: 'GET',
				   params: $sVar
				});
				*/
							
				Ext.getCmp('<?php echo $gridTopic->getGridName();?>').getStore().reload({params:{database:'<?php echo DB_WARPIT_HELP;?>', action : 'select', RMcomp:'grid', RMtable : '<?php echo $_POST['table'];?>', start: 0, limit : 15, SQL : "SELECT a.node_img,a.id_sup,a.id,b.title AS title,a.topic FROM <?php echo $_POST['table'];?> AS a, <?php echo $_POST['table'];?>Lang AS b WHERE (b.title LIKE '%"+$sVarInput+"%' OR b.text LIKE '%"+$sVarInput+"%') AND a.topic=1 AND a.id = b.id_sup AND b.id_lang = '1' ORDER BY a.topic ASC, a.orderBy" }});
					           
				
            }
        }]
	
		
});

Ext.getCmp('west-panel').add(helpSearchPanel).show();

helpTpl.overwrite('helpSearchRes');

<?php echo $gridTopic->getGridName().".on('rowdblclick',function(SearchResultGrid, rowIndex, e){var gridRowVar=SearchResultGrid.getStore().getAt(rowIndex).data; CallScript('webhelp/_forms/forms.helpGrid.php','id='+gridRowVar.id+'&table=".$_POST['table']."&tabTitle='+gridRowVar.title); });"; ?>

<?php 
if($_POST['s']!='' && $_POST['s']!=' '){
?>
	Ext.getCmp("testFieldHelpSearch").el.dom.value = '<?php echo $_POST['s'];?>';
	
	$sVarInput = Ext.getCmp("testFieldHelpSearch").el.dom.value;
            	            	
	$sOutput = 'Search results for "'+$sVarInput+'":';
            
	document.getElementById("searchText").innerHTML = $sOutput;
<?php 
}
?>
