<?php

include_once("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."config.php");

$idField = "id";

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

$cmpName = "labelEditor_" . $tableName;

$ajax = $pluginDir . "/_forms/_ajax/ajax.label.editor.php";
?>
var <?php echo $cmpName; ?> = new Ext.form.FormPanel({
        width:500,
        height:300,
        autoScroll:true,
        layout: 'fit', 
        closable: 'true',
        title: 'Answer editor',
        items:[new Ext.grid.EditorGridPanel({
                height: 200,
                id: 'grid_<?php echo $tableName; ?>',
                autoScroll: true,
                clicksToEdit: 1,
                border: 0,
                listeners: {
                    afteredit: function(e)
                    {
                        var jsonData = Ext.encode(Ext.pluck(Ext.getCmp('grid_<?php echo $tableName; ?>').getStore().data.items, 'data'));
                        CallScript('<?php echo $ajax; ?>', {
                            action: 'save',
                            data: jsonData,
                            tableName: '<?php echo $tableName; ?>',
                            questionId: <?php echo $questionId; ?>,
                            projectId: <?php echo $projectId; ?>
                        }, function(){
                            Ext.getCmp('grid_<?php echo $tableName; ?>').getStore().reload();
                        });
                    }
                },
                store: new Ext.data.JsonStore({
                        url : '<?php echo $ajax; ?>',
                        defaultSortable: false,
                        sortable:false,
                        root : 'rows',
                        autoLoad: true,
                        baseParams: {
                            action: 'get',
                            tableName: '<?php echo $tableName; ?>',
                            questionId: <?php echo $questionId; ?>,
                            projectId: <?php echo $projectId; ?>
                            },
                        fields:[
                                 {name: 'value', type:'int'},
                                 {name: 'answer', type:'string'},
                                 {name: 'color', type:'string'},
                                 {name: 'position', type:'string'}
                        ]}),
                    colModel: new Ext.grid.ColumnModel({
                        sortable:false,
                        defaultSortable: false,
                        defaults: {
                            width: 120,
                            defaultSortable: false,
                            sortable: false
                    },
                    columns: [
                        {
                            header: 'Value',
                            dataIndex: 'value',
                            editor:{
                                blank: false
                            }
                        },
                        {
                            header: 'Answer', 
                            dataIndex: 'answer',
                            flex: '1',
                            editor:{
                                blank: false
                            }
                        },
                        {
                            header: 'Position', 
                            dataIndex: 'position',
                            editor: new Ext.form.ComboBox({
                                   allowBlank     : false,
                                   store: [
                                        ['left', 'left'],
                                        ['right', 'right']
                                    ],
                                   typeAhead    : true,
                                    mode        : 'local',
                                    triggerAction    : 'all',
                                    selectOnFocus    :true
                           })
                        },
                        {
                            header: 'Color', 
                            dataIndex: 'color',
                            editor:{
                              blank: false
                            }
                        }
                        
                        
                    ],
                    }),
                    sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
                    frame: true,
                })]
    }).show();
    
Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').add(<?php echo $cmpName; ?>);
Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').setActiveTab(<?php echo $cmpName; ?>);