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

	$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
	
	// sample table
	if (isset($_POST['projName'])) {
		$projectName = $_POST['projName'];
	} elseif (isset($_POST['id_proj'])) {
		$projectName = $db->get('_Projects',$_POST['id_proj'],'proj_name',DB_WARPIT_WEBCATI_BASE);
	}

	if (isset($_POST['id_capiTable'])) {
		$capiTable = $_POST['capiTable'];
		$id_capiTable = $_POST['id_capiTable'];
	} else {
		$id_capiTable = $db->get('_Projects',$_POST['id_project'],'id_capiTable',DB_WARPIT_WEBCATI_BASE);
		$capiTable = 'capi___'. $db->get('_CapiTables',$id_capiTable,'name',DB_WARPIT_WEBCATI_BASE);
	}
	
	$capiSampleName = $projectName."___sample";
	$tabId = $_POST['uniqueTabId'];
	
	// I need also sample table...
	//let's check if there is any ___sample table
	if(!$db->TableExists($capiSampleName,DB_WARPIT_WEBCATI_BASE))
	{
		echo "alert('No CAPI sample exist for project $projectName')
		removeSession('$tabId')";
		exit;
	}

	$grid = new MakeGrid( 'grid_'.$tabId );
	
	$grid->setMainTable($capiSampleName,DB_WARPIT_WEBCATI_BASE);
	$grid->setTitle('Sample');
	$grid->setReqUrl("webcati/_ajax/capiManagment/ajax.manageCapiTable.php");
	$grid->dateSQL = 'U';		
	$grid->dateDisplay = 'Y.m.d H:i:s';
		
	$grid->addColumn('id','ID sample')->width(50)->tableAs('a');
	$grid->addColumn('tdisable','Disabled')->width(50)->tableAs('a')->type('int');
	$grid->addColumn('id_sample','ID field rec.')->width(50)->type('int');
	$grid->addColumn('id_sampleTable','ID field table')->width(50)->type('int');
	$grid->addColumn('user_id','ID user',"new Ext.form.NumberField()")->width(50)->type('int');
	$grid->addColumn('user_type','User type',"new Ext.form.NumberField()")->width(50)->type('int');
	$grid->addColumn('street','Address')->width(200);
	$grid->addColumn('address','Adress No.')->width(50)->type('int');
	$grid->addColumn('addon_address','Add No.')->width(50);
	$grid->addColumn('zip','ZIP')->width(50);
	$grid->addColumn('town','Town')->width(150);
	$grid->addColumn('name','Name')->width(150);
	$grid->addColumn('planned_visit_time','Planned visit')->width(200)->type('date')->editor('new Ext.ux.form.DateTime()');
	$grid->addColumn('num_interviews','TODO interviews')->width(50)->type('int');
	$grid->addColumn('id_municipality','ID munic.')->width(100)->type('int');
	$grid->addColumn('municipality','Municipality')->width(150);
	$grid->addColumn('id_settlement','ID settl.')->width(100)->type('int');
	$grid->addColumn('settlement','Settlement')->width(150);
	$grid->addColumn('id_street','ID street')->width(100)->type('int');
	$grid->addColumn('x_coordinate','X gps')->width(100)->type('int');
	$grid->addColumn('y_coordinate','Y gps')->width(100)->type('int');
	$grid->addColumn('id_region','ID region')->width(50);
	$grid->addColumn('population','Population')->width(50)->type('int');
	$grid->addColumn('type_settlement','Sett. type')->width(50)->type('int');
	$grid->addColumn('id_import','ID import')->width(50)->type('int');
	$grid->addColumn('time_import','Import time')->width(150)->type('date');
	$grid->addColumn('proj_reserved','Reserved for')->width(100)->type('string');		 
	
	$grid->setPaging()
		->setFiltering()
		->region('center')
		->setSearchField('Search','top')
		->baseParams = "projectName:'".$projectName."',capiSQL:'custom',capiTable:'".$capiTable."',id_capiTable:'".$id_capiTable."'";
	
	echo $grid->createGrid();
/*	
	$grid->addSetter("'tdisable','region','status','status_rec','call_time','call_num'");
	$grid->addColumn('tdisable','Dis')->type('checkColumn')->width(30)->tableAs('a');
	$grid->addColumn('oms','oms',"new Ext.form.NumberField()")->tableAs('a')->width(30);
	$grid->addColumn('telephone','Telephone',"new Ext.form.TextField()")->tableAs('b')->width(60);
	$grid->addColumn('region','Region',"new Ext.form.TextField()")->type('int')->tableAs('a')->width(50);
	$grid->addColumn('town','Town');
	$grid->addColumn('zip','ZIP',"new Ext.form.TextField()")->width(50)->tableAs('a');
	$grid->addColumn('name','Name')->width(200);
	$grid->addColumn('address','Adress')->width(200);
	$grid->addColumn('id_import',"Import seq.")->type('int')->width(80);
	$grid->addColumn('status',"Status","new Ext.form.NumberField()")->type('int')->tableAs('a');
	$grid->addColumn('status_rec',"Status rec","new Ext.form.NumberField()")->type('int')->tableAs('a');
	$grid->addColumn('last_call',"Last Call")->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('call_num',"Num. of calls")->type('int')->width(80);
	$grid->addColumn('call_time',"Call 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');
            }")->editor('new Ext.ux.form.DateTime()');
	$grid->addColumn('status_comment','Status comment')->width(200)->editor('new Ext.form.TextField()');
	$grid->addColumn('app_koment','Appointment comment')->width(200)->editor('new Ext.form.TextField()');
*/

	echo "
		mainTabPanel.add(
			{
				id: '$tabId'
				,layout:'border'
				,title:'Preview CAPI sample - $projectName'
				,iconCls:'RMicon_preview_cati'
				,closable: true
				,items:[$grid]
	 		}
	 	)";
	if (!$_POST['uniqueNotShow']) echo  ".show();";
	 
?>