<?php

    $fromdate= str_replace('/', '-',$fromdate);
    $from_date= date("Y-m-d", strtotime($fromdate));
    $todate= str_replace('/', '-',$todate);
    $end_date= date("Y-m-d", strtotime($todate));
    $month=date("m",strtotime($fromdate));
    $month_1=date("m",strtotime($todate));
    $year=date("Y",strtotime($todate));
    $leadextra_cndt='';
    if(Yii::app()->user->authorised_user != 1 && User::isSalesman()=='Y'){
        $leadextra_cndt =' AND lead.lead_owner_id='.Yii::app()->user->id;
    }
     $graph_data_quantity = Yii::app()->db->createCommand('SELECT CONCAT(user.first_name," ",user.last_name) AS x_value,COUNT(*) AS y_value FROM {{lead}} AS lead INNER JOIN '.MasCompany::MainDB.'.{{user}} AS user ON lead.lead_owner_id=user.id WHERE lead.enq_date BETWEEN "'.$from_date.'" AND "'.$end_date.'"  '.$leadextra_cndt.'  GROUP BY user.id')->queryAll();
    if(!empty($graph_data_quantity)) {
        $plot = '[';
        foreach($graph_data_quantity as $eachdata ){
          $color = substr(md5(rand()), 0, 6);
          $plot .= '{label:"'.$eachdata['x_value'].'", value:"'.$eachdata['y_value'].'", color: "#'.$color.'"},';
        }
        $plot .= ']';
    }else{
        $plot='[]';
    }
     
  //Country
        $graphdata = Yii::app()->db->createCommand('SELECT COUNT(*) AS value,emirates.emirates_name AS label FROM {{lead}} AS lead INNER JOIN {{customer}} AS customer ON lead.customer_id=customer.id INNER JOIN {{mas_emirates_list}} AS emirates ON customer.customer_emirates_name=emirates.id WHERE lead.enq_date BETWEEN "'.$from_date.'" AND "'.$end_date.'" '.$leadextra_cndt.'  GROUP BY customer.customer_emirates_name
 ')->queryAll();
     if(!empty($graphdata)) {
            $plot_1 = '[';
         foreach($graphdata as $eachdata ){
             $color = substr(md5(rand()), 0, 6);
             $plot_1 .= '{label:"'.$eachdata['label'].'", value:"'.$eachdata['value'].'", color: "#'.$color.'"},';
         }
         $plot_1 .= ']';
     }else{
         $plot_1 .= '[]';
     }
    $graphdata2 = Yii::app()->db->createCommand('SELECT sales_group.group_name AS label,COUNT(*) AS value FROM {{lead}} AS lead INNER JOIN {{mas_sales_group}} AS sales_group ON lead.sales_group_id=sales_group.id WHERE lead.enq_date BETWEEN "'.$from_date.'" AND "'.$end_date.'" '.$leadextra_cndt.'  GROUP BY sales_group.id')->queryAll();
    $plot_2 = '[';
    foreach($graphdata2 as $eachdata ){
         $color = substr(md5(rand()), 0, 6);
         $plot_2 .= '{label:"'.$eachdata['label'].'", value:"'.$eachdata['value'].'", color: "#'.$color.'" },';
     }
     $plot_2 .= ']';

    $currentYear  = date("Y");
    $currentmonth  = date("m");
    if($year==$currentYear){
        $month_1=$currentmonth;
    }
    $plot_4 = '[';
    for ($k=$month;$k<=$month_1;$k++) {
        $dateObj   = DateTime::createFromFormat('!m', $k);
        $monthName = $dateObj->format('F'); // March
        $graphdata3 = Yii::app()->db->createCommand('SELECT COUNT(id) AS value FROM {{lead}} AS lead  WHERE YEAR(enq_date) = "'.$year.'" AND MONTH(enq_date) = "'.$k.'"  '.$leadextra_cndt.' ')->queryAll();
        
        foreach($graphdata3 as $eachdata ){
             $color = substr(md5(rand()), 0, 6);
             $plot_4 .= '{label:"'.$monthName.'", value:'.$eachdata['value'].', color: "#'.$color.'" },';
         }
        
    }
     $plot_4 .= ']';
     
     $end_year= date("Y", strtotime($todate));
                  
?>
<div class="clearfix"></div>
<br>
<div class="clearfix">
    <div class="col-lg-3" data-initial-class="col-lg-3" id="pieChartParent">
        <section class="panel">
            <header class="panel-heading">
                Lead Owner
            </header>
            <div class="panel-body text-center">
                <div class="clearfix canvas-holder">
                    <?php if (isset(Yii::app()->user->havePermissionOn['dashboard_dril_level1_view']) ||
                                                    Yii::app()->user->authorised_user == 1) {
                                                ?>
                    <canvas id="myChart" width="400" height="400" onclick="loadPopupModal('dashboard/leadgraphdetails&type=1&from_date=<?php echo $fromdate; ?>&end_date=<?php echo $todate?>','Lead- Lead Owner','lg');"></canvas>
                    <?php }else{ ?>
                    <canvas id="myChart" width="400" height="400"></canvas>
                    <?php } ?>
                </div>
            </div>
        </section>
    </div>
    <div class="col-lg-3" data-initial-class="col-lg-3" id="pieChartParent_1">
        <section class="panel">
            <header class="panel-heading">
                Emirates
            </header>
            <div class="panel-body text-center">
                <div class="clearfix canvas-holder">
                    <?php if (isset(Yii::app()->user->havePermissionOn['dashboard_dril_level1_view']) ||
                                                    Yii::app()->user->authorised_user == 1) {
                                                ?>
                    <canvas id="myChart_1" width="400" height="400" onclick="loadPopupModal('dashboard/leadgraphdetails&type=2&from_date=<?php echo $fromdate; ?>&end_date=<?php echo $todate?>','Lead- Emirates','lg');"></canvas>
                    <?php }else{ ?>
                    <canvas id="myChart_1" width="400" height="400"></canvas>
                    <?php } ?>
                </div>
            </div>
        </section>
    </div>
    <div class="col-lg-3" data-initial-class="col-lg-3" id="pieChartParent_2">
        <section class="panel">
            <header class="panel-heading">
                Sales Group
            </header>
            <div class="panel-body text-center">
                <div class="clearfix canvas-holder">
                    <?php if (isset(Yii::app()->user->havePermissionOn['dashboard_dril_level1_view']) ||
                                                    Yii::app()->user->authorised_user == 1) {
                                                ?>
                    <canvas id="myChart_2" width="400" height="400" onclick="loadPopupModal('dashboard/leadgraphdetails&type=3&from_date=<?php echo $fromdate; ?>&end_date=<?php echo $todate?>','Lead- Sales Group','lg');"></canvas>
                    <?php }else{ ?>
                    <canvas id="myChart_2" width="400" height="400"></canvas>
                    <?php } ?>
                    
                    
                </div>
            </div>
        </section>
    </div>

    <div class="col-lg-3" data-initial-class="col-lg-3" id="pieChartParent_yr">
        <section class="panel">
            <header class="panel-heading">
                Months
            </header>
            <div class="panel-body text-center">
                <div class="clearfix canvas-holder">
                    <?php if (isset(Yii::app()->user->havePermissionOn['dashboard_dril_level1_view']) ||
                                                    Yii::app()->user->authorised_user == 1) {
                                                ?>
                    <canvas id="myChart_yr" width="400" height="400" onclick="loadPopupModal('dashboard/leadgraphdetails&type=4&from_date=<?php echo $fromdate; ?>&end_date=<?php echo $todate?>','Leads- <?php echo $end_year; ?>','lg');"></canvas>
                    <?php }else{ ?>
                    <canvas id="myChart_yr" width="400" height="400"></canvas>
                    <?php } ?>
                    
                </div>
            </div>
        </section>
    </div>
</div>


<script type="text/javascript">
    $(function(){
//        var data1 = [
//            { value : 300, color : "#F7464A", highlight : "#FF5A5E", label : "Red"},
//            { value : 50, color : "#46BFBD", highlight : "#5AD3D1", label : "Green"},
//            { value : 100, color : "#FDB45C", highlight : "#FFC870", label : "Yellow"}
//        ];
var data1=<?php echo $plot;?>;
var data2=<?php echo $plot_1;?>;
var data3=<?php echo $plot_2;?>;
var order_data4 =<?php echo $plot_4;?>;
        var canvas1 =document.getElementById("myChart");
        Chart.defaults.global.responsive = true;
        var ctx = canvas1.getContext("2d");
        var pieChart = new Chart(ctx).Pie(data1);
        
         var canvas_1 =document.getElementById("myChart_1");
        Chart.defaults.global.responsive = true;
        var ctx_1 = canvas_1.getContext("2d");
        var pieChart_1 = new Chart(ctx_1).Pie(data2);
        
         var canvas_2 =document.getElementById("myChart_2");
        Chart.defaults.global.responsive = true;
        var ctx_2 = canvas_2.getContext("2d");
        var pieChart_2 = new Chart(ctx_2).Pie(data3);

         var canvas_3 =document.getElementById("myChart_yr");
        Chart.defaults.global.responsive = true;
        var ctx_3 = canvas_3.getContext("2d");
        var pieChart_3 = new Chart(ctx_3).Pie(order_data4);
    });
    var c_size={
        //md:"col-xs-6 col-xs-offset-3",
      //  lg:"col-xs-8 col-xs-offset-2",
       // xl:"col-xs-12"
    }
    var returnNextItem= function(item){
        switch(item) {
            case "md":
                return "lg";
                break;
            case "lg":
                return "xl";
                break;
            case "xl":
                return "init";
                break;
            default:
                return "md";
        }
    }
    var growChart = function(id){
        var $el=$(id);
        var cs= returnNextItem($el.attr('data-size'));
        $el.attr('data-size',cs);
        if(cs!="init"){
            $el.attr('class',c_size[cs]);
        }else if(cs=="init"){
            $el.attr('class',$el.attr('data-initial-class'));
        }
        resizeWindow();
    }
</script>