<?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'];

$gridName = "settingsGrid_" . $tableName;

$ajax = $pluginDir . "/_forms/_ajax/ajax.settings.php";

//print_r($propertysNameArray);  

?>
var <?php echo $gridName;?> = new Ext.grid.EditorGridPanel({
                height: 200,
                id: '<?php echo $gridName; ?>',
                autoScroll: true,
                align: 'right',
                width: 200,
                clicksToEdit: 2,
                title:'Basic settings',
                border: 0,
                plugins:[
                    new Ext.ux.grid.Search({
                        searchText:'Search',  
                        minChars:3,
                        position:'top',
                        disableIndexes: ['option', 'type', 'value'],
                        paramNames:{
                            fields:'srcfields', 
                            query:'srcquery'
                        },
                        ref: 'searchField'
                    })
            ],
            tbar:[{
               iconCls:'icon-help-tab',
               handler: function()
               {
                    CallScript('webhelp/_forms/forms.helpGrid.php',{
                        id:  "206_1410337056_1",
                        tabTitle: "Slider plugin",
                        table: "_HelpMenu",
                    });                
               }
            }, '->'
            ],
          
                listeners: {
                    afteredit: function(e)
                    {
                        var jsonData = Ext.encode(Ext.pluck(<?php echo $gridName; ?>.getStore().data.items, 'data'));
                        CallScript('<?php echo $ajax; ?>', {
                            action: 'Save',
                            data: jsonData,
                            projectId: <?php echo $projectId; ?>,
                            questionId: <?php echo $questionId; ?>
                        }, function(){
                            e.record.commit();
                        });
                    },
                    beforeedit: function(e)
                    {
                        if(e.record.data.option == 'extreme_font' || e.record.data.option == 'scale_font' || e.record.data.option == 'slider_font' )
                        {
                            <?php echo $gridName; ?>.getColumnModel().getColumnAt(1).setEditor(new Ext.form.ComboBox({
                                   allowBlank     : false,
                                   store: [
                                        ['Verdana', 'Verdana'],
                                        ['Arial', 'Arial'],
                                        ['Times New Roman', 'Times New Roman']
                                    ],
                                   typeAhead    : true,
                                    mode        : 'local',
                                    triggerAction    : 'all',
                                    selectOnFocus    :true
                           }));
                        }
                        else if(e.record.data.type == 'bool')
                        {
                            <?php echo $gridName; ?>.getColumnModel().getColumnAt(1).setEditor(new Ext.form.ComboBox({
                               allowBlank     : false,
                               store: [
                                    ['1','true'],
                                    ['0','false']
                                ],
                                renderer : function(a) {
                                    if (a == 1) return 'True';
                                    else if (a == 0) return 'False';
                                },
                               typeAhead    : true,
                                mode        : 'local',
                                triggerAction    : 'all',
                                emptyText    :'-- Select scale type--',
                                selectOnFocus    :true
                           }));
                        }
                        else if(e.record.data.option == 'available_fields_list')
                        {
//                            console.log(e.record.data.value);
                            CallScript('webcati/_plugins/intervalselect/admin/_forms/forms.fields.editor.php',{
                               questionId: '<?php echo $questionId; ?>',
                               projectId: '<?php echo $projectId; ?>',
                               tableName: '<?php echo $tableName; ?>',
                               data: Ext.encode(e.record.data.value),
                               pluginDir: '<?php echo $pluginDir; ?>'
                            });
                        }
                        else
                        {
                             <?php echo $gridName; ?>.getColumnModel().getColumnAt(1).setEditor(new Ext.form.TextField({
                                   allowBlank     : true
                           }));
                        }
                    },
                },
                loadData: function() 
                {
                  <?php echo $gridName; ?>.getStore().reload();
                },
                /*saveData: function()
                {
                    var source = <?php echo $gridName; ?>.getSource();
                    
                    var jsonData = Ext.encode(source);
                    
                    CallScript('<?php echo $ajax; ?>', {
                        action: 'Save',
                        data: jsonData,
                        projectId: <?php echo $projectId; ?>,
                        questionId: <?php echo $questionId; ?>
                    }) 
                },*/
                store: new Ext.data.GroupingStore({
                        url : '<?php echo $ajax; ?>',
                  //      root : 'rows',
                  //      remoteSort: true,
                        autoLoad: true,
                        sortInfo:{field: 'group', direction: "ASC"},
                        groupField:'group',
                        baseParams: {
                            action: 'get',
                            projectId: '<?php echo $projectId; ?>',
                            questionId: '<?php echo $questionId; ?>'
                    
                        },
                        reader: new Ext.data.JsonReader({
                             root:'rows',
                             totalProperty : "results"
                       },   new Ext.data.Record.create([
                                 { name:"label", type: 'string'},
                                 { name:"option", type: 'string'},
                                 { name:"value", type: 'string'},
                                 { name:"group", type: 'string'},
                                 { name:"type", type: 'string'}
                        ]))
                }),
                view: new Ext.grid.GroupingView({
                    forceFit: true,
                    // custom grouping text template to display the number of items per group
                    groupTextTpl: '{group}'
                }),
               cm: new Ext.grid.ColumnModel({
                    columns: [{
                        header: 'Name',
                        dataIndex: 'label',
                        width: 200
                        
                    },{
                        header: 'Value',
                        dataIndex: 'value',
                        width: 175,
                        flex: 1,
                        editor:{
                            blank: false
                        },
                        renderer: function(value, metaData, record, rowIndex, colIndex, store) {
                            if(record.data.option == 'scale_type')
                            {
                                if(value == 1) return "Interval";
                                else if(value == 2 ) return "Time";       
                            }
                            else if(record.data.option == "slider_type")
                            {
                                if(value == 1) return "Circle";
                                else if(value == 2) return "Squar";
                            }
                            else if(record.data.type == "bool")
                            {
                                if(value == 1) return "true";
                                else if(value == 0) return "false";     
                            }
                            else if(record.data.option == "theme")
                            {
                                return value.charAt(0).toUpperCase() + value.substr(1); 
                            }
                            return value;
                        }
                    },
                    {
                        header: 'Group',
                        dataIndex: 'group',
                        hidden: true,
                        width: 175,
                        menuDisabled: true,
                        flex: 1,
                    },
                     {
                        header: 'Option',
                        dataIndex: 'option',
                        hidden: true,
                        menuDisabled: true,
                        width: 175,
                        flex: 1
                    },
                     {
                        header: 'Type',
                        dataIndex: 'type',
                        hidden: true,
                        menuDisabled: true,
                        width: 175,
                        flex: 1
                    }
                    ]}
                ),
                    sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
                    frame: true,
                });

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