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


	
	$tableName = 'capi___'. $db->get('_CapiTables',$_POST['id_table'],'name',DB_WARPIT_WEBCATI_BASE);
	
		
	$tabId = $_POST['uniqueTabId'];
	//this form can be used to show users from different conditions
	/**
	 * showType = 0 - show users from a selected group (double click on group)
	 * showType = 1 - show all users in _Interviewer table (double click on root in groups tree)
	 * showType = 2 - show users that meet the conditions in a sql query template!
	 */

	$grid = new MakeGrid( 'grid_'.$tabId );
	
	$grid->addSetter("'tdisable','id_municipality','zip','id_region','type_settlement'")
	->setMainTable($tableName,DB_WARPIT_WEBCATI_BASE)
	->setReqUrl("webcati/_ajax/capiManagment/ajax.manageCapiTable.php")
	->dateSQL = 'U';
	
	$grid->dateDisplay = 'd.m.Y H:i:s';
	$grid->addColumn('id','Id')->width(50)->type('int');
	$grid->addColumn('tdisable','Disable')->type('checkColumn')->width(30);
	$grid->addColumn('id_municipality','ID municipallity')->width(50)->type('int');
	$grid->addColumn('municipality','Municipaity')->width(180)->editor('new Ext.form.TextField()');
	$grid->addColumn('id_settlement','ID settlement')->width(50)->type('int');
	$grid->addColumn('settlement','Settlement')->width(180)->editor('new Ext.form.TextField()');
	$grid->addColumn('id_street','ID street')->width(50)->type('int');
	$grid->addColumn('street','Street')->width(180)->editor('new Ext.form.TextField()');
	$grid->addColumn('address','Address')->width(50)->type('int');
	$grid->addColumn('addon_address','Address addon')->width(20)->editor('new Ext.form.TextField()');
	$grid->addColumn('zip','ZIP')->width(50)->type('int');
	$grid->addColumn('town','Town')->width(150)->editor('new Ext.form.TextField()');
	$grid->addColumn('name','Name')->width(180)->editor('new Ext.form.TextField()');
	$grid->addColumn('x_coordinate','X coordinate')->width(50)->type('int');
	$grid->addColumn('y_coordinate','Y coordinate')->width(50)->type('int');
	$grid->addColumn('id_region','ID region')->width(40)->type('int');
	$grid->addColumn('population','Population')->width(70)->type('int');
	$grid->addColumn('type_settlement','Settlement type')->width(30)->type('int');
	$grid->addColumn('id_import','ID import')->width(30)->type('int');
	$grid->addColumn('time_import','Import time')->type('date')->width(200)->renderer("function(v) {
                 if(v.format('U') > 100) return Ext.util.Format.date(v, 'd.m.Y H:i:s');
            }");
	$grid->addColumn('proj_reserved','Reserved project')->width(50)->type('string');
	
	//let's get extra fields
	$extraFields = $db->get('_CapiTablesAddon','id_capiTable = '.$_POST['id_table'].' AND is_leaf = 1','name' );
	if(is_array($extraFields)) {
		foreach ($extraFields as $telField) {
			$grid->addColumn('capi_'.$telField,$telField);
		}
	}
	
	
	$grid->setPaging()
		//->setAutoExpand('email')
		->region('center')
		->setCheckBoxSelectionModel(true)
		->setRemoteSort()
		->setFiltering(true)
		->setSearchField('Search','top');

	echo $grid->createGrid();
	
	
	echo "
	mainTabPanel.add({
	id: '$tabId', 
	layout:'border',
	title: 'Edit field records - $tableName',
	closable: true,
	items:[$grid]

	 })";
	 if(!$_POST['uniqueNotShow'])
		echo  ".show();";
?>