<style>
.submit_pdf {
    background-image: url(./assets/images/file-icons/pdf.png);
    border: 0px;
    background-color: #FFFFFF;
    color: transparent;
    float: left;
    margin: 5px 0 10px 0;
    height: 25px;
    width: 25px;
}
.submit_excel {
    background-image: url(./assets/images/file-icons/xlsx.png);
    border: 0px;
    background-color: #FFFFFF;
    color: transparent;
    float: left;
    margin: 5px 0 10px 20px;
    height: 25px;
    width: 25px;
}
.print_chart {
    background-image: url(./assets/images/file-icons/chart.png);
    border: 0px;
    background-color: #FFFFFF;
    color: transparent;
    /*float: left;*/
    margin: 5px 0 10px 5px;
    height: 24px;
    width: 24px;
}
.cht-pie{
    width: 75%;
    margin: 0 auto;
    margin-top: 20px;
    margin-bottom: 60px;
    background: #f3f3f3;
    padding: 30px 30px;
    box-shadow: -1px 20px 7px -18px #717171;
}
.right-col {
    padding-right: 18px;
	min-width: 535px;
}
</style>
<aside class="right-side">
    <section class="content-header">
        <div class="title_bar">
            <h1 class="major_txt_color"><?php echo $title;?></h1>
            <div class="breadcrumb_container"><?php echo breadcrumb_links();?></div>
        </div>
    </section>
    <?php $general = get_general_setting();?>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <!-- /.box -->
                <div class="box box-primary">
                    <div class="box-body table-responsive">
                        <div class="cht-pie">
                            <canvas id="chart-area"></canvas>
                        </div>
                        
                        <form name="get_data_search" action="<?php echo $form_action;?>" method="post" class="search_form validate_form">
                            <input type="hidden" name="hidden_id" class="hidden_id" value="<?php echo (!empty($hidden_id)) ? $hidden_id : '';?>" />
                            
                            <!-- Filtered Columns to send JS - Start -->
                            <input type="hidden" name="hidden_print" class="hidden_print" value="<?php echo (!empty($start_date)) ? 1 : 0 ;?>" />
                            <?php foreach($filters as $key=>$val) : ?>
                                <input type="hidden" name="hidden_filter_array[]" class="hidden_filter_array" value="<?php echo $val;?>" />
                            <?php endforeach;?>
                            <!-- Filtered Columns to send JS - End -->
                             
                            <div class="col-xs-12">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label >Start Date</label>
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <input type="text" class="start_date" placeholder="DD-MM-YYYY" style="padding: 6px 12px;margin-right:5%;width:100%;" id="start_date" name="start_date" value="<?php echo (!empty($start_date)) ? $start_date : date('d-m-Y');?>" autocomplete="off" >
                                            
                                        </div>
                                    </div>
                                    
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label >End Date</label>
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <?php 
                                                $add_days = 6;
                                                $date = (!empty($start_date)) ? date('Y-m-d',strtotime($start_date)) : date('Y-m-d');
                                                $d = date('d-m-Y',strtotime($date) + (24*3600*$add_days));
                                            ?>
                                            <input type="text" class="end_date" placeholder="DD-MM-YYYY" style="padding: 6px 12px;margin-right:5%;width:100%;"  id="end_date" name="end_date" value="<?php echo (!empty($end_date)) ? $end_date : "";?>" autocomplete="off" >
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xs-12">
                                <div class="col-md-2">
                                    <input type="submit" value="Get Summary Data" name="submit" class="btn btn-danger btn-sm" style="background-color: #e33739;
    border-color: #e33739;" />
                                </div>
                                <div class="col-md-2">
                                    <input type="submit" value="Print Summary Data" name="submit" class="btn btn-primary btn-sm" />
                                </div>
                                <div class="col-md-2">
                                    <input type="submit" value="Get Details Data" name="submit" class="btn btn-danger btn-sm" style="background-color: #e33739;
    border-color: #e33739;" />
                                </div>
                                <div class="col-md-2">
                                    <input type="submit" value="Print Details Data" name="submit" class="btn btn-primary btn-sm" />
                                </div>
								<div class="col-md-2">
									<input type="submit" value="Section Wise Report" name="submit" class="btn btn-primary btn-sm" />
								</div>
                                <div class="col-md-2">
                                    <input type="submit" name="submit" value="<?php echo (!empty($pdftype)) ? $pdftype : 'Print Summary Data';?>" class="submit_pdf" title="Export PDF" />
                                    <input type="submit" name="submit" value="<?php echo (!empty($exceltype)) ? $exceltype : 'Print Summary Data';?>" class="submit_excel" title="Export Excel" />
                                    <input type="submit" name="submit" value="<?php echo (!empty($charttype)) ? $charttype : 'Print Summary Data';?>" class="print_chart" title="PDF Chart" />
                                </div>
                            </div>
                            
                            <!-- Filtered Columns to send JS - Start -->
                            <div class="col-xs-12" style="margin-bottom:4%;">
                                <div class="col-xs-12 blck" style="margin-bottom: -35px;">
                                    <div class="sub-title" style="color:#0B45B3; background:#f3f3f3 !important;" ><h4><i class="fa fa-fire" style="font-size: 14px;"></i> Filter Columns</h4><i class="ctrl opener opened fa fa-chevron-up" style="float: right; margin-top: -25px;"></i></div>
                                    <div class="blck-content col-xs-12" style="border-bottom: 1px inset rgba(209, 216, 216, 0.6); background: rgba(3, 12, 11, 0.05); margin-top: -15px;">
                                        <div id="grpChkBox" class="grpChkBox">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- Filtered Columns to send JS - End -->
                            
                        </form>
                    </div>
                    <div class="box-body table-responsive" style="clear:both;">
                        <div>
                            <h3 style="color: #0B45B3; font-size: 20px;"><?php echo $type;?></h3>
                        </div>
                        <div style="max-height: 410px; overflow-y: auto;">
                            <?php if($type == 'Get Summary Data') { ?>
                                <table class="table table-bordered table-striped filterTable" id="filterTable">
                                    <thead>
                                        <tr>
                                            <th width="5%" class="align-center">S.No</th>
                                            <th width="14%" class="align-center">Receipt No</th>
                                            <th width="22%" class="align-center">Name</th>
                                            <th width="10%" class="align-center">Quantity</th>
                                            <th width="12%" class="align-center">Unit Price</th>
											<?php if($general->gst > 0) { ?>
                                                <th width="13%" class="align-center">Before GST</th>
                                                <th width="8%" class="align-center">GST</th>
                                            <?php } ?>
                                            <th width="15%" class="align-center">Total <?php echo ($general->gst > 0) ? '(Incl. of GST)' : '';?></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
										$total_amt 			= 0; 
										$before_gst_total 	= 0;
										$gst_total 			= 0;
                                        $start_date 		= (!empty($start_date)) ? $start_date : date('d-m-Y');
                                        $add_days 			= 6;
                                        $d 					= date('d-m-Y',strtotime($start_date) + (24*3600*$add_days));
                                        $end_date 			= (!empty($end_date)) ? $end_date : $d;
                                        if(count($results) > 0) { ?>
                                            <?php $i = 0;
                                            foreach( $results as $key => $row) :
                                                //Refund Amount
                                                $Refund = $this->weekly_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;
                                                $total_amt += $recentAmt;
                                                if(!empty($row['gst'])) {
                                                    $AmtwithGst = (($recentAmt /107) * 100); //$recentAmt * ($row['gst'] / 100);
													$gst_amt 	= number_format($recentAmt - $AmtwithGst,2,'.','');
                                                } else {
                                                    $AmtwithGst = 0;
													$gst_amt 	= 0;
                                                }
                                            ?>
                                            
                                            <?php 
												$mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
												$auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
												$emp_id = '';												
											?>
                                                <tr>
                                                    <td class="align-center"><?php echo ++$i;?></td>
                                                    <td><?php echo $row['rec_no']; ?></td>
                                                    <td width="25%"><?php echo $row['name']; ?></td>
                                                    <td class="align-center"><?php echo $row['qty'];?></td>
                                                    <td class="align-center"><?php echo $row['price'];?></td>
													<?php if($general->gst > 0) { ?>
                                                        <td class="align-right">
														<?php 
															if(!empty($recentAmt)) {										
																$before_gst 		= number_format($recentAmt - $gst_amt,2,'.','');
																$before_gst_total 	= $before_gst_total + $before_gst;
																echo $before_gst;
															}else{
																echo '0.00';
															}
															?>
														</td>
                                                        <td class="align-right">
														<?php 
															if(!empty($row['gst'])) {									
																$gst_total 	= $gst_total + $gst_amt;
																echo $gst_amt;
															}else{															
																echo '0.00';
															}
															?>
														</td>
													<?php } ?>	
                                                    <td class="align-right"><?php echo number_format($recentAmt,2,'.','');?></td>
                                                </tr>
                                            
                                            <?php endforeach; ?>
                                        <?php } else {?>
                                            <tr>
                                                <td colspan="<?php echo ($general->gst > 0) ? 8 : 6;?>" style="color: #0B6CCB;"><center>No Records Found!</center></td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            <?php } else if($type == 'Get Details Data') {?>
                                <table class="table table-bordered table-striped filterTable" id="filterTable">
                                    <thead>
                                        <tr>
                                            <th width="5%" class="align-center">S.No</th>
                                            <th width="14%" class="align-center">Receipt No</th>
                                            <th width="23%" class="align-center">Category Name</th>
                                            <th width="20%" class="align-center">Sub Category Name</th>
                                            <th width="15%" class="align-center">Payment Type</th>
                                            <th width="14%" class="align-center">Total <?php echo ($general->gst > 0) ? '(Incl. of GST)' : '';?></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php $total_amt = 0;
                                        $start_date = (!empty($start_date)) ? $start_date : date('d-m-Y');
                                        $add_days = 6;
                                        $d = date('d-m-Y',strtotime($start_date) + (24*3600*$add_days));
                                        $end_date = (!empty($end_date)) ? $end_date : $d;
                                        if(count($results) > 0) { ?>
                                            <?php $i = 0; 
                                            foreach( $results as $key => $row) :
                                                //Refund Amount
                                                $Refund = $this->weekly_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;
                                                $total_amt += $recentAmt;
                                            ?>  
                                                <?php $mem_card_id = (!empty($row['mem_card_id'])) ? $row['mem_card_id'] : '';
                                                      $auto_id = (empty($row['mem_card_id'])) ? $row['id'] : '';
                                                $paymentType = $this->weekly_model->get_payment_details($row['type_id'],$auto_id,$start_date,$end_date,$mem_card_id);?>
                                                
                                                <?php 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']; }
                                                
                                                $tooltip = ($recentAmt == 0) ? 'Deleted Record : ' . $row['del_reason'] : '';
                                                ?>
                                                
                                                <tr data-toggle="tooltip" title="<?php echo $tooltip;?>">
                                                    <td class="align-center"><?php echo ++$i;?></td>
                                                    <td><?php echo $row['rec_no']; ?></td>
                                                    <td width="25%"><?php echo $row['cat_name']; ?></td>
                                                    <td><?php echo $row['name'];?></td>
                                                    <td><?php echo $payType;?></td>
                                                    <td class="align-right"><?php echo number_format($recentAmt,2,'.','');?></td>
                                                </tr>
                                            
                                            <?php endforeach; ?>
                                        <?php } else {?>
                                            <tr>
                                                <td colspan="6" style="color: #0B6CCB;"><center>No Records Found!</center></td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            <?php } ?>
                        </div>
                    </div><!-- /.box-body -->
                    
                    <div class="box-body table-responsive">
                        <div class="row">                        	
                            <div class="right-col action-right-col">
                                <table class="table table-bordered table-striped">
								<thead>
									<tr>
										<th width="27%">&nbsp;</th>
										<?php if($before_gst_total > 0){ ?>
										<th width="27%" class="align-center" align="center">Before GST</th>
										<th width="19%" class="align-center" align="center">GST</th>
										<?php } ?>
										<th class="align-right" align="right">Amount</th>
									</tr>
								</thead>
                                <tbody>
                                    <tr>
                                        <td align="left" width="27%">Total S$</td>
										<?php if($before_gst_total > 0){ ?>
										<td align="right" width="27%"><?php echo number_format($before_gst_total,2,'.','');?></td>
										<td align="right" width="19%"><?php echo number_format($gst_total,2,'.','');?></td>
										<?php } ?>
                                        <td align="right"><?php echo number_format($total_amt,2,'.','');?></td>
                                    </tr>
                                    <?php foreach($mode as $key=>$value) { ?>
                                        <?php $paidAmount = $this->weekly_model->get_total_paid_amt_by_type($value->id,$search_date);?>
                                        <?php if($value->payment_mode == 'cash') { 
                                                $refundAmt = $this->weekly_model->get_refund_amount($search_date);
                                            } else { 
                                                $refundAmt = '0.00';
                                            }
                                        ?>
                                        <tr>
                                            <td align="left"><?php echo ucfirst($value->name);?> S$</td>
                                            <td <?php echo (($before_gst_total > 0)? 'colspan="3"' : ''); ?>><?php echo (!empty($paidAmount->amount)) ? (($value->payment_mode == 'cash') ? number_format($paidAmount->amount - $refundAmt,2,'.','') :  $paidAmount->amount ) : '0.00';?></td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                            </div>
                        </div>
                    </div>
                    
                </div><!-- /.box -->
            </div>
        </div>
    </section><!-- /.content -->
</aside><!-- /.right-side -->
<!-- ./wrapper -->

<div class="modal fade" id="pdfModal" role="dialog">
    <div class="modal-dialog modal-lg">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Weekly Summary Report PDF</h4>
        </div>
        <div class="modal-body">
         <iframe src="" class="pdf_iframe" width="850" height="600"></iframe>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
</div>

<div class="modal fade" id="pdfchartModal" role="dialog">
    <div class="modal-dialog modal-lg">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Weekly Chart Report PDF</h4>
        </div>
        <div class="modal-body">
         <iframe src="" class="pdf_iframe" width="850" height="600"></iframe>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
</div>

<div class="modal fade" id="pdfsectiontModal" role="dialog">
    <div class="modal-dialog modal-lg">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Weekly Section Wise Report PDF</h4>
        </div>
        <div class="modal-body">
         <iframe src="" class="pdf_iframe" width="850" height="600"></iframe>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
</div>
<?php 
//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,".","")."'";
		}
	}		
}
?>
<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');
var myChart = new Chart(ctx, {
  type: 'bar',
  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; ?>]
     
    }]
    
  },
  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
          responsive: true,
          legend: false,
		  scales: {
				yAxes: [{
					ticks: {
						beginAtZero: true
					}
				}]
			}
        }
});
};
    
    $(document).ready(function() {
		<?php if(isset($_SESSION['pdf_weekly'])): ?>
        var srcval='<?php echo base_url().'uploads/weekly_pdf/'.$_SESSION['pdf_weekly_file']; ?>';
        $('.pdf_iframe').attr("src",srcval);
        $('#pdfModal').modal({"show":true}); 
      <?php 
      unset($_SESSION['pdf_weekly']);
      endif; ?>
	  
	  <?php if(isset($_SESSION['pdf_chart'])): ?>
        var srcval='<?php echo base_url().'uploads/weekly_pdf/'.$_SESSION['pdf_chart_file']; ?>';
        $('.pdf_iframe').attr("src",srcval);
        $('#pdfchartModal').modal({"show":true}); 
      <?php 
	  $_SESSION['pdf_chart'] = '';
      unset($_SESSION['pdf_chart']);
      endif; ?>  
	  
	  <?php if(isset($_SESSION['pdf_section'])): ?>
		var srcval='<?php echo base_url().'uploads/weekly_pdf/'.$_SESSION['pdf_section_file']; ?>';
		$('.pdf_iframe').attr("src",srcval);
		$('#pdfsectiontModal').modal({"show":true}); 
		<?php 
		$_SESSION['pdf_section'] = '';
		unset($_SESSION['pdf_section']);
		endif; 
		?>
	  
        $('.start_date').bootstrapMaterialDatePicker({ 
            time: false, 
            format: 'DD-MM-YYYY'
        }).on('change', function(){
            if($('.end_date').val() != '') {
                $('.end_date').val('');
            }
            change_todate();
        });
        
        $('.end_date').bootstrapMaterialDatePicker({ 
            time: false, 
            format: 'DD-MM-YYYY'
        });
        change_todate();
    });
   
    function change_todate() {
        if (($('.start_date').val() != '') && ($('.start_date').val()!= undefined)) {
            var start_date_arr = $('.start_date').val().split('-');
            var start_date_obj = new Date(start_date_arr[2], (start_date_arr[1] - 1), start_date_arr[0]);
            var end_date_start_obj = new Date(start_date_obj.getTime() + (6 * 24 * 60 * 60 * 1000));
            var start_date = (end_date_start_obj.getDate()) + '-' + (end_date_start_obj.getMonth() + 1) + '-' + end_date_start_obj.getFullYear();
            $('.end_date').bootstrapMaterialDatePicker('setMinDate', start_date);
        }
    }
</script>