<?php 

$idField = "path";

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

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

$cmpName = "installer_panel";

$pluginsFolder = "webcati/_plugins/";

?>


var <?php echo $cmpName;?> = new Ext.form.FormPanel({
        id : '<?php echo $cmpName; ?>',
        title: 'Install new plugin',
        closable: 'true',
        fileUpload: true,
        border : false,
        height: 300,
        pluginName: '',
        //layout: 'fit',
        autoScroll:true,
        items:[
        {
            xtype: "fieldset",
            title: "Upload plugin",
            autoHeight:true,
            anchor: "45%",
            items: [{
                layout: 'column',
                anchor: 0,
                border : false,
                items: [{
                    columnWidth: .5,
                    layout: 'form',
                    border : false,
                    items: [{
                        xtype: 'fileuploadfield',
                        fieldLabel: 'Select file',
                        validator: function(v){
                        if(!/\.zip$/.test(v)){
                          return 'Only zip files allowed';
                        }
                        return true;
                      },
                        name: 'plugin_zip',
                        width: 150,
                        border : false,
                    }]
                },{
                        columnWidth: 0.5,
                        border : false,
                        items: [{
                            text: 'Upload',
                            xtype:'button',
                            border : false,
                            handler: function(){
                                
                                 if(<?php echo $cmpName; ?>.getForm().isValid())
                                 {
                                   <?php echo $cmpName; ?>.getForm().submit({
                                    url:'<?php echo $ajax; ?>',
                                    params:{
                                        action: 'upload_plugin'
                                    },
                                    success: function(form, action) {
                                        Ext.Msg.alert('Success', "Plugin was succesfuly uploaded");
                                        Ext.getCmp('plugins_combo').getStore().reload();
                                    },
                                    failure: function(form, action) {
                                         Ext.Msg.alert('Success', "Plugin uploaded");
                                    }
                                   });
                                 }
                            }
                        }]
                    }]
            }]
        },
        {
            xtype: "fieldset",
            title: "Install plugin",
            autoHeight:true,
            anchor: "45%",
            items: [{
                layout: 'column',
                anchor: 0,
                border : false,
                items: [{
                    columnWidth: .5,
                    layout: 'form',
                    border : false,
                    items: [{
                        xtype: 'combo',
                        width: 150,
                        border : false,
                        id:'plugins_combo',
                        displayField: 'name',
                        valueField: 'path',
                        fieldLabel: 'Select plugin',
                        triggerAction: 'all',
                        store: new Ext.data.JsonStore({
                            url : '<?php echo $ajax; ?>',
                            root : 'rows',
                            autoLoad: true,
                            idProperty : '<?php echo $idField; ?>',
                            baseParams: { 
                                action: 'list_plugins'
                                },
                            fields:[
                                     { name:"<?php echo $idField;?>", type: 'string' },
                                     { name:"name", type: 'string' }
                            ],
                        })
                    }]
                },{
                        columnWidth: 0.5,
                        border : false,
                        items: [{
                            text: 'Install',
                            xtype:'button',
                            border : false,
                            handler: function(){
                                if(<?php echo $cmpName; ?>.getForm().isValid())
                                {
                                   <?php echo $cmpName; ?>.getForm().submit({
                                    url:'<?php echo $ajax; ?>',
                                    params:{
                                        action: 'install',
                                        path: Ext.getCmp('plugins_combo').getValue()
                                    },
                                    success: function(form, action) {
                                        Ext.Msg.alert('Success', "Plugin was successfuly installed");
                                        Ext.getCmp('pluginsGrid_<?php echo $tableName; ?>').getStore().reload();
                                    },
                                    failure: function(form, action) {
                                         Ext.Msg.alert('Failure', action.result.error);
                                    }
                                   });
                                }
                            }
                        }]
                    }]
            }]
        },
        {
            xtype: "fieldset",
            title: "Install using instalation code",
            autoHeight:true,
            anchor: "45%",
            items: [{
                layout: 'column',
                anchor: 0,
                border : false,
                items: [{
                    columnWidth: .5,
                    layout: 'form',
                    border : false,
                    items: [{
                        xtype: 'textfield',
                        width: 150,
                        id: 'installation_code',
                        border : false,
                        name: 'code',
                        fieldLabel: 'Installation code'
                    }]
                },{
                        columnWidth: 0.5,
                        border : false,
                        items: [{
                            text: 'Install',
                            xtype:'button',
                            border : false,
                            handler: function(){
                                Ext.Ajax.request({
                                      url: '<?php echo $ajax; ?>',
                                      params:{
                                          action: 'install_with_code',
                                          code: Ext.getCmp('installation_code').getValue()
                                      },
                                      method:'POST',
                                      success: function(result, request){
                                         var res = new Object();
                                         res = Ext.util.JSON.decode(result.responseText);
                                         if(res.success == false)
                                         {
                                               Ext.MessageBox.alert('Warning',res.error);
                                         }else
                                         {
                                             Ext.getCmp('pluginsGrid').getStore().reload();
                                         }
                                      }
                                    });
                               
                            /*   CallScript('<?php echo $ajax; ?>', {
                                  action: 'install_with_code',
                                  code: Ext.getCmp('installation_code').getValue()
                               },function(a,b,c){
                                   console.log(a,b,c);
                               });*/
                              
                            }
                        }]
                    }]
            }]
        }
        ],
        buttons: []
        
});

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