
<?php  
            error_reporting(0);
            $start=$_POST['s'];
            $end=$_POST['e'];
            $act=$_POST['a'];
            if($start==NULL && $end==NULL){
                $title=GetMonthFull(date('m')).' '.date('Y');
                $querynya="MONTH(tanggal)='".date('m')."' AND YEAR(tanggal)='".date("Y")."'";
            }
            else if($start!=NULL && $end!=NULL){
                
                $title=GetTanggalIndo($start).' - '.GetTanggalIndo(date('Y-m-d'));
                $querynya="tanggal>='$start' AND tanggal<='".date('Y-m-d')."'";
            }
            else{
                $title=GetTanggalIndo($start).' - '.GetTanggalIndo($end);
                $querynya="tanggal>='$start' AND tanggal<='$end'";
            }
            if($act!=NULL){
                $querynya.=" AND jenis_aktivitas='$act'";
            }
            ?>
<script type="text/javascript">
$(function () {

   
            // Create the chart
            $('#container').highcharts({
				 credits: {
					enabled: false
					},
                chart: {
                    type: 'column',
                events: {
                    drilldown:function(e){
           // console.log(this);
            //$('#table-data').html(this.options.series[0].name);
            $('#table-data').load('<?php echo base_url()?>dashboard/data_marketing',{m:e.point.drilldown,start:'<?php echo $start?>',end:'<?php echo $end?>',act:'<?php echo $act?>'});
            //alert();
            //alert(this.options.series[0].name);
           // alert('OK');
                    },
                    drillup:function(e){
                        $('#table-data').empty();
                    }
                }
                }, 
                title: {
                    text: 'Solution Force Activity. <?php echo $title?>'
                },
                subtitle: {
                    text: 'Click the columns to view results.'
                },
                xAxis: {
                    type: 'category'
                },
                yAxis: {
                    title: {
                        text: 'Total Activity'
                    }
                },
                legend: {
                    enabled: false
                },
                plotOptions: {
                    series: {
                        borderWidth: 0,
                        dataLabels: {
                            enabled: true,
                            format: '{point.y}'
                        }
                    }
                },

                tooltip: {
                    headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
                    pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y}</b> Prospect Entry<br/>'
                },

                series: [{
                    name: 'All Marketing',
                    colorByPoint: true,
                    data: [
					<?php
                                        $filter=array();
                                        if($this->session->userdata('webmaster_grup')=='3' || $this->session->userdata('webmaster_grup')=='11' ){
                                            $filter['divisi']='where/'. GetValue('divisi', 'sv_master_sales',array('id'=>'where/'.$this->session->userdata('webmaster_marketing')));
                                        }
                                        $anaknya=anaknya();
                                        $marketing=GetValue('marketing','sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')));
            $this->db->select('*')->from('sv_master_sales');                            
            if(!diaadmin()){
                $anaknya=implode(',',$anaknya);
            //$this->db->where('leader',$anaknya);
            //$this->db->or_where('id',$marketing);
                $this->db->where("(leader IN ($anaknya) OR id='$marketing')");
            }
            if($this->session->userdata('divisi') && $this->session->userdata('groups')){                
                $this->db->where("(divisi ='".$this->session->userdata('divisi')."' AND groups='".$this->session->userdata('groups')."')");
            }
            else{
                if($this->session->userdata('divisi')){
                $this->db->where('divisi',$this->session->userdata('divisi'));
                //$q.=" AND b.divisi='".$this->session->userdata('divisi')."'";
                }
                elseif($this->session->userdata('groups')){
                $this->db->where('groups',$this->session->userdata('groups'));
                //$q.=" AND b.divisi='".$this->session->userdata('divisi')."'";
                }
               
            }
            
            //if(diasales()){$this->db->or_where('id',$marketing);}
            //lastq();
            //$mar=GetAll('sv_master_sales',$filter)->result_array();
            $this->db->where('status','Aktif');
            $mar=$this->db->get()->result_array();
            
             if(diaadmin()){
                 $mar=array();
                 //$this->db->select('*')->from('sv_master_sales')->where('leader!=null')->group_by('leader'); 
                 $qzzz="SELECT * FROM sv_master_sales WHERE leader>0 AND status='Aktif' GROUP BY leader";
                 $oi=$this->db->query($qzzz)->result_array();
                 foreach($oi as $pemimpin){
                     $ppm[]=$pemimpin['leader'];
                 }
                 $ppmz=implode(',',$ppm);
                 //lastq();
                 $a=0;
                 foreach($oi as $leader){
                     $mar[$a]['id']=$leader['leader'];
                     $mar[$a]['nama_lengkap']=GetValue('nama_lengkap','master_sales',array('id'=>'where/'.$leader['leader']));
                     $a++;
                     $qbz="SELECT * FROM sv_master_sales WHERE leader='".$leader['leader']."' AND status='Aktif' AND id NOT IN($ppmz)";
                     //if($this->session->userdata('divisi')) $qbz.=" AND divisi='".$this->session->userdata('divisi')."'";
                     //if($this->session->userdata('groups')) $qbz.=" AND groups='".$this->session->userdata('groups')."'";
                     
                if($this->session->userdata('divisi') && $this->session->userdata('groups')){                
                    $qbz.=" AND (divisi='".$this->session->userdata('divisi')."' AND groups='".$this->session->userdata('groups')."' )";
                }
                else{
                    if($this->session->userdata('divisi')){
                    $qbz.=" AND divisi='".$this->session->userdata('divisi')."'";
                    }
                    elseif($this->session->userdata('groups')){
                        $qbz.=" AND groups='".$this->session->userdata('groups')."'";
                    }
                }
                        $oiz=$this->db->query($qbz)->result_array();
                        //lastq(); 
                        foreach($oiz as $leaderz){
                            $xyz=$a-1;
                            //if(in_array($leaderz['id'],$leader)){ //echo "HAI";
                            
                            //}else{
                     $mar[$a]['id']=$leaderz['id'];
                     $mar[$a]['nama_lengkap']=$leaderz['nama_lengkap'];
                            $a++;//}
                        }
                 }
                                        //$mar=$this->db->get()->result_array();
             }
            // print_mz($mar);
                                        //lastq();
					$drill[]=array();
						$a=0;
						foreach($mar as $sales){
                                                    
        $wherez['status']='where/Aktif';
        if($this->session->userdata('divisi')) $wherez['type']='where/'.$this->session->userdata('divisi');
       // if($this->session->userdata('groups')) $wherez['type']='where/'.$this->session->userdata('groups');
                                          $q="SELECT COUNT(id) as tot"; 
                                          $act=GetAll('sv_ref_activity',$wherez)->result_array();
                                          foreach($act as $ct){
                                          $q.=",(SELECT COUNT(id) as `".$ct['id']."` FROM sv_activity_sales WHERE jenis_aktivitas='".$ct['id']."' AND sales=".$sales['id']." AND ($querynya)) as `".$ct['id']."`";
                                                    }

$q.=" FROM sv_activity_sales WHERE sales=".$sales['id']." AND ($querynya)";
                                                  // echo $q; 
                                        $sum=$this->db->query($q)->row_array();
                                        
					$drill[$a]['salesid']=$sales['id'];
                                        foreach($act as $ct){
                                            
					$drill[$a][$ct['id']]=$sum[$ct['id']];
                                        }
					?>
					
						{
							name: '<?php echo $sales['nama_lengkap']?>',
							y: <?php echo $sum['tot']?>,
							drilldown: '<?php echo $sales['id']?>'
						},
						<?php 
						$a++;
						} ?>	
					]
                }],
                drilldown: {
                    series: [
					<?php foreach($drill as $down){ ?>
					{
                id: '<?php echo $down['salesid']?>',
                data: [
            <?php foreach($act as $ct){ ?>
                                            
                    ['<?php echo $ct['notes']?>', <?php echo $down[$ct['id']]?>],
					<?php //$drill[$a][$ct['title']]=$sum[$ct['title']]; 
                                        }?>
                ]
					},<?php } ?>
			]
                }
            });
        }
    );


		</script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 " class="col-md-12"></div>
<div id="table-data">
    
</div>