<?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'];
	$projectId = $_POST['id_project'];
	$projName = $_POST['projName'];
	$tableName = $projName.'___telLog';

	//let's check if there is any telLog table
	if(!$db->TableExists($projName.'___telLog',DB_WARPIT_WEBCATI_BASE))
	{
		echo "alert('Telephone log table $tableName is missing for project $projName!')
		removeSession('$tabId')";
		exit;
	}

	// if I have custom telephone fields, I need ID of telephone table
	$id_telname = $db->get('_Projects',$projectId,'id_telname',DB_WARPIT_WEBCATI_BASE);
	$telTable = 'tel___'.$db->get('_TelephoneTables',$id_telname,'name',DB_WARPIT_WEBCATI_BASE);

	$grid = new MakeGrid( 'grid_'.$tabId );

	$grid->setMainTable($tableName,DB_WARPIT_WEBCATI_BASE)->setTitle('Telephone numbers log');
	$grid->selectSQL = "SELECT a.*,b.name,b.address,b.zip,b.town FROM $tableName a LEFT JOIN $telTable b ON a.id_tel = b.id";

	$grid->setReqUrl("webcati/_ajax/telNumberManagment/ajax.telNumbersLog.php")
	->dateSQL = 'U';

	$grid->dateDisplay = 'Y.m.d H:i:s';
	$grid->addColumn('id','ID')->width(50)->tableAs('a');
	$grid->addColumn('id_tel','Id. tel.')->width(80)->type('int');
	$grid->addColumn('id_telname','ID tel. table')->width(70)->type('int');

	$grid->addColumn('id_structure','ID structure')->width(80)->type('int');
	$grid->addColumn('id_user','ID interviewer')->width(80)->type('int');
	$grid->addColumn('user_type','Interviewer type')->width(100)->type('int');
	$grid->addColumn('oms','Area code')->width(70)->tableAs('a');
	$grid->addColumn('telephone','Telephone number')->width(120)->tableAs('a');
	$grid->addColumn('status','Status')->width(70)->type('int')->tableAs('a');
	$grid->addColumn('status_rec','Status rec')->width(70)->type('int')->tableAs('a');
	$grid->addColumn('name','Name')->width(120)->tableAs('b');
	$grid->addColumn('address','Address')->width(120)->tableAs('b');
	$grid->addColumn('zip','Zip')->width(50)->type('int')->tableAs('b');
	$grid->addColumn('town','Town')->width(120)->tableAs('b');
	$grid->addColumn('call_time','Call time')->width(120)->type('date')->renderer("function(v) {
                 													if(v.format('U') > 100) return Ext.util.Format.date(v, 'Y.m.d H:i:s');
            													}");
	$grid->addColumn('end_time','End time')->width(120)->type('date')->renderer("function(v) {
                 													if(v.format('U') > 100) return Ext.util.Format.date(v, 'Y.m.d H:i:s');
            													}");
	$grid->addColumn('app_koment','Comment')->width(250);
	//$grid->setAutoExpand('app_koment');

	$grid->setPaging()
		->region('center')
		->setCheckBoxSelectionModel(true)
		->setRemoteSort()
		->setFiltering(true)
		->setSearchField('Search','top')
		->addtBarItem("'->'")
		->addExportButton();

	echo $grid->createGrid();


	echo "
		mainTabPanel.add({
		id: '$tabId',
		iconCls:'RMicon_tel_numbers_log',
		layout:'border',
		title: 'Telephone number log - $tableName',
		closable: true,
		items:[$grid]
	 })";

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


?>
