<?php 
        error_reporting(0);
            $limit=$_POST['limit'];
            if($limit==NULL)$limit=10;
            $limit+=1;
            
            $sales=$_POST['marketing'];
            if($sales!=NULL){
                $title1=  GetValue('nama_lengkap','sv_master_sales',array('id'=>'where/'.$sales));
                $cust="AND sales='$sales'";
            }else{
                $title1=  '';
                $cust='';
            }
            $and='';
            if(!diaadmin()){
                $anaknya=anaknya();
                $anaknya=implode(',',$anaknya);
                $marketing=GetValue('marketing','sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')));
                
                //$this->db->where_in('leader',$anaknya);
                //$this->db->or_where('id',$marketing);
                
                $and=" AND (c.leader IN($anaknya) OR c.id='$marketing')";
            }
            $aktf=" AND c.status='Aktif'";
            $aktfb=" AND b.status='Aktif'";
       if($this->session->userdata('divisi')) $and.=" AND divisi='".$this->session->userdata('divisi')."'";
       if($this->session->userdata('groups')) $and.=" AND groups='".$this->session->userdata('groups')."'";
            $start=$_POST['s'];
            $end=$_POST['e'];
            if($start==NULL && $end==NULL){
                $title=GetMonthFull(date('m')).' '.date('Y');
                $querynya="MONTH(tanggal)='".date('m')."' AND YEAR(tanggal)='".date("Y")."'";
                $querynyaz="MONTH(a.tanggal)='".date('m')."' AND YEAR(a.tanggal)='".date("Y")."'";
                
            }else{
                $title=GetTanggalIndo($start).' - '.GetTanggalIndo($end);
                $querynya="tanggal>='$start' AND tanggal<='$end'";
                $querynyaz="a.tanggal>='$start' AND a.tanggal<='$end'";
            }
            
//$mar=$this->db->query("SELECT COUNT(a.id) as hasil,a.pelanggan as id,b.title title FROM sv_activity_sales a LEFT JOIN sv_master_pelanggan b ON b.id=a.pelanggan LEFT JOIN sv_master_sales c ON a.sales=c.id WHERE ($querynya) $aktf $cust $and GROUP BY pelanggan ORDER BY hasil DESC LIMIT $limit")->result_array();
$mar=$this->db->query("SELECT * FROM sv_ref_bidang_pelanggan")->result_array();
foreach($terlaku as $laku){
    if($laku['pelanggan']!=NULL){
        $ssz[]=$laku['pelanggan'];
    }
}
$lakubanget=implode(',',$ssz);
//echo $lakubanget;
//die();?>
<script type="text/javascript">
$(function () {
 

            // Create the chart
            $('#customeranalysis').highcharts({
				 credits: {
					enabled: false
					},
                chart: {
                    type: 'column',
                    events: {
                        drillup: function (e) {
                            this.yAxis[0].update({
                            title:{
                             text:"Total Customer"
                            }
                            });
                            $('#table-customeranalysis').empty();
                        },
                        drilldown: function (e) {
                            this.yAxis[0].update({
                            title:{
                             text:"Total Customer"
                            }
                            });
                            $('#table-customeranalysis').load('<?php echo base_url()?>dashboard/data_customer',{m:e.point.drilldown});
                        }
                    }     
                },
                title: {
                    text: 'Customer by Bidang'
                },
                subtitle: {
                    text: 'Click the columns to view results.'
                },
                xAxis: {
                    type: 'category'
                },
                yAxis: {
                    title: {
                        text: 'Total Customer'
                    }
                },
                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> Activity<br/>'
                },

                series: [{
                    name: 'All Customer',
                    colorByPoint: true,
                    data: [
					<?php //$mar=$this->db->query("SELECT * FROM sv_master_pelanggan WHERE id IN($lakubanget)")->result_array();
					//$drill[]=array();
						$a=0;
						$div=divisinya();
                $anaknya=  anaknya(); 
						foreach($mar as $sales){
                                                
//            if(!diaadmin()){
//                $anaknya=anaknya();
//                $anaknya=implode(',',$anaknya);
//                $marketing=GetValue('marketing','sv_admin',array('id'=>'where/'.$this->session->userdata('webmaster_id')));
//                //$this->db->where_in('leader',$anaknya);
//            //$this->db->or_where('id',$marketing);
//            $and=" AND (c.leader IN($anaknya) OR c.id='$marketing')";
//            }    
//            $q="SELECT COUNT(a.id) as tot"; 
//            $wherez['status']='where/Aktif'; 
//            if($this->session->userdata('divisi')) $wherez['type']='where/'.$this->session->userdata('divisi');
//            $act=GetAll('sv_ref_activity',$wherez)->result_array();
//                  foreach($act as $ct){
//                            $q.=",(SELECT COUNT(a.id) as `".$ct['title']."` FROM sv_activity_sales a LEFT JOIN sv_master_sales b ON a.sales=b.id WHERE a.pelanggan_divisi='".$ct['id']."' AND a.pelanggan='".$sales['id']."' AND ($querynyaz) $aktfb $cust $and ";
//                            $q.=") as `".$ct['title']."`";
//                            }
//                            $q.=" FROM sv_activity_sales a LEFT JOIN sv_master_sales b ON a.sales=b.id  WHERE a.pelanggan='".$sales['id']."' AND ($querynyaz) $aktfb $cust $and";
//                         
//            if(!diaadmin()){$q.=" AND (b.leader IN (".implode(',',$anaknya).")";}
//            if(diasales()){$q.=" OR a.created_by ='".$this->session->userdata('webmaster_id')."')";}else{
//                if(!diaadmin()) $q.=')';
//            }
            $q="SELECT COUNT(a.id) as tot"; 
            //$wherez['status']='where/Aktif'; 
            //if($this->session->userdata('divisi')) $wherez['type']='where/'.$this->session->userdata('divisi');
            $wherez['perusahaan']='where/'.$sales['id']; 
            //if($this->session->userdata('divisi')) $wherez['type']='where/'.$this->session->userdata('divisi');
             
             $act=GetAll('sv_master_pelanggan',$wherez)->result_array();
                                                    foreach($act as $ct){
                                                    //$q.=",(SELECT COUNT(a.id) as `".$ct['id']."` FROM sv_activity_sales a LEFT JOIN sv_master_sales c ON a.sales=c.id WHERE a.pelanggan=".$ct['id']." AND ($querynya) $aktf $cust $and) as `".$ct['id']."`";
                                                    }

//$q.=" FROM sv_master_pelanggan a LEFT JOIN sv_master_sales c ON a.sales=c.id WHERE a.pelanggan=".$sales['id']." AND  ($querynya) $aktf $cust $and";
$q.=" FROM sv_master_pelanggan a WHERE a.perusahaan='".$sales['id']."' ";
                                                  // echo $q; 
                                        $sum=$this->db->query($q)->row_array();
                                        
					$drill[$a]['id']=$sales['id'];
					$drill[$a]['title']=$sales['title'];
                                        foreach($act as $ct){
                                            
					$drill[$a][$ct['id']]=$sum[$ct['id']];
                                        }
					?>
					
						{
							name: '<?php echo $sales['title']?>',
							y: <?php echo $sum['tot']?>,
							drilldown: '<?php echo $sales['id']?>'
						},
						<?php 
						$a++;
						} ?>	
					]
                                        <?php //print_mz($drill)?>
                }],
                /*drilldown: {
                    series: [
            <?php foreach($drill as $down){ ?>
					{
                name: '<?php echo $down['title']?>',
                id: '<?php echo $down['id']?>',
                data: [
            <?php 
            
            $wherez['perusahaan']='where/'.$down['id']; 
            //if($this->session->userdata('divisi')) $wherez['type']='where/'.$this->session->userdata('divisi');
             
             $act=GetAll('sv_master_pelanggan',$wherez)->result_array();
             
            foreach($act as $ct){ 
                $qr=$this->db->query("SELECT COUNT(a.id) as tot FROM sv_activity_sales a WHERE a.pelanggan=".$ct['id']."")->row_array();
                if(strpos($ct['title'],"'")){
                    $ct['title']=str_replace("'","\'",$ct['title']);
                }
                ?>
            ['<?php echo $ct['title']?>',  <?php echo $qr['tot'] ?>],<?php //$drill[$a][$ct['title']]=$sum[$ct['title']]; 
                                        }?>
                ]
					},<?php } ?>
			]
                }*/
            });
        }
    );
;


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