<?php

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

$tabId = 'wupWindowSelectFields';
$id_project = $_POST['id_project'];
$reqUrl = 'webuserspool/_ajax/ajax.projectUsers.php';

echo "

function toggleCheck(node,isCheck)
{
     if(node)
     {
        var args=[isCheck];
        node.cascade(function(){
          if(this.ui.node.leaf)
          {
            c=args[0];
            this.ui.toggleCheck(c);
            this.attributes.checked=c;
          }

        },null,args);
     }
}
";


$interFields = new MakeTree($tabId.'_inter');
$interFields->setMainTable('_InterviewerStructure',DB_WARPIT_USERSPOOL)
	->setReqUrl($reqUrl)
	->setTitle('Interviewer fields')
	->flex(1)
	->baseParams = 'id_project:'.$id_project;
$interFields->addtBarItem("
{
    text: 'Select all',
    handler: function()
    {
            var loader = $interFields.getLoader();
            loader.addListener('load', function()
            {
                    toggleCheck($interFields.getRootNode(), true);
            });
           $interFields.getRootNode().expand(true, true);
           toggleCheck($interFields.getRootNode(), true);
    }
}
");
$interFields->addtBarItem("
{
    text: 'Deselect all',
    handler: function()
    {
            var loader = $interFields.getLoader();
            loader.addListener('load', function()
            {
                    toggleCheck($interFields.getRootNode(), false);
            });
           $interFields.getRootNode().expand(true);
           toggleCheck($interFields.getRootNode(), false);
    }
}
");
echo $interFields->CreateTree();

$projFields = new MakeTree($tabId.'_proj');
$projFields->setMainTable($_POST['projName'],DB_WARPIT_WEBCATI)
->setReqUrl($reqUrl)
->setTitle('Structure fields')
->flex(1)
->baseParams = 'id_project:'.$id_project;

$projFields->addtBarItem("
{
    text: 'Select all',
    handler: function()
    {
            var loader = $projFields.getLoader();
            loader.addListener('load', function()
            {
                    toggleCheck($projFields.getRootNode(), true);
            });
           $projFields.getRootNode().expand(true, true);
           toggleCheck($projFields.getRootNode(), true);
    }
}
");
$projFields->addtBarItem("
{
    text: 'Deselect all',
    handler: function()
    {
            var loader = $projFields.getLoader();
            loader.addListener('load', function()
            {
                    toggleCheck($projFields.getRootNode(), false);
            });
           $projFields.getRootNode().expand(true);
           toggleCheck($projFields.getRootNode(), false);
    }
}
");

echo $projFields->CreateTree();
?>

var win1 = new Ext.Window({
        modal:true,
 		title: 'File Upload Form',
        width:400,
        height:500,
        layout:'hbox',
        layoutConfig:{ align:'stretch' },
        closeAction:'close',
        plain: true,
		items: [ <?php echo "$interFields,$projFields";?> ],
		buttons:[{
			text:'Save',
			handler:function(){
			<?php
				echo "$interFields.RMconn.request({params:{
					action:'saveFields',
					id_project:$id_project,
					interFields: $interFields.getChecked('id').join(),
					questFields : $projFields.getChecked('text').join()
				},
				success:function(resp)
				{
					var r = Ext.decode(resp.responseText);
					var grid = Ext.getCmp('{$_POST['compId']}');
				    grid.filters.clearFilters();
					Ext.each(r.columns, function(col){
					    if(col.type != 1)
					    {
					       col.sortable = true;
                           grid.gridFilter.addFilter({dataIndex: col.dataIndex, type:'string'});
					    }

					});
					r.columns.unshift(grid.selModel);

                    //set width of email field
				    r.columns[2].width = 150;

				    // setting for t_start and t_stop
					r.columns[4].renderer = r.columns[5].renderer = function(val){

                    	if(val)
                    	{
                    	    if(val instanceof Date)
                    	       return Ext.util.Format.date(val, 'd.m.Y h:i:s');
                    	    else
                            	return Date.parseDate(val,'U').format('d.m.Y H:i:s');
                    	}
                    	 //
                    };
                    r.columns[4].type = r.columns[5].type = 'date'
                    r.columns[4].width = r.columns[5].width = 120;
                    //settings for t_diffs
                    r.columns[6].renderer = function(val){
                    	if(val)
                    	   return Date.parseDate(val-3600,'U').format('H:i:s');
                    };
                    r.columns[6].width = 120;
					grid.colModel.setConfig(r.columns);
					var reader = grid.getStore().reader;
					reader.meta.fields = r.fields;
					reader.recordType = Ext.data.Record.create(r.fields);
					delete reader.ef;
					reader.buildExtractors();
					grid.getStore().reload();

				}
				});";
			?>
			win1.close();
			}
			}
		]

    }).show();
