<?php

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_tree.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_form.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_grid.php");
include_once(PATH_TO_ROOT."config.php");


$id_project = $_POST['id_project'];


$tabId = $_POST['uniqueTabId'];

$tableName = $db->get("_Projects",$id_project,'proj_name',DB_WARPIT_WEBCATI_BASE);

//the grid to show all the fields
$fieldGrid = new MakeGrid($tabId.'_fieldGrid');

	//project tree to be shown inside window for adding!!!
		
	$fieldGrid->setMainTable('_DesignerManager',DB_WARPIT_WEBCATI_BASE)
	//->setReqUrl('webcati/_ajax/designerPanel/ajax.designerTemplates.php')
	->setTitle('Template settings')
	->setMainTable($tableName)
	->setReqUrl("webcati/_ajax/buildQuestionare/ajax.buildQuestionnaire.php")
	//we will create or own afteredit listener, so that we can update the column structure
	->setEditable(false)
	->setCheckBoxSelectionModel()
	->region('center')
	->addConfig('assignFunction',"function(id_special){
			var foundNode = false;
			var cm = Ext.getCmp('CatiMenu');
			if(cm && cm.selModel && cm.selModel.getSelectedNode() )
			{
				var n = cm.selModel.getSelectedNode();
				if(n.id.substr(0,6) == 'design')
				{
				  var p = {
				  	action:'addDesign',
				  	template_id: n.id.substr(7)
				  
				  };
				  foundNode = true;
				  var sm = $fieldGrid.selModel;
				  if(sm.hasSelection() || id_special == 9 )
						{
							if(id_special != 9)
							{
								var ids = [];
								Ext.each(sm.getSelections(),function(item){
									ids.push(item.id);
								});
								p.selections = true;
								p.ids = ids.join();
								p.templateType=id_special;
							}
							else 
								p.isProject = true;
							
							p.forDevice = $fieldGrid.topToolbar.deviceCombo.getValue();
							$fieldGrid.RMconn.request({params:p,
							success:function(){
									$fieldGrid.store.reload();
								}
							});
						}
				}
			} 
			if(!foundNode)
				Ext.Msg.alert('Warning!','Please slect design template from webcati menu!');
		}")
	->addConfig('removeFunction',"function(id_special)
		{
			 var sm = $fieldGrid.selModel;
			 
			 	if(id_special == 9)
			 	{
			 		$fieldGrid.RMconn.request({params:{
								action:'removeDesign',
								isProject: true,
								forDevice: $fieldGrid.getTopToolbar().deviceCombo.getValue()
							},
							success:function(){
									$fieldGrid.store.reload();
								}
							});
			 	}
			 
				  if(sm.hasSelection() )
						{
							var ids = [];
							Ext.each(sm.getSelections(),function(item){
								ids.push(item.id);
							});
							
							$fieldGrid.RMconn.request({params:{
								action:'removeDesign',
								templateType:id_special,
								ids: ids.join(),
								forDevice: $fieldGrid.getTopToolbar().deviceCombo.getValue()
							},
							success:function(){
									$fieldGrid.store.reload();
								}
							});
						}
		}")
	->addTbarItem("{
		text: 'Assign template',
		menu:[
			{text:'Project',handler: function(){ $fieldGrid.assignFunction(9) } },
			{text:'Default',handler: function(){ $fieldGrid.assignFunction(0) } },
			{text:'Answer',handler: function(){ $fieldGrid.assignFunction(6) } },
			{text:'Control',handler: function(){ $fieldGrid.assignFunction(7) } }
		]
	},
	{
		text: 'Remove templates',
		menu:[
			{text:'Project',handler: function(){ $fieldGrid.removeFunction(9) } },
			{text:'Default',handler: function(){ $fieldGrid.removeFunction(0) } },
			{text:'Answer',handler: function(){ $fieldGrid.removeFunction(6) } },
			{text:'Control',handler: function(){ $fieldGrid.removeFunction(7) } }
		] 
	},
	{
		xtype:'label',
		encode:false,
		listeners: {
			render:function(){
				this.setText('Legend: <span style=\"color:red\">System</span> - <span style=\"color:green\">Pos/Type</span> - <span style=\"color:blue\">Project</span> -<span style=\"color:black\">Question</span> ',false);
			}
		}
	},
	{
		xtype: 'combo',
		store: [[0,'Desktop'],[2,'Mobile']],
		triggerAction:'all',
		editable:false,
		value:0,
		ref:'deviceCombo'
	}
	")
	->listeners[]="afteredit:function(e){
				/*var p = {};
				p.field = e.field;
				p.action = 'update';
				p.tableId = e.grid.selectedId;
				p.id = e.record.id;
				p.value = e.value;
				if(e.field == 'name')
				{
					p.oldName = e.originalValue;
					p.dataType = e.record.data.type
				}
				else
				{
					p.fieldName = e.record.data.name;
					p.oldName = p.fieldName;
				}
				$fieldGrid.RMconn.request({
					params: p,
					success : function(resp,opt){if(Ext.decode(resp.responseText).success){ e.record.commit(); } else {e.record.reject();
						Ext.Msg.alert('Warning!',Ext.decode(resp.responseText).error);
					}},
					failure: function(){e.record.reject();}
				});*/
			}"
	;
	$fieldGrid->baseParams = "styleManager:true,id_project:$id_project,forDevice: 0 ";
	//$fieldGrid->selectSQL = "SELECT a.id,b.id AS id_order,a.field_name,a.data_type,field_text,field_type,is_hidden FROM _InterviewerStructure as a INNER JOIN _InterviewerFieldOrder as b ON a.id = b.field_id AND b.user_id = 0";
	
	$fieldGrid->addColumn('id');
	$fieldGrid->addColumn('tempFrom');
	$fieldGrid->addColumn('tempFromAns');
	$fieldGrid->addColumn('q_name','Question');
	$fieldGrid->addColumn('id_template','Template ID');
	$fieldGrid->addColumn('name','Template')->renderer("function(val,md,rec){
		var zla;
		if(rec.data.tempFrom == 'system')
			zla = '<span style=\"color:red;\">';
		else if(rec.data.tempFrom == 'project')
			zla = '<span style=\"color:blue;\">';
		else if(rec.data.tempFrom == 'question')
			zla = '<span style=\"color:black;\">';
		else
			zla = '<span style=\"color:green;\">';
		zla += val+'</span>';
		return zla;
	}");
	$fieldGrid->addColumn('id_ansTemp','Answer Template ID');
	$fieldGrid->addColumn('name_ansTemp','Template')->renderer("function(val,md,rec){
		var zla;
		if(rec.data.tempFromAns == 'system')
			zla = '<span style=\"color:red;\">';
		else if(rec.data.tempFromAns == 'project')
			zla = '<span style=\"color:blue;\">';
		else if(rec.data.tempFromAns == 'question')
			zla = '<span style=\"color:black;\">';
		else
			zla = '<span style=\"color:green;\">';
		zla += val+'</span>';
		return zla;
	}");
	

echo $fieldGrid->createGrid();
/*echo $form->createForm();
echo $grid->createGrid();
echo $propForm->createForm();
*/


?>
	var <?php echo $tabId; ?> = new Ext.Panel({
		id:'<?php echo $tabId; ?>',
		title:'Style manager - <?php echo  $tableName;  ?> ',
		defaults:{split:true,border:false},
		iconCls:'RMicon_layout_doc',
		layout:'border',
		border: false,
		closable:true,
		items:[<?php echo  $fieldGrid;  ?>   ]
	});
	mainTabPanel.add(<?php echo $tabId; ?>).show();