<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_grid.php");


		$tabId = $_POST['uniqueTabId'];
		$row = $_POST['row'];
		$column = $_POST['column'];
		$gridOptions=$_POST['gridOptions'];
		$options = explode(",", $gridOptions);
			        
        echo "
        //dodan toolbar na pivotgrid
        var tb = new Ext.Toolbar({
			    renderTo: document.body,
			    items: [ ]
			});
			
			
		 var storeProject = new Ext.data.Store({
						  proxy: new Ext.data.HttpProxy({url: 'webcati/_forms/_ajax/ajax.fillPivotGrid.php?action=getProjects'}),
						  reader: new Ext.data.JsonReader({
							  root:'rows'},
							   [{name:'id'},{name:'value'}])
					    });		
					    
		 var storeInterviewer = new Ext.data.Store({
						  proxy: new Ext.data.HttpProxy({url: 'webcati/_forms/_ajax/ajax.fillPivotGrid.php?action=getInterwievers'}),
						  reader: new Ext.data.JsonReader({
							  root:'rows'},
							   [{name:'id'},{name:'value'}])
					    });	
		 var storeCallStatus = new Ext.data.Store({
						  proxy: new Ext.data.HttpProxy({url: 'webcati/_forms/_ajax/ajax.fillPivotGrid.php?action=getCallStatus'}),
						  reader: new Ext.data.JsonReader({
							  root:'rows'},
							   [{name:'id'},{name:'value'}])
					    });							    						    

		var projectG=new Ext.ux.form.LovCombo({  
						    id:'selectProject',  
						    forceSelection: true,  
						    store:  storeProject,  
						    emptyText:'Select parameter ...',  
						    triggerAction: 'all',  
						    editable:false,  
						    displayField:'value',  
					        valueField:'id',
							width:110,
							mode:'local'
						});	
								
			var interviewerG=new Ext.ux.form.LovCombo({  
						    id:'selectInterwiever',  
						    multiSelect: true,
						    forceSelection: true,  
						    store:  storeInterviewer,  
						    emptyText:'Select parameter ...',  
						    triggerAction: 'all',  
						    editable:false,  
						    displayField:'value',  
					        valueField:'id',
							width:110,
							mode:'local'
						});	
							
		var callStatusG=new Ext.ux.form.LovCombo({  
						    id:'selectCall',  
						    multiSelect: true,  
						    store:  storeCallStatus,  
						    emptyText:'Select parameter ...',  
						    triggerAction: 'all',  
						    editable:false,  
						    displayField:'value',  
					        valueField:'id',
							width:220,
							mode:'local'
						});	
						
		var dateFrom =new Ext.ux.form.DateTime();	
		var dateTo =new Ext.ux.form.DateTime();							
						
			var filterButton = new Ext.Button({
			id:'filter',  
			text: 'filtering',
		    handler: function(){
		    var statement='';
		    var n;

			    if (callStatusG.getValue()!='')
			    {
			    	n=callStatusG.getValue().split(','); 
			       	statement+=createCondition(n,statement, 'status');
			     }
			    if (interviewerG.getValue()!='')
			    {
			    	n=interviewerG.getValue().split(','); 
			       	statement+=createCondition(n,statement, 'id_user');
			     }			     
 				if (projectG.getValue()!='')
			    {
			    	n=projectG.getValue().split(','); 
			       	statement+=createCondition(n,statement, 'idProject');
			     }	
			     
			    if (dateFrom.getValue()!='' && dateTo.getValue()!='')
			    { 
			     var fromD=dateFrom.getValue().getTime()/1000;
			     var toD=dateTo.getValue().getTime()/1000;
			  	statement=createDate(fromD, toD, statement);
			     }	

				
			 	pivotStore.removeAll();
			  	pivotStore.proxy= new Ext.data.HttpProxy({url: 'webcati/_forms/_ajax/ajax.fillPivotGrid.php?action=fillPivot&row=$row&column=$column&stat='+statement});
				pivotStore.load();
				pivotGrid.doLayout();
		    }
		   }) ;		

		    function createDate(from , to, statement)
		   {	
		   		if (statement.length==0){ statement='WHERE ';}
		   		else { statement+=' AND ';}
		   		statement+= '( call_time >= '+from+' AND end_time <='+to+')';
		  		 return statement;
			}

		   function createCondition(n, statement, parameter)
		   {	
		   		if (statement.length==0){ statement+='WHERE ';}
		   		else if (n.length>0){ statement+=' AND ';}
		   		statement+='(';
		   		for (var i=0; i<n.length-1; i++)
		   		{
		   			statement+= parameter+' = '+n[i]+' OR ';
		   		}
		   		statement+= parameter+' = '+n[n.length-1]+')';
		  		 return statement;
			}
		   			
		";

		for ($i=0;$i<count($options)-1;$i++)
		{
			if ($options [$i]=='Project')
			{
				echo "storeProject.load();
				tb.add('Projects:  ',projectG,'&nbsp;&nbsp;','-','&nbsp;&nbsp;'); 
				";
			}
			if ($options [$i]=='Interviewers')
			{
				echo "storeInterviewer.load();
				tb.add('Interviewers:  ',interviewerG,'&nbsp;&nbsp;','-','&nbsp;&nbsp;');";
			}		
			if ($options [$i]=='Call status')
			{
				echo "storeCallStatus.load();
				tb.add('Call status:  ',callStatusG,'&nbsp;&nbsp;','-','&nbsp;&nbsp;');";
			}
			if ($options [$i]=='Time')
			{
				echo "tb.add(' Date from: ',dateFrom,'&nbsp;&nbsp;');";
				echo "tb.add('to: ',dateTo,'&nbsp;&nbsp;','-','&nbsp;&nbsp;');";
			}
		}
		
		if (count($options)>1)
		{
			echo "tb.add(filterButton,'&nbsp;&nbsp;');";
		}
	 

	   echo"     var SaleRecord = Ext.data.Record.create([
        {name: 'l1',   type: 'string'},
        {name: 'l2',  type: 'string'},
        {name: 't1',     type: 'string'},
        {name: 't2',    type: 'string'},
        {name: 'st',    type: 'int'}
    ]);
    
	     var pivotStore = new Ext.data.Store({
						    proxy: new Ext.data.HttpProxy({url: 'webcati/_forms/_ajax/ajax.fillPivotGrid.php?action=fillPivot&row=$row&column=$column'}),
						    reader: new Ext.data.JsonReader({
							    root:'rows'}, SaleRecord)
					    });		    
		pivotStore.load();		    
	    
	    var pivotGrid = new Ext.grid.PivotGrid({
	        title     : 'PivotGrid example',
	        flex:1,
	        width:'100%',
	        store     : pivotStore,
	        aggregator: 'sum',
	        measure   : 'st',
	        viewConfig: {
	            title: 'TABLE'
	        },
	        leftAxis: [
	            {
					width: 120,
	                dataIndex: 'l1'
	            }
	        ],
	        topAxis: [
	            {
	           		 width: 90,
	                dataIndex: 't1'
	            }
	        ]
	    });
	    pivotGrid.add(tb);

        mainTabPanel.add({ 
		id:'$tabId',
		closable:true,
		title: 'Complete pivot tables statistics',
                RMreopen : function(p){ 
			pivotGrid.getStore().reload( );
		},

		iconCls: 'RMicon_view_details',
		layout:'vbox',
        items:[pivotGrid]}).show();";
		
		
        

?>
