<style>
::-webkit-scrollbar {display:none;}
.page_loader {
     position: fixed;
     left: 0px;
     top: 0px;
     width: 100%;
     height: 100%;
     z-index: 9999;
     background: url('<?php echo base_url(); ?>assets/images/loader.gif') 50% 50% no-repeat rgb(249, 249, 249);
 }

</style>
<!--<div class="loader"></div>-->
<div class="page_loader"></div>
<?php 
$logo_url 			= !empty($data['settings']['logo']) && file_exists(DIR_COMPANY_LOGO. $data['settings']['logo']) ? DIR_COMPANY_LOGO . $data['settings']['logo'] : 'assets/images/logo.png';

if($data['type'] == 'Chart Summary Data') { 
	$search_date = (!empty($data['search_date'])) ? $data['search_date'] : date('d-m-Y');
	if(count($data['results']) > 0) { 
		foreach( $data['results'] as $key => $row) :
			$Refund = $this->daily_summary_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$search_date);
			  
			if(!empty($Refund)) {
				$recentAmt = $row['amount'] - $Refund;
			}
			else {
			   $recentAmt = $row['amount'];
			}
			
			$chartamout[$row['type_id']] += $recentAmt;
		 endforeach;
	 } 
} else if($data['type'] == 'Chart Details Data') {
	$search_date = (!empty($data['search_date'])) ? $data['search_date'] : date('d-m-Y');
	if(count($data['results']) > 0) {											 
		foreach( $data['results'] as $key => $row) :
			$Refund = $this->daily_summary_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$search_date);
			  
			if(!empty($Refund)) {
				$recentAmt = $row['amount'] - $Refund;
			}
			else {
			   $recentAmt = $row['amount'];
			}
			$chartamout[$row['type_id']] += $recentAmt;
		endforeach; 
	}	
} 
$calArray	= $chartamout;
arsort($calArray);
if($calArray){
	$i = 0;
	foreach($calArray as $greater){
	  if($i == 1)
	  	continue;
		
	  if($i == 0){
		  $num 		= (int)((int)$greater/6);
		  $round 	= (strlen($num)-1)*-1;
		  $stepSize =  round($num,$round); // prints 200
	  }
	  $i++;
	}
}else{
	$num 		= 20;
	$round 		= (strlen($num)-1)*-1;
	$stepSize 	=  round($num,$round); 
}
?>
<div id="print_content">
    <table width="100%" style="font-family:Verdana, Geneva, sans-serif">
        <tr>
            <td width="25%">
                    <img src="<?php echo $logo_url; ?>"/>
            </td>
            <td width="50%" align="center">
                <table border="0" width="100%">
                    <tr>
                        <td align="center" style="font-size:24px;">
                            <strong><?php echo $data['settings']['temple_name']; ?></strong>
                        </td>
                    </tr>
                </table>
            </td>
            <td width="25%">&nbsp;
                   
            </td>
        </tr>
        <tr><td colspan="3"><hr></td></tr>
    </table>
    <table width="100%" border="0" cellpadding="5" style="font-family:Verdana, Geneva, sans-serif">
        <tr>
            <td width="25%">&nbsp;</td>
            <td align="center" width="50%" style="font-weight:bold; font-size:24px;">
            <?php echo 'Daily Chart Report'. ' - '. date('d/m/Y', strtotime($data['search_date'])); ?>
            </td>
            <td width="25%" style="font-size:16px;">
            &nbsp;<?php //echo "Printed On: ".$user_data['name']."-".date("Y-m-d H:i:s"); ?>
            </td>
        </tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
        <tr><td colspan="3">&nbsp;</td></tr>
    </table>
    <canvas id="chart-area"></canvas>
</div>
<?php 
//echo "<pre>"; print_r($chartamout); echo "</pre>"; 
$servicess 	= service_type_name(); 
$label 		= '';
$dataval 	= '';

if($chartamout){
	foreach($chartamout as $key => $amount){
		if(empty($label)){
			$label = "'".$servicess[$key]."'";
		}else{
			$label .= ",'".$servicess[$key]."'";
		}
		
		if(empty($dataval)){
			$dataval = "'".number_format($amount,2,".","")."'";
		}else{
			$dataval .= ",'".number_format($amount,2,".","")."'";
		}
	}		
}
?>
<script type="text/javascript" src="<?php echo base_url().'assets/js/'; ?>library/jquery-1.11.1.min.js"></script>
<script src="<?php echo base_url().'assets/js/Chart.min.js'?>"></script>
<script src="<?php echo base_url().'assets/js/jspdf.min.js'; ?>"></script>
<script src="<?php echo base_url().'assets/js/html2canvas.min.js'; ?>"></script>
<script>
window.onload = function() {
var ctx = document.getElementById("chart-area").getContext('2d');
     Chart.defaults.global.defaultFontColor = "#ff0000";
     Chart.defaults.global.defaultFontSize = "18";
     Chart.defaults.global.defaultFontStyle = "bolder";
debugger;

var data= {
    labels: [<?php echo $label; ?>],
    datasets: [{
      backgroundColor: [
        "#ff4700",
        "#FFA07A",
        "#bc00bf",
        "#FA8072",
        "#E9967A",
        "#E74C3C",
        "#34495E",
        "#bf8400",
        "#148F77",
        "#dd0000",
        "#0093f4",
		"#B22222",
		'#FFD700',
		'#FFEFD5',
		'#FFE4B5',
		'#7CFC00',
		'#00FF7F',
		'#98FB98',
		'#808000',
		'#00FFFF',
		'#008B8B'       
      ],
      //label: '',
      data: [<?php echo $dataval; ?>]
     
    }]
    
  }
var myChart = new Chart(ctx, {
  type: 'bar',
  data: data,
  options: {
            
//			  tooltips: {
//			  callbacks: {
//					label: function(tooltipItem, data) {
//						var value = data.datasets[0].data[tooltipItem.index];
//						if(parseInt(value) >= 1000){
//						   return 'S$ ' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
//						} else {
//						   return 'S$ ' + value;
//						}
//					}
//			  } // end callbacks:
//			}, //end tooltips 
      "hover": {
                    "animationDuration": 0
                },
                "animation": {
                  "duration": 1,
                    
                  "onComplete": function() {
                      
                    var chartInstance = this.chart,
                      ctx = chartInstance.ctx;
                    ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontColor, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily, Chart.defaults.global.defaultFontWeight);
                    ctx.textAlign = 'center';
                    ctx.textBaseline = 'bottom';

                    this.data.datasets.forEach(function(dataset, i) {
                      var meta = chartInstance.controller.getDatasetMeta(i);
                      meta.data.forEach(function(bar, index) {
                        var data = "S$"+dataset.data[index];
//                          this.style.("font-weight","bolder");
                        ctx.fillText(data, bar._model.x, bar._model.y - 3);
                      });
                    });
                  }
                },
        tooltips: {
              "enabled": false
            },
          responsive: true,
         
          legend: false,
       fontColor: 'black',
         
		   scales: {
                  yAxes: [{
                    display: true,
                    gridLines: {
                      display: false
                    },
                    ticks: {
                      //max: Math.max(...data.datasets[0].data) + 50,
					  stepSize: <?php echo $stepSize; ?>,
                      display: true,
                      beginAtZero: true
                    }
                  }],
                  xAxes: [{
                    gridLines: {
                      display: true
                    },
                    ticks: {
                      beginAtZero: true
                    }
                  }]
               
                }
        }
});
};

function saveAsPDF() {
   html2canvas(document.getElementById("print_content"), {
      onrendered: function(canvas) {
         var img = canvas.toDataURL(); //image data of canvas
         var doc = new jsPDF();
         doc.addImage(img, 0, 2, 210, 150);
		var daily_chart_summary_report = doc.output('blob');
		var formData = new FormData();
		formData.append('pdf', daily_chart_summary_report);
		$.ajax('<?php echo base_url().'daily_summary_report/uploadchartpdf';?>',
		{
			method: 'POST',
			data: formData,
			processData: false,
			contentType: false,
			success: function(data){window.location.href = '<?php echo base_url().'daily_summary_report';?>';}
		});
      }
   });
}

setTimeout(function(){ saveAsPDF(); }, 1);
</script>