<?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";

$sql = "SELECT * FROM " .  DB_WARPIT_WEBCATI_BASE . "._Plugin_label WHERE id_plugin = 34";
$res= $db->SQLexecute($sql);

$sourceArray = array();
$propertysNameArray = array();
$fieldsArray = array();

while ($dat = $db->fetchAssoc($res)) {  
    $sourceArray[] = $dat['name'] . ": ''";
    $propertysNameArray[] = $dat['name'] . " : '" . $dat['label'] . "'";
    $fieldsArray[]  = "{ name:'" . $dat['name'] ."', type: '" . $dat['type'] . "' }";
}

//print_r($propertysNameArray);  

?>
   
var <?php echo $gridName;?> = new Ext.grid.PropertyGrid({
        id : '<?php echo $gridName; ?>',
        title: "Basic settings",
        editable : true,
        border : false,
        //autoHeight: true,
        layout: 'fit',
        source: {
            <?php echo implode(',', $sourceArray); ?>
        },
        propertyNames: {
            <?php echo implode(',', $propertysNameArray); ?>
        },
        loadData: function() 
        {
            new Ext.data.JsonStore({
                url : '<?php echo $ajax; ?>',
                root : 'rows',
                autoLoad: true,
                idProperty : '<?php echo $idField; ?>',
                baseParams: {
                    action: 'get',
                    projectId: '<?php echo $projectId; ?>',
                    questionId: '<?php echo $questionId; ?>'
                    
                    },
                fields:[
                         <?php echo implode(',', $fieldsArray); ?>
                         
                ],
                listeners: {
                    load: {
                        fn: function(store, records, options){
                                <?php echo $gridName; ?>.setSource(store.getAt(0).data);
                        }
                    }
                }
           });
        },
        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; ?>
            }) 
        },
        listeners:{
            propertychange: function(record){
                    <?php echo $gridName; ?>.saveData();
                    //<?php echo $gridName; ?>.loadData();
            }
        }
        
}).show();

<?php echo $gridName; ?>.loadData();

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