<?php
$userInputTable = 'user_input_labels';

$user_id  = $_SESSION['UserInfo']['id'];
	//we get the structure of the interviewer table
	if($selFields)
	{
		$where = "AND a.id IN ($selFields)";
	}
	$sql = "SELECT b.field_id,a.id,a.field_name,a.field_text,a.field_type,a.data_type,b.is_hidden,a.field_defaultValue  FROM _InterviewerFieldOrder b LEFT JOIN _InterviewerStructure a   ON a.id = b.field_id  WHERE user_id = $user_id $where ORDER BY b.orderBy";

	$db->ChangeDatabase(DB_WARPIT_USERSPOOL);

	if(!$rs = $db->SQLexecute($sql))
	{
		echo 	"alert(\"".$db->GetError()."\");";
		exit;
	}
	if($db->NumRows($rs) == 0 )
	{
		 $grid->addColumn('email')->header('Email')->editor('new Ext.form.TextField()')
		 ->setProperty('maxWidth','100')
		 ;
		$sql = "SELECT b.field_id,a.id,a.field_name,a.field_text,a.field_type,a.data_type,b.is_hidden,a.field_defaultValue  FROM _InterviewerFieldOrder b LEFT JOIN _InterviewerStructure a   ON a.id = b.field_id  WHERE user_id = 0 ORDER BY b.orderBy";
		if(!$rs = $db->SQLexecute($sql))
		{
			echo 	"alert(\"".$db->GetError()."\");";
			exit;
		}
	}
	if($specCol)
	{
		foreach ($specCol as $di => $hd)
		{
			$grid->addColumn($di,$hd);
		}
	}

	//20151210 Tanis added extra field for status
	$definedStatusCombo = "definedStatusesCombo_" . $tabId;
	echo "var " .  $definedStatusCombo . " = new Ext.form.ComboBox({
			typeAhead: true,
			triggerAction: 'all',
			lazyRender:true,
			mode: 'local',
			store: new Ext.data.ArrayStore({
					id: 0,
					fields: [
							'myId',
							'displayText'
					],
					data: [['1', 'Active'], ['9', 'Removal request'], ['8', 'Wrong address']]
			}),
			valueField: 'myId',
			displayField: 'displayText'
		});";

		$grid->addColumn('definedStatus', "Interviwer status")
		->editor($definedStatusCombo)
		->width(100)
		->renderer("function(val,mt,rec,ind)
		{
				switch(val){
					case '1': return 'Active';
					case '8': return 'Wrong address';
					case '9':	return 'Removal request';
					default : return 'Active';
				}
		}");
		//20151210 Tanis END

	while ($row = $db->fetchAssoc($rs))
	{
		if($row['field_id'] == 0)
		{
			$grRow = $grid->addColumn('email')->header('Email')->editor('new Ext.form.TextField()')->width(200)
		 ->setProperty('maxWidth','100')
		 ;
		}
		else
		{
			$grRow = $grid->addColumn($row['field_name'])->header($row['field_text']);
			if($notEditable)//sometimes we want the editor for filter options, but we don't want editable fields!
				$grRow->setProperty('editable','false');
			if($addEditor)//addEditor is set before grid.showgroup.php is included!
			switch ($row['field_type']) {
				case 5:
					$grRow->type('date')->editor('new Ext.form.DateField()');
				break;
				case 2:
					//checkbox - if more then 1 values are in userInputTable we set up a checkCombo, otherwise a checkColumn
					$sql = "SELECT * from $userInputTable WHERE lab_field = ".$row['id'];
					if(!$rsCB = $db->SQLexecute($sql))
						{
							echo 	"alert(\"".$db->GetError()."\");";
							exit;
						}
					if($db->NumRows($rsCB) > 1 )
					{

						$grRow->type('checkCombo')->comboTable($userInputTable)
						->comboSQL('SELECT lab_code as id,lab_label FROM '.$userInputTable.' WHERE lab_field='.$row['id'].' ORDER BY lab_position')
						->comboValue('lab_label')->comboDisplay('lab_label')
						->comboListeners("select:function(fld,rec,ind){ //alert('tralalaaaaa');
								$grid.RMconn.request({params:{
									action:'update', field: '".$row['field_name']."_'+rec.id,
									id:fld.gridEditor.record.id,value:rec.data.checked ? rec.id : 0
								} });
						} ")
						;
					}
					else
					{
						$grRow->type('checkColumn');
					}
				break;
				case 3:
				case 4:
					$grRow->type('combo')->comboTable($userInputTable)
					->comboSQL('SELECT *,lab_code as id FROM '.$userInputTable.' WHERE lab_field='.$row['id'].' ORDER BY lab_position')
					->comboValue('lab_code')->comboDisplay('lab_label');
				break;

				default:
					$grRow->editor('new Ext.form.TextField()')->defaultValue($row['field_defaultValue']);
				break;
			}

		}
		if($row['is_hidden'])
			{
				$grRow->hidden(true);
			}
	}
?>
