<?php 

$idField = "id_plugin";

$ajax  = "webcati/_forms/plugins/_ajax/ajax.plugin.php";

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

$gridName = "pluginsGrid";

$pluginsFolder = "webcati/_plugins/";

?>

var <?php echo $gridName;?> = new Ext.grid.EditorGridPanel({
        id : '<?php echo $gridName; ?>',
        editable : false,
        border : false,
        height: 300,
        pluginName: '',
        //layout: 'fit',
        selModel : new Ext.grid.RowSelectionModel(),
        autoScroll:true, 
        closable : true,
        store: new Ext.data.JsonStore({
                url : '<?php echo $ajax; ?>',
                root : 'rows',
                autoLoad: true,
                idProperty : '<?php echo $idField; ?>',
                baseParams: {
                    action: 'get',
                    tableName: '<?php echo $tableName; ?>',
                    questionId: '<?php echo $questionId; ?>'
                    
                    },
                fields:[
                         { name:"<?php echo $idField;?>", type: 'int' },
                         { name:"plugin_name", type: 'string' },
                         { name:"plugin_path", type: 'string' },
                         { name:"defined", type: 'bool' }
                ],
        }),
        id: '<?php echo $gridName; ?>',
        columns: [{
            header: ' ',
            dataIndex: 'defined',
            width: 20,
            renderer: function(value){
                if(value == 1)
                    return '<img width="15" height="15" src="_img/selected-plugin.jpg" />';
                return '';
            },
        },{
            header: 'Plugin Name',
            dataIndex: 'plugin_name',
            width: 175,
            //flex: 1
            
        }],
        viewConfig : {
            getRowClass : function(record, index) {
                var c = record.get('defined');
                if (c == 1) {
                    <?php echo $gridName; ?>.loadSettings(record.get('plugin_path'), record.get('<?php echo $idField; ?>'));
                    <?php echo $gridName; ?>.pluginName = record.get('plugin_path');
                    
                    return 'grid-plugin-row-green';
                } 
        }
        },
        loadSettings: function(pluginPath, idPlugin)
        {
             CallScript('<?php echo $pluginsFolder; ?>' + pluginPath + '/admin/_forms/forms.init.php', {
                  tableName: '<?php echo $tableName; ?>',
                  questionId : '<?php echo $questionId; ?>',
                  projectId: '<?php echo $projectId; ?>',
                  pluginPath: pluginPath
              });
              
              CallScript('webcati/_forms/plugins/forms.copy.settings.php', {
                   tableName : '<?php echo $tableName; ?>',
                   questionId:  '<?php echo $questionId; ?>',
                   projectId : '<?php echo $projectId; ?>',
                   pluginId:  idPlugin
            });
        },
        tbar:[{
            text: 'Define',
            handler: function()
            {
               var selectedRecord = <?php echo $gridName; ?>.getSelectionModel().getSelected();
               Ext.Ajax.request({
                        waitTitle : 'Komunikacija s strežnikom',
                        waitMsg : 'Prenos podatkov',
                        url : '<?php echo $ajax ?>',
                        params : {
                            action : 'DefinePlugin',
                            tableName: '<?php echo $tableName; ?>',
                            questionId : '<?php echo $questionId; ?>',
                            projectId: '<?php echo $projectId; ?>',
                            pluginName: selectedRecord.get('plugin_path'),
                            id_plugin: selectedRecord.get('<?php echo $idField ?>')
                        },
                        //async: false,
                        success : function(res, opt) 
                        {  
                           <?php echo $gridName; ?>.getStore().reload();
                           /* CallScript('<?php echo $ajax; ?>', {
                               action: 'RemoveSettings',
                               questionId: <?php echo $questionId; ?>,
                               projectId: <?php echo $projectId; ?>
                           });**/  
                           //Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').destroy();   
                      //     Ext.getCmp('copySettings_<?php echo $tableName; ?>').destroy();
                      //     Ext.getCmp('settingsGrid_<?php echo $tableName; ?>').destroy();
                           
                                    
                        }
                    });
            }
        },{
            text: 'Undefine',
            handler: function()
            {
               Ext.Ajax.request({
                        waitTitle : 'Komunikacija s strežnikom',
                        waitMsg : 'Prenos podatkov',
                        url : '<?php echo $ajax ?>',
                        params : {
                            action : 'DefinePlugin',
                            tableName: '<?php echo $tableName; ?>',
                            questionId : <?php echo $questionId; ?>,
                            id_plugin: 'NULL'
                        },
                        //async: false,
                        success : function(res, opt) 
                        {  
                           <?php echo $gridName; ?>.getStore().reload();
                           CallScript('<?php echo $ajax; ?>', {
                               action: 'RemoveSettings',
                               pluginName: <?php echo $gridName; ?>.pluginName,
                               questionId: <?php echo $questionId; ?>,
                               projectId: <?php echo $projectId; ?>
                           });
                           
                           // console.log('pluginSettings_<?php echo $tableName; ?>');
                           Ext.getCmp('pluginSettings_<?php echo $tableName; ?>').removeAll();
                             
                        }
                });
               
               
            }
        },{
            text: 'Install',
            handler: function()
            {
                CallScript('webcati/_forms/plugins/forms.installer.panel.php', {
                   tableName : '<?php echo $tableName; ?>',
                   questionId:  '<?php echo $questionId; ?>',
                   projectId : '<?php echo $projectId; ?>'
                });
            }
        }]
});
Ext.getCmp('pluginList_<?php echo $tableName; ?>').add(<?php echo $gridName; ?>);

Ext.getCmp('pluginEditor_<?php echo $tableName; ?>').show();

