<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");


$tabId = $_POST['uniqueTabId'];
$ajax = "webuserspool/_forms/recode/_ajax/ajax.recode.php";
?>

  var WUP_recode_visible = new Ext.grid.CheckColumn({ header: 'Include', dataIndex: 'visible', width: 50 }); 
 
    // create the Grid
    var <?php echo $tabId; ?> = new Ext.grid.EditorGridPanel({
        
    	store: new Ext.data.Store({
			url: '<?php echo $ajax;?>',
			remoteGroup:true,
			reader: new Ext.data.JsonReader({		
				root:'rows',
				totalProperty : "results",
				idProperty:"id"
			},
				new Ext.data.Record.create(
					[
					 { name:"id", type: 'int' },
					 { name:"recode_condition", type: 'string' },
					 { name:"recode_variable", type: 'string' },
					 { name:"recode_value", type: 'string' },
					 { name:"visible", type: 'bool' },
					  { name:"orderBy", type: 'int' }
					]
				)

			)
    	}),
        id: '<?php echo $tabId; ?>',
        border: false,
        title: 'Recoder',
        closable: true,
  		sm: new Ext.grid.RowSelectionModel({
                			singleSelect: true
        }),
  		
    	plugins: [WUP_recode_visible],
      	columns: [
      		WUP_recode_visible,
      		{header: 'Condition',  width:200, sortable: true, dataIndex: 'recode_condition', editor:  new Ext.form.TextField()},
            {header: 'Variable', width:150,  sortable: true, dataIndex: 'recode_variable', editor:   new Ext.form.TextField()} ,
      		{header: 'Variable value',  sortable: true, dataIndex: 'recode_value', editor:  new Ext.form.TextField()},
      		{header: 'Order',  sortable: true, dataIndex: 'orderBy', editor:  new Ext.form.TextField()}    
        ],
        
        listeners: {
       		
       		afteredit : function(e) {
       		
       			Ext.Ajax.request({
       				url:'<?php echo $ajax;?>',
       				params: { action: 'update' ,
       								
       							  id: e.record.data.id,
							   field: e.field,
								value : e.value
								
       				},
					success : function(resp,opt){ 
						e.record.commit();  
					}							  							
				})
			}
			
		},
		 tbar: [{
        		
        			 text: 'Add Condition',
		           	 handler: function() {
		           	 	CallScript('<?php echo $ajax;?>',{ action: 'add', gridID: '<?php echo $tabId; ?>' }); 	
		             }
        		
        		},{
        		
        			 text: 'Delete Condition',
		           	 handler: function() {
		           	 	rec = <?php echo $tabId; ?>.getSelectionModel().getSelected();
		           	 	Ext.Msg.show({
								   title:'Delete',
								   msg: 'Are ypu sure',
								   buttons: Ext.Msg.YESNO,
								   fn: function(selBtn) { 
								   		if (selBtn == 'yes') {
								   			CallScript('<?php echo $ajax;?>',{ action: 'delete', id: rec.get('id'),gridID: '<?php echo $tabId; ?>' }); 			
								   		}
								   },
								   animEl: 'elId',
								   icon: Ext.MessageBox.QUESTION
								});		  	
		             }
        		
        		},{
        		
        			 text: 'Make Recode',
		           	 handler: function() {
		           	 	CallScript('<?php echo $ajax;?>',{ action: 'make-recode', gridID: '<?php echo $tabId; ?>' }); 	
		             }
        		
        		}
        		
        		]
});
<?php echo $tabId; ?> .getStore().load();
	
<?php	
echo "mainTabPanel.add( Ext.getCmp('".$tabId."')).show();"; 

?>