<?php 

//$id_proj = $_POST['id_project'].uniqid();
$id_proj = $_POST['id_project'];
$formId = 'RandomDataFillForm_'.$id_proj;
$projName = $_POST['project'];

?>
//random fill vprašalnikov

var stopExe<?php echo $projName; ?> = false;
var timeOut<?php echo $projName; ?> = 1000;//1 sekunda
var num<?php echo $projName; ?> = 0;//podano število polnjenj
var count<?php echo $projName; ?> = 0;//števec polnjenj
var id_attempt<?php echo $projName; ?> = 0;

function callAjax<?php echo $projName; ?>(id_attempt){
    Ext.Ajax.request({
        url: 'webcati/_forms/_ajax/ajax.fillQuestionnaire.php',
        params: {
            id_proj: '<?php echo $id_proj; ?>',
            id_attempt: id_attempt
        },
        success: function(data){
            //prevEnd<?php echo $projName; ?> = false;
            var val = count<?php echo $projName; ?>/num<?php echo $projName; ?>; 
            Ext.getCmp('fill_progress<?php echo $projName; ?>').updateProgress(val);
            if(val != 1){
                Ext.getCmp('fill_progress<?php echo $projName; ?>').updateText('Processing ' + count<?php echo $projName; ?> + '/' + num<?php echo $projName; ?>);
            } else 
            {
                CallScript('webcati/_forms/_ajax/ajax.fillQuestionnaire.php', {
                    action: 'completeGeneration',
                    id_proj: '<?php echo $id_proj; ?>',
                    id_attempt: id_attempt
                });
                Ext.getCmp('fill_progress<?php echo $projName; ?>').updateText('Done');
            }
            count<?php echo $projName; ?>++;
        }
    });
}  

var <?php echo $formId; ?> = new Ext.form.FormPanel({
    id: '<?php echo $formId; ?>',
    bodyStyle:'padding: 20px',
    region: 'north',
    items: [
        {
            xtype: 'container',
            layout: 'column',
            defaults: {
                style: 'margin-right: 15px; margin-bottom: 15px'
            },
            items: [
                {
                    xtype: 'container',
                    layout: 'form',
                    border: false,
                    items: [
                        {
                            xtype: 'numberfield',
                            id: 'fill_num<?php echo $projName; ?>',
                            name: 'fill_num',
                            fieldLabel: 'Number of fills',
                            width: 100,
                            allowDecimals: false,
                            allowNegative: false,
                            allowBlank: false,
                            value: 1//temp
                        }   
                    ]
                },{
                    xtype: 'button',
                    name: 'start_fill_btn',
                    text: 'Start',
                    width: 70,
                    handler: function(){
                        if(Ext.getCmp('<?php echo $formId; ?>').getForm().isValid()){
                            num<?php echo $projName; ?> = Ext.getCmp('fill_num<?php echo $projName; ?>').getValue();
                            stopExe<?php echo $projName; ?> = false;
                            count<?php echo $projName; ?> = 1;
                            
                            Ext.Ajax.request({
                                url: 'webcati/_forms/_ajax/ajax.fillQuestionnaire.php',
                                params: {
                                    id_proj: '<?php echo $id_proj; ?>',
                                    id_attempt: 0
                                },
                                success: function(data){
                                     var rtnData = Ext.decode(data.responseText);
                                     id_attempt<?php echo $projName; ?> = rtnData.id_attempt;
                                     
                                     Ext.getCmp('fill_progress<?php echo $projName; ?>').updateProgress(0);
                            
                                    //če je št. polnitev manjše od 10, se pošlje vsak ajax posebej
                                    if(num<?php echo $projName; ?> <= 10){
                                        for(var j=0; j<num<?php echo $projName; ?>; j++){
                                            if(stopExe<?php echo $projName; ?> == false){
                                                    callAjax<?php echo $projName; ?>(id_attempt<?php echo $projName; ?> );
                                            }
                                        }
                                    }
                                    
                                    //če je št. polnitev večje od 10, se pošlje po 10 ajaxov naenkrat
                                    else {
                                        var tens = Math.floor(num<?php echo $projName; ?>/10);
                                        var rem = num<?php echo $projName; ?>%10;
                                        
                                        //ostanek od 10
                                        if(rem > 0){
                                            for(var j=0; j<rem; j++){
                                                if(stopExe<?php echo $projName; ?> == false){
                                                    callAjax<?php echo $projName; ?>(id_attempt<?php echo $projName; ?> );
                                                }
                                            }
                                        }
                                        
                                        //grupe po 10
                                        (function sendRequest(i) {
                                            setTimeout(function() {
                                                if(stopExe<?php echo $projName; ?> == false){
                                                    for(var j=0; j<10; j++){
                                                        callAjax<?php echo $projName; ?>(id_attempt<?php echo $projName; ?> );
                                                    }
                                                    if(--i){
                                                        sendRequest(i);
                                                    }
                                                }
                                            }, timeOut<?php echo $projName; ?>);
                                        })(tens);
                                    }
                                    
                                }
                            });
                            
                            
                        }
                    }
                },{
                    xtype: 'button',
                    name: 'stop_fill_btn<?php echo $projName; ?>',
                    text: 'Cancel',
                    width: 70,
                    handler: function(){
                        if(stopExe == false){
                            stopExe = true;
                            Ext.getCmp('fill_progress<?php echo $projName; ?>').updateProgress(0);
                            Ext.getCmp('fill_progress<?php echo $projName; ?>').updateText('Done');
                            var id_attempt<?php echo $projName; ?> = 0;
                        }
                    }
                },
                {
                    xtype: 'button',
                    name: 'custom_fill_btn<?php echo $projName; ?>',
                    text: 'Add custom fill script',
                    width: 70,
                    handler: function(){
                        var data = [];
                        data.push([
                            'custom_fill_script_<?php echo $id_proj; ?>', 
                            'webcati/_forms/buildQuestionare/randomDataFill/forms.customFillScript.php',
                            {
                                project: "<?php echo $projName; ?>", 
                                id_node: <?php echo $_POST['id_node']; ?>,
                                id_project: "<?php echo $id_proj; ?>" 
                            }
                        ]);
                        CallMultiScripts(data);
                    }
                }       
            ]
        },{
            xtype: 'container',
            layout: 'form',
            defaults: {
                style: 'margin-right: 15px; margin-bottom: 15px'
            },
            items: [
                new Ext.ProgressBar({
                    id:'fill_progress<?php echo $projName; ?>',
                    text:'Ready',
                    cls:'left-align'
                })
            ]
        }
    ]
});

mainTabPanel.add({
        id: 'random_data_fill_<?php echo $id_proj; ?>', 
        iconCls:'',
        title: 'Random data fill - <?php echo $projName; ?>',
        closable: true,
        items:[
            <?php echo $formId; ?>
        ]
}).show();
