<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>
<canvas id="chart-area" width="210" height="70"></canvas>
<?php $general = get_general_setting();?>
<?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') {
	$total_amt 	= 0;
	$start_date = (!empty($data['start_date'])) ? $data['start_date'] : date('d-m-Y');
	$add_days 	= 27;
	$d 			= date('d-m-Y',strtotime($start_date) + (24*3600*$add_days));
	$end_date 	= (!empty($data['end_date'])) ? $data['end_date'] : date('d-m-Y');
	if(count($data['results']) > 0) { 
		foreach( $data['results'] as $key => $row) :
			//Refund Amount
			$Refund = $this->monthly_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$start_date,$end_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') {
	$total_amt 	= 0;
	$start_date = (!empty($data['start_date'])) ? $data['start_date'] : date('d-m-Y');
	$add_days 	= 27;
	$d 			= date('d-m-Y',strtotime($start_date) + (24*3600*$add_days));
	$end_date 	= (!empty($data['end_date'])) ? $data['end_date'] : date('d-m-Y');
	if(count($data['results']) > 0) { 
		foreach( $data['results'] as $key => $row) :
			//Refund Amount
			$Refund = $this->monthly_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$start_date,$end_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);
}
$html = '<div id="print_content">
    <table width="100%" style="font-family:Verdana, Geneva, sans-serif">
        <tr>
            <td width="25%">
                    <img src="'.$logo_url.'"/>
            </td>
            <td width="50%" align="center">
                <table border="0" width="100%">
                    <tr>
                        <td align="center" style="font-size:18px;">
                            <strong>'.$data['settings']['temple_name'].'</strong>
                        </td>
                    </tr>
                </table>
            </td>
            <td width="25%">&nbsp;</td>
        </tr>
        <tr><td colspan="3"><hr></td></tr>    
        <tr>
            <td width="25%">&nbsp;</td>
            <td align="center" width="50%" style="font-weight:bold; font-size:18px;">
            Monthly Report
            </td>
            <td width="25%" style="font-size:14px;">
            &nbsp;
            </td>
        </tr>
        <tr>
            <td align="left" style="font-size:14px;"><strong>Start Date : '.date('d-m-Y',strtotime($start_date)).'</strong></td>
            <td>&nbsp;</td>
            <td align="right" style="font-size:14px;"><strong>End Date : '.date('d-m-Y',strtotime($end_date)).'</strong></td>
        </tr>
		<tr><td colspan="3">&nbsp;</td></tr>
		<tr><td colspan="3"><img id="myImage" /></td></tr>
    </table>  ';
    
	if($data['dataType'] == 'Chart Summary Data') { 
		$html .= '<table width="100%" border="1" cellpadding="3" cellspacing="0" style="font-family:Trebuchet MS, Arial, Helvetica, sans-serif; font-size:14px; width: 100%; border-collapse:collapse;" class="filterTable" id="filterTable">
			<thead>
				<tr>
					<th style="padding:0.6%; width:7%;">S.No</th>
					<th style="padding:0.6%; width:17%;">Receipt No</th>
					<th style="padding:0.6%; width:44%">Name</th>
					<th style="padding:0.6%; width:10%">Qty</th>
					<th style="padding:0.6%; width:9%">U.Price</th>';
					if($general->gst > 0) {
						$html .= '<th style="padding:0.6%; width:13%">Before GST</th>
						<th style="padding:0.6%; width:9%">GST</th>';
					}
		 $html .= '<th style="padding:0.6%; width:13%">Total</th>
				</tr>
			</thead>
			<tbody>';
				$total_amt 			= 0;
				$before_gst_total 	= 0;
				$gst_total 			= 0;
				$start_date 		= (!empty($data['start_date'])) ? $data['start_date'] : date('d-m-Y');
				$add_days 			= 27;
				$d 					= date('d-m-Y',strtotime($start_date) + (24*3600*$add_days));
				$end_date 			= (!empty($data['end_date'])) ? $data['end_date'] : $d;
				if(count($data['results']) > 0) { 
					 $i = 0;
					foreach( $data['results'] as $key => $row) :
						//Refund Amount
						$Refund = $this->monthly_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$start_date,$end_date);
						if(!empty($Refund)) {
							$recentAmt = $row['amount'] - $Refund;
						}else {
						   $recentAmt = $row['amount'];
						}						
						$total_amt += $recentAmt;
						if(!empty($row['gst'])) {
							$AmtwithGst = $recentAmt * ($row['gst'] / 100);
							$gst_amt 	= number_format($AmtwithGst,2,'.','');
						} else {
							$AmtwithGst = 0;
							$gst_amt 	= 0;
						} // not necessary to send auto id
						
						$mem_card_id 	= (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
						$auto_id 		= (empty($row['mem_card_id'])) ? $row['id'] : '';
						$emp_id 		= '';
					
						
						$html .= '<tr>
									<td align="center" style="padding:0.6%;">'.++$i.'</td>
									<td style="padding:0.6%;">'.$row['rec_no'].'</td>
									<td width="20%" style="padding:0.6%;">'.$row['name'].'</td>
									<td align="center" style="padding:0.6%;">'.$row['qty'].'</td>
									<td align="center" style="padding:0.6%;">'.$row['price'].'</td>';
									if($general->gst > 0) {
										if(!empty($recentAmt)) {										
											$before_gst 		= number_format($recentAmt - $gst_amt,2,'.','');
											$before_gst_total 	= $before_gst_total + $before_gst;
										}else{
											$before_gst 		= '0.00';
										}
										
										if(!empty($row['gst'])) {									
											$gst_total 	= $gst_total + $gst_amt;									
										}
											$html .= '<td align="right" style="padding:0.6%;">'.$before_gst.'</td>
														<td align="right" style="padding:0.6%;">'.$gst_amt.'</td>';
									}							
					   $html .= '<td align="right" style="padding:0.6%;">'.number_format($recentAmt,2,'.','').'</td>
								</tr>';
					
					endforeach;
				} else {
					$html .= '<tr>
						<td colspan="'.(($general->gst > 0) ? 8 : 6).'" style="color: #0B6CCB;"><center>No Records Found!</center></td>
					</tr>';
				}
			$html .= '</tbody>
		</table>';
	 } else if($data['dataType'] == 'Chart Details Data') {
		$html .= '<table width="100%" border="1" cellpadding="3" cellspacing="0" style="font-family:Trebuchet MS, Arial, Helvetica, sans-serif; font-size:14px; width: 100%; border-collapse:collapse;" class="filterTable" id="filterTable">
			<thead>
				<tr>
					<th width="5%" align="center" style="padding:0.6%;">S.No</th>
					<th width="14%" align="center" style="padding:0.6%;">Receipt No</th>
					<th width="23%" align="center" style="padding:0.6%;">Category Name</th>
					<th width="20%" align="center" style="padding:0.6%;">Sub Category Name</th>
					<th width="15%" align="center" style="padding:0.6%;">Payment Type</th>
					<th width="14%" align="center" style="padding:0.6%;">Total</th>
				</tr>
			</thead>
			<tbody>';
				$total_amt = 0;
				$start_date = (!empty($data['start_date'])) ? $data['start_date'] : date('d-m-Y');
				$add_days = 27;
				$d = date('d-m-Y',strtotime($start_date) + (24*3600*$add_days));
				$end_date = (!empty($data['end_date'])) ? $data['end_date'] : $d;
				if(count($data['results']) > 0) { 
					$i = 0; 
					foreach( $data['results'] as $key => $row) :
						//Refund Amount
						$Refund = $this->monthly_model->get_refund_amt_by_ids($row['type_id'],$row['service_id'],$row['id'],$start_date,$end_date);
                                              
						if(!empty($Refund)) {
							$recentAmt = $row['amount'] - $Refund;
						}else {
						   $recentAmt = $row['amount'];
						}						
						$total_amt += $recentAmt;
					
						$mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
						$auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
						$paymentType = $this->monthly_model->get_payment_details($row['type_id'],$auto_id,$start_date,$end_date,$mem_card_id);
					 
						if((!empty($row['amount_type'])) && $row['amount_type'] == 2) {
							$payType = '-';
						} elseif((!empty($row['amount_type'])) && $row['amount_type'] != 2){
							$payType = $paymentType[0]['payment'];
						} else { 
							$payType = $paymentType[0]['payment']; 
						}
						
						$html .= '<tr>
									<td align="center" style="padding:0.6%;">'.++$i.'</td>
									<td style="padding:0.6%;">'.$row['rec_no'].'</td>
									<td width="25%" style="padding:0.6%;">'.$row['cat_name'].'</td>
									<td style="padding:0.6%;">'.$row['name'].'</td>
									<td style="padding:0.6%;">'.$payType.'</td>
									<td align="right" style="padding:0.6%;">'.number_format($recentAmt,2,'.','').'</td>
								</tr>';
					
					endforeach;
				} else {
					$html .= '<tr>
						<td colspan="6" style="color: #0B6CCB;"><center>No Records Found!</center></td>
					</tr>';
				}
			$html .= '</tbody>
		</table>';
	}

	$html .= '<br/>
	
					<table width="59%" align="right" border="1" cellpadding="3" cellspacing="0" style="Trebuchet MS, Arial, Helvetica, sans-serif; font-size:14px; border-collapse:collapse;" class="filterTable" id="filterTable">
						<tr><td align="left" style="padding:0.6%;border:1px solid #000; width: 190px;">Total S$</td>';
						if($before_gst_total > 0){
							$html .= '<td align="right" style="padding:0.6%; width:25%;">'.number_format($before_gst_total,2,'.','').'</td>
							<td align="right" style="padding:0.6%; width:11%;">'.number_format($gst_total,2,'.','').'</td>';
						 }
							$html .= '<td style="padding:0.6%; width:25%;" align="right">'. number_format($total_amt,2,'.','').'</td>							
						</tr>';
						foreach($data['mode'] as $key=>$value) {
							$paidAmount = $this->monthly_model->get_total_paid_amt_by_type($value->id,$start_date,$end_date);
							if($value->payment_mode == 'cash') { 
								$refundAmt = $this->monthly_model->get_refund_amount($start_date,$end_date);
							} else { 
								$refundAmt = '0.00';
							}
							$html .= '<tr>                                                        
								<td align="left" style="padding:0.6%;border:1px solid #000;">'.ucfirst($value->name).' S$</td>';
								if($before_gst_total > 0){
									$html .= '<td align="right" colspan="2" style="padding:0.6%;border-left:0px;">&nbsp;</td>';
								}
								$html .= '<td align="right" style="padding:0.6%;border:1px solid #000;">'.((!empty($paidAmount->amount)) ? (($value->payment_mode == 'cash') ? number_format($paidAmount->amount - $refundAmt,2,'.','') :  $paidAmount->amount ) : '0.00').'</td>
							</tr>';
						}
					 $html .= '</table>

</div>';
//echo "<pre>"; print_r($chartamout); echo "</pre>";  
$servicess 	= service_type_name(); 
$label 		= '';
$dataval 	= '';

if($chartamout){
	$barchart_label	= array();
	$barchart_data	= array();
	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,".","")."'";
		}
	}		
}
?>
<div id="htmldata"><?php echo $html; ?></div>
<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 type="text/javascript">
window.onload = function() {
var ctx = document.getElementById("chart-area").getContext('2d');
     Chart.defaults.global.defaultFontColor = "#ff0000";
     Chart.defaults.global.defaultFontSize = "14";
     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: {
      "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);
                      });
					  
					  document.querySelector('#myImage').setAttribute('src', myChart.toBase64Image());
					   var value = $('#htmldata').html(); 				    
					   var url = '<?php echo base_url(); ?>monthly_report/generate_chart_report';
					   $.post( url, { variable: value}, function( resdata ){
						  //main_image.attr('src', data);  
						  window.location.href = '<?php echo base_url().'monthly_report'; ?>';
					   });
                    });
                  }
                },
        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
                    }
                  }]
               
                }
        }
});
};
</script>