<?php
    $CI =& get_instance();
    //Earning graph
    $CI->load->model('Retainedearning');
    $earning[$this->lang->line('lastYear')] = $CI->Retainedearning->incomeNexpenses($lastYear,$lastPeriod,1);
    for($i = $start; $i <= $end; $i++){
      $earning[$this->lang->line('monthSrt'.$i).$year] = $CI->Retainedearning->incomeNexpenses($year,$i,2);
    }
?>
<div class="row-fluid scRow">
    <!-- Company Profit and Loss -->
    <div class="span12 scCol">
        <div class="block">
            <div class="head">                                        
            	<h2><?php echo $this->lang->line('profitNloss'); ?></h2>
            	<ul class="buttons">
            		<li><a href="#" class="block_loading" onclick="drawChart()"><span class="i-cycle"></span></a></li>
            		<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
            	</ul>
            </div>
            <div class="content">
            	<div id="toolbar_div"></div>
            	<div id="barChart1" style=" margin: 0px auto;"></div>
            </div>
        </div>
    </div>
</div>

<script>
	
	   google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(barChart1);
    function barChart1() {
      var data = google.visualization.arrayToDataTable([
          ['Year', "Income","Expense","Earning"],
          <?php
		  	if(!empty($earning)){
		  		foreach($earning as $key=>$v){
					echo "['".$key."', ".number_format($v['income'],2,'.','').", ".number_format($v['expense'],2,'.','').", ".number_format($v['income']+$v['expense'],2,'.','')."],";
				}
			}
			else{
				echo "[test,0]";
			}
		  ?>
        ]);

        var options = {
          title : '<?php echo $this->lang->line('profitNloss'); ?>',
          seriesType: 'bars',
          series: {2: {type: 'line'}},
          height:400,
          isStacked: true,
          legend: {position: 'top', maxLines: 3},
        };

        var chart = new google.visualization.ComboChart(document.getElementById('barChart1'));
        chart.draw(data, options);
        //drawToolbar();
  	}

  	function drawToolbar() {
      var components = [
          {type: 'igoogle', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA',
           gadget: 'https://www.google.com/ig/modules/pie-chart.xml',
           userprefs: {'3d': 1}},
          {type: 'html', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA'},
          {type: 'csv', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA'},
          {type: 'htmlcode', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA',
           gadget: 'https://www.google.com/ig/modules/pie-chart.xml',
           userprefs: {'3d': 1},
           style: 'width: 800px; height: 700px; border: 3px solid purple;'}
      ];

      var container = document.getElementById('toolbar_div');
      google.visualization.drawToolbar(container, components);
    };

    $(window).resize(function(){
        console.log('um here');
        barChart1();
    });
</script>