<?php
require_once "../../../../autoload.php";
use \koolreport\KoolReport;
use \koolreport\processes\Filter;
use \koolreport\processes\TimeBucket;
use \koolreport\processes\Group;
use \koolreport\processes\Limit;
use \koolreport\processes\AggregatedColumn;
use \koolreport\processes\CalculatedColumn;
class SakilaRental extends KoolReport
{
   
	use \koolreport\inputs\Bindable;
	use \koolreport\inputs\POSTBinding;
	
	 protected function defaultParamValues()
    {
        return array(
            "portalSelect2"=>array(),
            "countrySelect2"=>array(),
            "sizeSelect2"=>array(),
			"dateRange"=>array("10/1/2018","3/31/2019"),
        );
    }
    
    protected function bindParamsToInputs()
    {
        return array(
            
			"Creative"=>"sizeSelect2",
            "Adunit"=>"portalSelect2",
			"Country"=>"countrySelect2",
			"cdate"=>"dateRange",
        );
    }
	
	 public function settings()
    {
        return array(
            "dataSources"=>array(
                "6months"=>array(
                    "class"=>'\koolreport\datasources\CSVDataSource',
                    'filePath'=>dirname(__FILE__)."/Video-benchmarksNEW.csv",
                )
            )
        );
    }   
	   
	   
    protected function setup()
    {
	
		
		$query_params = array();
        if($this->params["Creative"]!=array())
        {
           //echo "am here";
		   $query_params[":Creative"] = $this->params["Creative"];
        }
        if($this->params["Adunit"]!=array())
        {
            $query_params[":Adunit"] = $this->params["Adunit"];
        }
        if($this->params["Country"]!=array())
        {
            $query_params[":Country"] = $this->params["Country"];
        }
		
		if($this->params["cdate"]!=array())
        {
			 //echo "am here";
            $query_params[":cdate"] = $this->params["cdate"];
			
			$startInput = $query_params[":cdate"][0];
			$endInput = $query_params[":cdate"][1];
 			$newStartInput = date("n/j/Y", strtotime($startInput));
			$newEndInput = date("n/j/Y", strtotime($endInput));
        }
		
	
        
		if(!isset($newStartInput) && !isset($newEndInput))
		{
			$newStartInput = "10/1/2018";
			$newEndInput = "3/31/2019";
		
		}
		
		
		
		
		if($query_params[":Creative"]==array() && $query_params[":Adunit"]==array() && $query_params[":Country"]==array())
		{
				echo "am here";
		
				 $this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
				
		
				)))
		
		
		
				/*->pipe(new AggregatedColumn(array(
    			    "total_imps"=>array("sum","impressions"),
					"total_clicks"=>array("sum","clicks"),
				)))
				->pipe(new CalculatedColumn(array(
   					"NCTR"=>"{total_clicks}/{total_imps}"
				)))
		
				->pipe(new CalculatedColumn(array(
    			  "NCTR"=>array(
       					 "exp"=>"{clicks}/{impressions}*100",
        				 "type"=>"number",
						 "decimals"=>2,
						
    			))))*/
				
				
				->pipe(new CalculatedColumn(array(
  			"NEWCTR"=>array(
		 		"exp"=>"{clicks}/{impressions}*100",
		 "type"=>"number",
		
	))))
->pipe(new Group(array(
	"by"=>"Creative",
	"sum"=>"NEWCTR"
)))
				
				
				
				
				/*->pipe(new Group(array(
           
					
					"by"=>"Creative",
					
					"sum"=>array("clicks","impressions")
					
					
					
           			
       		    )))*/
				
				
				
				
				
				
		
				->pipe($this->dataStore('ctr_by_size'));
		}
		
		
		if($query_params[":Creative"]!=array() && $query_params[":Adunit"]==array() && $query_params[":Country"]==array())
		{
		
				print_r($query_params[":Creative"]);
				
				$this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("Creative","in",$query_params[":Creative"]),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
		
				)))
		
		
				
				
				
				/*->pipe(new AggregatedColumn(array(
    			    "total_imps"=>array("sum","impressions"),
					"total_clicks"=>array("sum","clicks"),
				)))
				->pipe(new CalculatedColumn(array(
   					
					"NCTR"=>"{total_clicks}/{total_imps}"
				)))*/
		
				
		
				->pipe(new Group(array(
           
					"by"=>"Creative",
					"sum"=>"clicks",
					"sum"=>"impressions"
					
					
					
       		    )))
		
				->pipe($this->dataStore('ctr_by_size'));
				
				
				
				
		}
		
		
		
		if($query_params[":Creative"]!=array() && $query_params[":Adunit"]!=array() && $query_params[":Country"]==array())
		{
		
				
				
				$this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("Creative","in",$query_params[":Creative"]),
				array("Adunit","in",$query_params[":Adunit"]),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
				)))
		
		
				->pipe(new Group(array(
           
					"by"=>"Creative",
           			"avg"=>"{clicks}/{impressions}"
       		    )))
		
				->pipe($this->dataStore('ctr_by_size'));
				
				
				
				
		}
		
		
		
		if($query_params[":Creative"]!=array() && $query_params[":Adunit"]==array() && $query_params[":Country"]!=array())
		{
		
				
				
				$this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("Creative","in",$query_params[":Creative"]),
				array("Country","in",$query_params[":Country"]),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
				)))
		
		
				->pipe(new Group(array(
           
					"by"=>"Creative",
           			"avg"=>"{clicks}/{impressions}"
       		    )))
		
				->pipe($this->dataStore('ctr_by_size'));
				
				
				
				
		}
		
		
		if($query_params[":Creative"]==array() && $query_params[":Adunit"]!=array() && $query_params[":Country"]!=array())
		{
		
				
				
				$this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("Adunit","in",$query_params[":Adunit"]),
				array("Country","in",$query_params[":Country"]),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
				)))
		
		
				->pipe(new Group(array(
           
					"by"=>"Creative",
           			"avg"=>"{clicks}/{impressions}"
       		    )))
		
				->pipe($this->dataStore('ctr_by_size'));
				
				
				
				
		}
		
		
		
		if($query_params[":Creative"]==array() && $query_params[":Adunit"]!=array() && $query_params[":Country"]==array())
		{
		
				
				$this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("Adunit","in",$query_params[":Adunit"]),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
		
				)))
		
		
				->pipe(new Group(array(
           
					"by"=>"Creative",
           			"avg"=>"{clicks}/{impressions}"
       		    )))
		
				->pipe($this->dataStore('ctr_by_size'));
				
				
				
				
		}
		
		if($query_params[":Creative"]==array() && $query_params[":Adunit"]==array() && $query_params[":Country"]!=array())
		{
		
				
				$this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("Country","in",$query_params[":Country"]),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
				)))
		
		
				->pipe(new Group(array(
           
					"by"=>"Creative",
           			"avg"=>"{clicks}/{impressions}"
       		    )))
		
				->pipe($this->dataStore('ctr_by_size'));
				
				
				
				
		}
		
		
		if($query_params[":Creative"]!=array() && $query_params[":Adunit"]!=array() && $query_params[":Country"]!=array())
		{
		
				
				$this->src('6months')
		
		
				->pipe(new Filter(array(
		
				array("Creative","contain",'v'),
				array("Creative","in",$query_params[":Creative"]),
				array("Adunit","in",$query_params[":Adunit"]),
				array("Country","in",$query_params[":Country"]),
				array("cdate",">=", $newStartInput),
   				array("cdate","<=",$newEndInput),
		
				)))
		
		
				->pipe(new Group(array(
           
					"by"=>"Creative",
           			"avg"=>"{clicks}/{impressions}"
       		    )))
		
				->pipe($this->dataStore('ctr_by_size'));
				
				
				
				
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
    } 
}