<?php

include_once("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."config.php");
include_once(PATH_TO_ROOT . "_class/createQuestion/Question.php");

$idField = "id";

$tableName = $_POST['tableName'];
$questionId = $_POST['questionId'];
$pluginDir = $_POST['pluginDir'];
$projectId = $_POST['projectId'];

$ajax = $pluginDir . "/_forms/_ajax/ajax.fields.editor.php";

$gridName = "fieldseditor" . $tableName;


$data  = json_decode(stripslashes($_POST['data']), TRUE);

//print_r($propertysNameArray);  
$data = Question::answerToArray($data);

foreach($data as $val)
{
    $dat[] = "['" . $val[0] . "','" . $val[1] . "']";  
}

//var_dump($data);


?>

var <?php echo $gridName;?> = new Ext.grid.EditorGridPanel({
                height: 200,
                id: '<?php echo $gridName; ?>',
                autoScroll: true,
                align: 'right',
                width: 200,
                clicksToEdit: 2,
                closable: true,
                title:'Fields editor',
                border: 0,
                store: new Ext.data.ArrayStore({
                    fields: [
                        {name: 'value'},
                        {name: 'label'}
                    ],
                    data:[
                        <?php echo implode(',', $dat); ?>
                    ]  
                }),
                tbar: [{
                    text: 'Add',
                    iconCls: 'icon-add-record',
                    handler: function()
                    {
                        var store = <?php echo $gridName; ?>.getStore();
                        var rType = store.recordType;
                         var p = new rType({
                                    value: '0',
                                    label: 'New label'
                                });
                                <?php echo $gridName; ?>.stopEditing();
                                store.insert(0, p);
                                <?php echo $gridName; ?>.startEditing(0, 0);
                    }
                },{
                    text: 'Remove',
                    iconCls: 'icon-delete-record',
                    handler: function()
                    {
                        var store = <?php echo $gridName; ?>.getStore();
                        
                        var rec = <?php echo $gridName; ?>.getSelectionModel().getSelected();
                        
                        Ext.Msg.show({ 
                            title:"Confirm delete",
                            buttons: Ext.MessageBox.YESNO,
                            msg:'Are you sure?',
                            fn: function(btn)
                            {
                                if(btn == 'yes')
                                {
                                  Ext.each(rec, function(r)
                                    {
                                        store.remove(r);      
                                    });  
                                }
                            }
                            });
                        
                        
                    }
                }],
                bbar:['->',
                {
                   text:'Save',
                   iconCls: 'icon_save_document',
                   handler: function()
                   {
                       
                       var data = Ext.pluck(<?php echo $gridName; ?>.getStore().data.items, 'data');
                       CallScript('<?php echo $ajax; ?>', {
                          action: 'update',
                          projectId: '<?php echo $projectId; ?>',
                          questionId: '<?php echo $questionId; ?>',
                          data: Ext.encode(data) 
                       });
                       Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').remove(<?php echo $gridName; ?>);
                       Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').setActiveTab(Ext.getCmp('settingsGrid_<?php echo $tableName;?>'));   
                   } 
                }],
               cm: new Ext.grid.ColumnModel({
                    columns: [{
                        header: 'Value',
                        dataIndex: 'value',
                        width: 50,
                        editor:{
                            blank: false
                        }
                        
                    },{
                        header: 'Label',
                        dataIndex: 'label',
                        //width: 175,
                        flex: 1,
                        editor:{
                            blank: false
                        }
                    }
                    ]}
                ),
                    sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
                    frame: true,
                });

Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').add(<?php echo $gridName; ?>);
Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').setActiveTab(<?php echo $gridName; ?>);