<?php $setting = $this->common->getsData('web_setting',array('id'=>1)); ?>
<?php  
	if(isset($_GET['show_by']) && $_GET['show_by']==2){
		$show_by = 2;
		$sd_start = date('Y-m-d',strtotime('-7 days'));
		$sd_end = date('Y-m-d');
	}else if(isset($_GET['show_by']) && $_GET['show_by']==3){
		$show_by = 3;
		$sd_start = date('Y-m-d',strtotime('-30 days'));
		$sd_end = date('Y-m-d');
	}else if(isset($_GET['show_by']) && $_GET['show_by']==4){
		$show_by = 4;
		$sd_start = date('Y-m-d',strtotime('-365 days'));
		$sd_end = date('Y-m-d');
	}else{
		$show_by = 1;
		$sd_start = date('Y-m-d');
		$sd_end = date('Y-m-d');
	}

	$ocount = $this->db->query('select count(id) as id from orders where date(order_time)>="'.$sd_start.'" and date(order_time)<="'.$sd_end.'"')->result_array();
	if(isset($ocount[0]['id']) && $ocount[0]['id']){
		$torders = $ocount[0]['id'];
	}else{
		$torders = 0;
	}

	$scount = $this->db->query('select sum(grandtotal) as gt from orders where date(order_time)>="'.$sd_start.'" and date(order_time)<="'.$sd_end.'" and order_status!=5')->result_array();
	
	if(isset($scount[0]['gt']) && $scount[0]['gt']){
		$tsale = number_format($scount[0]['gt'],2);
	}else{
		$tsale = 0;
	}

	$ucount = $this->db->query('select count(id) as id from user where date(reg_date)>="'.$sd_start.'" and date(reg_date)<="'.$sd_end.'" and user_type="register_user"')->result_array();
	if(isset($ucount[0]['id']) && $ucount[0]['id']){
		$tuser = $ucount[0]['id'];
	}else{
		$tuser = 0;
	}

	// $tsp = $this->db->query('select C.*, sum(C.subtotal) as tamount, sum(C.qty) as tqty,O.payment_status from cart_mapping as C left join orders as O on O.orderid=C.orderid where date(O.order_time)>="'.$sd_start.'" and date(O.order_time)<="'.$sd_end.'" group by C.productid order by tqty desc limit 3')->result_array();
	$tsp = $this->db->query('SELECT * FROM (SELECT productid,sum(subtotal) as tamount,SUM(qty) as tqty FROM `cart_mapping` WHERE EXISTS (SELECT orderid FROM orders WHERE cart_mapping.orderid=orders.orderid and (date(order_time)>="'.$sd_start.'" and date(order_time)<="'.$sd_end.'")) GROUP BY productid order BY tqty DESC) CM')->result_array();
?>
<style>
	.panel-p{background:linear-gradient(230deg, #759bff, #843cf6);box-shadow:6px 11px 41px -28px #a99de7;border: 0;border-radius: 0.625rem;}
	.panel-s{background:linear-gradient(316deg, #fc5286, #fbaaa2);box-shadow:6px 11px 41px -28px #a99de7;border: 0;border-radius: 0.625rem;}
	.panel-w{background:linear-gradient(135deg, #ffc480, #ff763b);box-shadow:6px 11px 41px -28px #a99de7;border: 0;border-radius: 0.625rem;}
	.filter-btn .btn{box-shadow:6px 11px 41px -28px #a99de7;border-radius: 0.625rem;margin-right: 10px;background: #fff !important;}
	table.dataTable{width:96%;}
</style>
<div class="page-wrapper">
    <div class="container-fluid pt-25">
    	<div class="row">
    		<div class="col-md-8">
    			<h4><?php echo author; ?> Ecommerce Dashboard</h4><br>
    		</div>
    		<?php  ?>
    		<div class="col-md-4 text-right">
    			<!-- <h4>Show Popup &nbsp;<input type="checkbox" name="home_popup" class="home_popup" value="1" data-id="1" <?php //echo ($setting['home_popup']==1)?'checked':""; ?> style="width: 30px;height: 25px;" /></h4> -->
    			<h5><?php echo date('D, d M Y, h:i A'); ?></h5>
    		</div>
    		<?php  ?>
    	</div>
    	<div class="row mb-20">
    		<div class="col-md-12 filter-btn">
    			<label class="btn btn-default btn-outline mr-20" style="cursor:not-allowed;">Show By : </label>
    			<a href="<?php echo base_url(A.'?show_by=1'); ?>" class="btn <?php echo($show_by==1)?'btn-info':'btn-default'; ?> btn-outline">Day</a>
    			<a href="<?php echo base_url(A.'?show_by=2'); ?>" class="btn  <?php echo($show_by==2)?'btn-info':'btn-default'; ?> btn-outline">Week</a>
    			<a href="<?php echo base_url(A.'?show_by=3'); ?>" class="btn  <?php echo($show_by==3)?'btn-info':'btn-default'; ?> btn-outline">Month</a>
    			<a href="<?php echo base_url(A.'?show_by=4'); ?>" class="btn  <?php echo($show_by==4)?'btn-info':'btn-default'; ?> btn-outline">Annual</a>
    		</div>
    	</div>
		<?php if($this->admin_type=='superadmin' || (in_array('12', $this->authority))){ ?>
			<div class="row">
				<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
					<div class="panel panel-p card-view pa-0">
						<div class="panel-wrapper collapse in">
							<div class="panel-body pa-0">
								<div class="sm-data-box">
									<div class="container-fluid">
										<div class="row">
											<div class="col-xs-6 text-center pl-0 pr-0 data-wrap-left">
												<span class="txt-light block counter"><span class="counter-anim"><?php echo $torders; ?></span></span>
												<span class="weight-500 uppercase-font block font-13 txt-light">Total Orders</span>
											</div>
											<div class="col-xs-6 text-center  pl-0 pr-0 data-wrap-right">
												<!-- <i class="zmdi zmdi-storage data-right-rep-icon txt-light"></i> -->
												<img src="http://websentialsdraft.com/aqua-staging/assets/user/images/order.png" width="50">
											</div>
										</div>	
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
					<div class="panel panel-s card-view pa-0">
						<div class="panel-wrapper collapse in">
							<div class="panel-body pa-0">
								<div class="sm-data-box">
									<div class="container-fluid">
										<div class="row">
											<div class="col-xs-6 text-center pl-0 pr-0 data-wrap-left">
												<span class="txt-light block counter">$<span class="counter-anim"><?php echo $tsale; ?></span></span>
												<span class="weight-500 uppercase-font block txt-light">Total Sales</span>
											</div>
											<div class="col-xs-6 text-center  pl-0 pr-0   data-wrap-right">
												<!-- <i class="icon-control-rewind data-right-rep-icon txt-light"></i> -->
												<img src="http://websentialsdraft.com/aqua-staging/assets/user/images/sales.png" width="60">
											</div>
										</div>	
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>		
				<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
					<div class="panel panel-w card-view pa-0">
						<div class="panel-wrapper collapse in">
							<div class="panel-body pa-0">
								<div class="sm-data-box">
									<div class="container-fluid">
										<div class="row">
											<div class="col-xs-6 text-center pl-0 pr-0 data-wrap-left">
												<span class="txt-light block counter"><span class="counter-anim"><?php echo $tuser; ?></span></span>
												<span class="weight-500 uppercase-font block txt-light">Total Users</span>
											</div>
											<div class="col-xs-6 text-center  pl-0 pr-0 data-wrap-right">
												<i class="icon-user-following data-right-rep-icon txt-light"></i>
											</div>
										</div>	
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		<?php } ?>

		<div class="row">
			
	            <div class="col-sm-12">
	                
	            </div>
	    	
		</div>
	    
	    <?php if($this->admin_type=='superadmin' || (in_array('9', $this->authority))){ ?>

            <div class="row">
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <div class="panel panel-primary card-view pa-0">
                        <div class="panel-heading ma-0">
                            <h3 class="panel-title txt-light">New Orders Last 30 Days</h3>
                        </div>
                        <div class="panel-wrapper collapse in">
                            <div class="panel-body">
                                <div class="sm-data-box">
                                    <div class="container-fluid">
                                      	<div class="row">
                                          	<div class="col-md-12 text-center mb-10">
                                          		<h5>Total : <span id="l30daystoreder">0</span></h5>
                                          	</div>
                                          	<div class="col-md-12">
                                              	<div id="days30chartdiv"></div>
                                          	</div>
                                      	</div> 
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div> 
                <?php if($this->admin_type=='superadmin' || (in_array('14', $this->authority))){ ?>
	                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
	            	    <div class="panel panel-default card-view" style="min-height: 480px;">
		                    <div class="panel-heading pb-0" style="background:transparent;">
		                        <div class="pull-left">
		                            <h6 class="panel-title ">Top Selling Products</h6>
		                        </div>
		                        <div class="clearfix"></div>
		                    </div>
		                    <div class="panel-wrapper collapse in">
		                        <div class="panel-body">
		                            <div class="table-wrap" style="">
		                                <div class="table-responsive">
		                                    <table id="datable_2-old" class="table ">
		                                        <!-- <thead>
		                                            <tr>
		                                                <th>Sr. No.</th>
		                                                <th>Product Name</th>
		                                                <th>Sold</th>
		                                                <th>Sales</th>
		                                            </tr>
		                                        </thead> -->
		                                        <tbody>
		                                            <?php 
		                                             if(isset($tsp) && $tsp){ foreach ($tsp as $key => $value) : 
		                                             	$p = $this->common->getsData('products',array('pid'=>$value['productid']),'product_name');
		                                             	if(isset($p) && $p){

		                                             	}else{
		                                             		$p = $this->common->getsData('products',array('productid'=>$value['productid']),'product_name');
		                                             	}
		                                             	$pimage = $this->common->getsData('product_media',array('product_id'=>$value['productid'],'isThumb'=>1));
		                                             	if(isset($p['product_name']) && $p['product_name']!=''){
		                                            ?>
		                                                <tr>
		                                                    <td style="width:15%;">
		                                                    	<?php if(!empty($pimage)){ ?>
											                      <img src="<?php echo base_url($pimage['product_image']); ?>" style="height:60px; width:60px;border:1px solid #ddd;padding: 2px;border-radius:5px;" alt="<?php echo $p['product_name']; ?>">
											                    <?php }else{ ?>
											                      <img src="<?php echo base_url('assets/user/images/order.png'); ?>" style="height:50px; width:50px;background:#ddd;border:1px solid #ddd;padding: 2px;border-radius:5px;" alt="<?php echo $p['product_name']; ?>">
											                    <?php } ?>
		                                                    </td>
		                                                    <td style="width:60%;">
		                                                    	<p><?php echo $p['product_name']; ?></p>
		                                                    	<p>Qty : <?php echo $value['tqty']; ?></p>	
		                                                    </td>
		                                                    <td style="width:25%;">
		                                                    	<h5>$<?php echo $value['tamount']; ?></h5>
		                                                    	<!-- <p><?php //echo($value['payment_status']=='Complete')?'Sales':'Unpaid'; ?></p> -->
		                                                    </td>
		                                                </tr>
		                                            <?php } endforeach; } ?>
		                                        </tbody>
		                                    </table>
		                                </div>
		                            </div>
		                        </div>
		                    </div>
		                </div>  
	                </div>
                <?php } ?>
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class="panel panel-info card-view pa-0">
                        <div class="panel-heading ma-0 pb-10 pt-10">
                        	<div class="row">
	                        	<div class="col-md-4">
	                            	<h3 class="panel-title txt-light">Income Report</h3>
	                            </div>
	                            <div class="col-md-8">
	                                <form id="addPostFrom" action="<?php echo base_url(A); ?>" method="get">
	                                  <div class="row">
	                                      <div class="col-md-4 mtop-1 pl-0">
	                                          <input type="text" class="form-control start_dt" name="start_date"placeholder="Start Date" style="height:36px">
	                                      </div>
	                                      <div class="col-md-4 mtop-1 pl-0">
	                                          <input type="text" class="form-control start_dt" name="end_date"placeholder="End Date" style="height:36px">
	                                      </div>
	                                      <div class="col-md-4 mtop-1 pl-0">
	                                          <input type="submit" class="btn btn-primary" value="search" style="height:36px;padding: 7px 25px;">
	                                      </div>
	                                  </div>
	                                </form>
	                            </div>
	                        </div>
                        </div>
                        <div class="panel-wrapper collapse in">
                            <div class="panel-body">
                                <div class="sm-data-box">
                                    <div class="container-fluid">
                                      	<div class="row">
                                          	<div class="col-md-12 text-center mb-10">
                                          		<h5>Total Income : $<span id="totalincomeorder">0</span></h5>
                                          	</div>
                                          	<div class="col-md-12">
                                              	<div id="chartdiv"></div>
                                          	</div>
                                      	</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div> 
            </div>
            <div class="row">
            	<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <div class="panel panel-success card-view pa-0">
                        <div class="panel-heading ma-0">
                            <h3 class="panel-title txt-light">New Users Last 30 Days</h3>
                        </div>
                        <div class="panel-wrapper collapse in">
                            <div class="panel-body">
                                <div class="sm-data-box">
                                    <div class="container-fluid">
                                      	<div class="row">
                                          	<div class="col-md-12 text-center mb-10">
                                          		<h5>Total : <span id="l30daystuser">0</span></h5>
                                          	</div>
                                          	<div class="col-md-12">
                                              	<div id="l30daystuserchartdiv"></div>
                                          	</div>
                                      	</div> 
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div> 
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <div class="panel panel-warning card-view pa-0">
                        <div class="panel-heading ma-0">
                            <h3 class="panel-title txt-light">Last Year Sales Analysis</h3>
                        </div>
                        <div class="panel-wrapper collapse in">
                            <div class="panel-body">
                                <div class="sm-data-box">
                                    <div class="container-fluid">
                                      	<div class="row">
                                      		<div class="col-md-12 text-center mb-10">
                                          		<h5>Total Income : $<span id="totalyearincomeorder">0</span></h5>
                                          	</div>
                                          	<div class="col-md-12">
                                              	<div id="linechartdiv"></div>
                                          	</div>
                                      	</div> 
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div> 
            </div>
        <?php } ?>

        <div class="row">			
	    	<?php if($this->admin_type=='superadmin' || (in_array('13', $this->authority))){ ?>
				<?php $order_status = $this->config->item("order_status"); ?>
	            <div class="col-sm-12">
	                <div class="panel panel-default card-view">
	                    <div class="panel-heading pb-0" style="background:transparent;">
	                        <div class="pull-left">
	                            <h6 class="panel-title ">Pending Order List</h6>
	                        </div>
	                        <div class="clearfix"></div>
	                    </div>
	                    <div class="panel-wrapper collapse in">
	                        <div class="panel-body">
	                            <div class="table-wrap" style="">
	                                <div class="table-responsive">
	                                    <table id="datable_1" class="table">
	                                        <thead>
	                                            <tr>
	                                                <th>Sr. No.</th>
	                                                <th>Invoice No.</th>
	                                                <th>Date Placed</th>
	                                                <th>Total Amount</th>
	                                                <th>Status</th>
	                                            </tr>
	                                        </thead>
	                                        <tbody>
	                                            <?php 
	                                             if(isset($porders) && $porders){ foreach ($porders as $key => $value) : ?>
	                                                <tr id="<?php echo "orders_".$value['orderid']; ?>">
	                                                    <td><?php echo ($key+1); ?></td>
	                                                    <?php /* ?>
	                                                    <td>
	                                                        <?php if($this->admin_type=='superadmin' || (in_array('4.3', $this->authority))){ ?>
	                                                            <a href="<?php echo base_url(A."/order-invoice/".$value['orderid']); ?>"><?php echo $value['orderid']; ?></a>
	                                                        <?php }else{ ?>
	                                                            <?php echo $value['orderid']; ?>
	                                                        <?php } ?>
	                                                    </td>
	                                                    <?php */ ?>
	                                                    <td>
	                                                    	<?php if($this->admin_type=='superadmin' || (in_array('4.3', $this->authority))){ ?>
	                                                            <a href="<?php echo base_url(A."/order-invoice/".$value['orderid']); ?>"><?php echo $value['invoice_no']; ?></a>
	                                                        <?php }else{ ?>
	                                                            <?php echo $value['invoice_no']; ?>
	                                                        <?php } ?>
	                                                    </td>
	                                                    <td><?php echo date("d M Y h:i A",strtotime($value['order_time'])); ?></td>
	                                                    <td><?php echo $value['grandtotal']; ?></td>
	                                                    <td>
	                                                    	<span class="badge badge-warning-light">
	                                                        	<?php echo (isset($order_status[$value['order_status']]) && $order_status[$value['order_status']])?$order_status[$value['order_status']]:''; ?>
	                                                        </span>
	                                                    </td>
	                                                </tr>
	                                            <?php endforeach; } ?>
	                                        </tbody>
	                                    </table>
	                                </div>
	                            </div>
	                        </div>
	                    </div>
	                </div>  
	            </div>
	    	<?php } ?>
	    </div>
	</div>
<style>
    #days30chartdiv {
      width: 100%;
      height: 350px;
    }
    #l30daystuserchartdiv {
      width: 100%;
      height: 400px;
    }
    #chartdiv {
      width: 100%;
      height: 400px;
    }
    #linechartdiv {
      width: 100%;
      height: 400px;
    }
</style>
<?php  
    function date_range($start_date, $end_date, $step = '+1 day', $output_format='Y-m-d'){
      $dates = array();
      $current = strtotime($start_date);
      $end_date = strtotime($end_date);
      while( $current <= $end_date ) {
          $dates[] = date($output_format, $current);
          $current = strtotime($step, $current);
      }
      return $dates;
    }
?>
<!-- Resources -->
<script src="https://cdn.amcharts.com/lib/5/index.js"></script>
<script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
<script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>

<?php  
	$start_date2 = date('Y-m-d',strtotime(date('Y-m-d').' - 29 days'));
    $end_date2 = date('Y-m-d');
    $dates2 = date_range($start_date2, $end_date2);
    $t30order = 0;
    $g30data = '[';
    if($dates2){ 
        foreach ($dates2 as $key => $value){
            $graphorders2 = $this->db->query('select count(orderid) as ogt from orders where date(order_time)="'.$value.'" and order_status!=5')->result_array();
            $gt2 = $graphorders2[0]['ogt'];
            $gt2 = ($gt2>0)?$gt2:0;
            $g30data .= '{date: "'.$value.'",value: '.$gt2.'},';
            $t30order += $gt2;
        }
    }
    $g30data .= ']';
?>
<script>
$('#l30daystoreder').html('<?php echo $t30order; ?>');
am5.ready(function() {
var root = am5.Root.new("days30chartdiv");
root.setThemes([
  am5themes_Animated.new(root)
]);

var chart = root.container.children.push(am5xy.XYChart.new(root, {
  panX: true,
  panY: true,
  wheelX: "panX",
  wheelY: "zoomX"
}));

var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {}));
cursor.lineY.set("visible", false);

var xRenderer = am5xy.AxisRendererX.new(root, { minGridDistance: 30 });
xRenderer.labels.template.setAll({
  rotation: -65,
  centerY: am5.p50,
  centerX: am5.p100,
  paddingRight: 15
});

var xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, {
  maxDeviation: 0.3,
  categoryField: "date",
  renderer: xRenderer,
  tooltip: am5.Tooltip.new(root, {})
}));

var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
  maxDeviation: 0.3,
  renderer: am5xy.AxisRendererY.new(root, {})
}));

var series = chart.series.push(am5xy.ColumnSeries.new(root, {
  name: "Series 1",
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  sequencedInterpolation: true,
  categoryXField: "date",
  tooltip: am5.Tooltip.new(root, {
    labelText:"{valueY}"
  })
}));

series.columns.template.setAll({ cornerRadiusTL: 5, cornerRadiusTR: 5 });
series.columns.template.adapters.add("fill", (fill, target) => {
  return chart.get("colors").getIndex(series.columns.indexOf(target));
});

series.columns.template.adapters.add("stroke", (stroke, target) => {
  return chart.get("colors").getIndex(series.columns.indexOf(target));
});


// Set data
var data = <?php echo $g30data; ?>;

xAxis.data.setAll(data);
series.data.setAll(data);

series.appear(1000);
chart.appear(1000, 100);

}); // end am5.ready()
</script>
<?php  
	$start_date3 = date('Y-m-d',strtotime(date('Y-m-d').' - 29 days'));
    $end_date3 = date('Y-m-d');
    $dates3 = date_range($start_date3, $end_date3);
    $t30user = 0;
    $g30udata = '[';
    if($dates3){ 
        foreach ($dates3 as $key => $value){
            $graphuser = $this->db->query('select count(id) as ugt from user where date(reg_date)="'.$value.'"')->result_array();
            $ugt2 = $graphuser[0]['ugt'];
            $ugt2 = ($ugt2>0)?$ugt2:0;
            $g30udata .= '{date: "'.$value.'",value: '.$ugt2.'},';
            $t30user += $ugt2;
        }
    }
    $g30udata .= ']';
?>
<script>
$('#l30daystuser').html('<?php echo $t30user; ?>');
am5.ready(function() {
var root = am5.Root.new("l30daystuserchartdiv");
root.setThemes([
  am5themes_Animated.new(root)
]);

var chart = root.container.children.push(am5xy.XYChart.new(root, {
  panX: true,
  panY: true,
  wheelX: "panX",
  wheelY: "zoomX"
}));

var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {}));
cursor.lineY.set("visible", false);

var xRenderer = am5xy.AxisRendererX.new(root, { minGridDistance: 30 });
xRenderer.labels.template.setAll({
  rotation: -65,
  centerY: am5.p50,
  centerX: am5.p100,
  paddingRight: 15
});

var xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, {
  maxDeviation: 0.3,
  categoryField: "date",
  renderer: xRenderer,
  tooltip: am5.Tooltip.new(root, {})
}));

var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
  maxDeviation: 0.3,
  renderer: am5xy.AxisRendererY.new(root, {})
}));

var series = chart.series.push(am5xy.ColumnSeries.new(root, {
  name: "Series 1",
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  sequencedInterpolation: true,
  categoryXField: "date",
  tooltip: am5.Tooltip.new(root, {
    labelText:"{valueY}"
  })
}));

series.columns.template.setAll({ cornerRadiusTL: 5, cornerRadiusTR: 5 });
series.columns.template.adapters.add("fill", (fill, target) => {
  return chart.get("colors").getIndex(series.columns.indexOf(target));
});

series.columns.template.adapters.add("stroke", (stroke, target) => {
  return chart.get("colors").getIndex(series.columns.indexOf(target));
});


// Set data
var data = <?php echo $g30udata; ?>;

xAxis.data.setAll(data);
series.data.setAll(data);

series.appear(1000);
chart.appear(1000, 100);

}); // end am5.ready()
</script>

<?php  
    if((isset($_GET['start_date']) && $_GET['start_date']!='') && (isset($_GET['end_date']) && $_GET['end_date']!='')){
        $start_date = $_GET['start_date'];
        $end_date = $_GET['end_date'];
    }else if((isset($_GET['start_date']) && $_GET['start_date']!='')){
        $start_date = $_GET['start_date'];
        $end_date = date('Y-m-d');
    }else if((isset($_GET['end_date']) && $_GET['end_date']!='')){
        $start_date = $_GET['end_date'];
        $end_date = date('Y-m-d');
    }else{
        $start_date = date('Y-m-d',strtotime(date('Y-m-d').' - 29 days'));
        $end_date = date('Y-m-d');
    }

    $dates = date_range($start_date, $end_date);
    // echo "<pre>";
    // print_r ($dates);
    // echo "</pre>";
    $totalincomeorder = 0;
    $gdata = '[';
    if($dates){ 
        foreach ($dates as $key => $value){
            $graphorders = $this->db->query('select sum(grandtotal) as gt from orders where date(order_time)="'.$value.'" and order_status!=5')->result_array();
            $gt = $graphorders[0]['gt'];
            $gt = ($gt>0)?$gt:0;
            $gdata .= '{date: "'.$value.'",value: '.$gt.'},';
            $totalincomeorder += $gt;
        }
    }
    $gdata .= ']';
    // echo "<pre>";
    // print_r ($gdata);
    // echo "</pre>";
?>

<script>
var totalincomeorder = '<?php echo number_format($totalincomeorder,2); ?>';
$('#totalincomeorder').html(totalincomeorder);
am5.ready(function() {
var root = am5.Root.new("chartdiv");
root.setThemes([
  am5themes_Animated.new(root)
]);

var chart = root.container.children.push(am5xy.XYChart.new(root, {
  panX: true,
  panY: true,
  wheelX: "panX",
  wheelY: "zoomX"
}));

var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {}));
cursor.lineY.set("visible", false);

var xRenderer = am5xy.AxisRendererX.new(root, { minGridDistance: 30 });
xRenderer.labels.template.setAll({
  rotation: -65,
  centerY: am5.p50,
  centerX: am5.p100,
  paddingRight: 15
});

var xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, {
  maxDeviation: 0.3,
  categoryField: "date",
  renderer: xRenderer,
  tooltip: am5.Tooltip.new(root, {})
}));

var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
  maxDeviation: 0.3,
  renderer: am5xy.AxisRendererY.new(root, {})
}));

var series = chart.series.push(am5xy.ColumnSeries.new(root, {
  name: "Series 1",
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  sequencedInterpolation: true,
  categoryXField: "date",
  tooltip: am5.Tooltip.new(root, {
    labelText:"{valueY}"
  })
}));

series.columns.template.setAll({ cornerRadiusTL: 5, cornerRadiusTR: 5 });
series.columns.template.adapters.add("fill", (fill, target) => {
  return chart.get("colors").getIndex(series.columns.indexOf(target));
});

series.columns.template.adapters.add("stroke", (stroke, target) => {
  return chart.get("colors").getIndex(series.columns.indexOf(target));
});


// Set data
var data = <?php echo $gdata; ?>;

xAxis.data.setAll(data);
series.data.setAll(data);

series.appear(1000);
chart.appear(1000, 100);

}); // end am5.ready()
</script>
<?php  
	$start_date1 = date('Y-m-d',strtotime(date('Y-m-d').' - 1 years'));
    $end_date1 = date('Y-m-d');
    $dates1 = date_range($start_date1, $end_date1);
    $totalyearincomeorder = 0;
    $mgdata = '[';
    if($dates1){ 
        foreach ($dates1 as $key => $value){
            $mgraphorders = $this->db->query('select sum(grandtotal) as gt from orders where date(order_time)="'.$value.'" and order_status!=5')->result_array();
            $mgt = $mgraphorders[0]['gt'];
            $mgt = ($mgt>0)?$mgt:0;
            $mgdata .= '{date: "'.$value.'",value: '.$mgt.'},';
            $totalyearincomeorder += $mgt;
        }
    }
    $mgdata .= ']';
?>
<script>
var totalyearincomeorder = '<?php echo number_format($totalyearincomeorder,2); ?>';
$('#totalyearincomeorder').html(totalyearincomeorder);
am5.ready(function() {

var root = am5.Root.new("linechartdiv");

root.setThemes([
  am5themes_Animated.new(root)
]);

root.dateFormatter.setAll({
  dateFormat: "yyyy",
  dateFields: ["valueX"]
});

var data = <?php echo $mgdata; ?>;


// Create chart
// https://www.amcharts.com/docs/v5/charts/xy-chart/
var chart = root.container.children.push(am5xy.XYChart.new(root, {
  focusable: true,
  panX: true,
  panY: true,
  wheelX: "panX",
  wheelY: "zoomX",
  pinchZoomX:true
}));

var easing = am5.ease.linear;


// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xAxis = chart.xAxes.push(am5xy.DateAxis.new(root, {
  maxDeviation: 0.1,
  groupData: false,
  baseInterval: {
    timeUnit: "day",
    count: 1
  },
  renderer: am5xy.AxisRendererX.new(root, {

  }),
  tooltip: am5.Tooltip.new(root, {})
}));

var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
  maxDeviation: 0.2,
  renderer: am5xy.AxisRendererY.new(root, {})
}));


// Add series
// https://www.amcharts.com/docs/v5/charts/xy-chart/series/
var series = chart.series.push(am5xy.LineSeries.new(root, {
  minBulletDistance: 10,
  connect: false,
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  valueXField: "date",
  tooltip: am5.Tooltip.new(root, {
    pointerOrientation: "horizontal",
    labelText: "{valueY}"
  })
}));

series.fills.template.setAll({
  fillOpacity: 0.2,
  visible: true
});

series.strokes.template.setAll({
  strokeWidth: 2
});


// Set up data processor to parse string dates
// https://www.amcharts.com/docs/v5/concepts/data/#Pre_processing_data
series.data.processor = am5.DataProcessor.new(root, {
  dateFormat: "yyyy-MM-dd",
  dateFields: ["date"]
});

series.data.setAll(data);

series.bullets.push(function() {
  var circle = am5.Circle.new(root, {
    radius: 4,
    fill: root.interfaceColors.get("background"),
    stroke: series.get("fill"),
    strokeWidth: 2
  })

  return am5.Bullet.new(root, {
    sprite: circle
  })
});

var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
  xAxis: xAxis,
  behavior: "none"
}));
cursor.lineY.set("visible", false);

// add scrollbar
chart.set("scrollbarX", am5.Scrollbar.new(root, {
  orientation: "horizontal"
}));

chart.appear(1000, 100);

});
</script>

<script>
	var site_url = "<?php echo base_url(); ?>";
	var urlLink = "<?php echo A; ?>";
	$('.home_popup').change(function(){
	    var td = this.checked ? '1' : '0';
	    var pid = $(this).attr('data-id');
	    $.ajax({
	        url: site_url+urlLink+"/popupSetting",
	        type: "POST",
	        data: "id="+pid+"&home_popup="+td,
	        success: function(data){ 
	        	alert('Setting Update'); 
	        }
	    });
	});
</script>

<style>
	.sm-data-box .data-wrap-right
	{
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 46px 0 ;
	}
</style>